use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class EquivalenceIT method testDuelTermsHistogram.
// Duel between histograms and scripted terms
public void testDuelTermsHistogram() throws Exception {
prepareCreate("idx").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("num").field("type", "double").endObject().endObject().endObject().endObject()).execute().actionGet();
final int numDocs = scaledRandomIntBetween(500, 5000);
final int maxNumTerms = randomIntBetween(10, 2000);
final int interval = randomIntBetween(1, 100);
final Integer[] values = new Integer[maxNumTerms];
for (int i = 0; i < values.length; ++i) {
values[i] = randomInt(maxNumTerms * 3) - maxNumTerms;
}
for (int i = 0; i < numDocs; ++i) {
XContentBuilder source = jsonBuilder().startObject().field("num", randomDouble()).startArray("values");
final int numValues = randomInt(4);
for (int j = 0; j < numValues; ++j) {
source = source.value(randomFrom(values));
}
source = source.endArray().endObject();
client().prepareIndex("idx", "type").setSource(source).execute().actionGet();
}
assertNoFailures(client().admin().indices().prepareRefresh("idx").setIndicesOptions(IndicesOptions.lenientExpandOpen()).execute().get());
Map<String, Object> params = new HashMap<>();
params.put("interval", interval);
SearchResponse resp = client().prepareSearch("idx").addAggregation(terms("terms").field("values").collectMode(randomFrom(SubAggCollectionMode.values())).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "floor(_value / interval)", params)).size(maxNumTerms)).addAggregation(histogram("histo").field("values").interval(interval).minDocCount(1)).execute().actionGet();
assertSearchResponse(resp);
Terms terms = resp.getAggregations().get("terms");
assertThat(terms, notNullValue());
Histogram histo = resp.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(terms.getBuckets().size(), equalTo(histo.getBuckets().size()));
for (Histogram.Bucket bucket : histo.getBuckets()) {
final double key = ((Number) bucket.getKey()).doubleValue() / interval;
final Terms.Bucket termsBucket = terms.getBucketByKey(String.valueOf(key));
assertEquals(bucket.getDocCount(), termsBucket.getDocCount());
}
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class EquivalenceIT method testRandomRanges.
// Make sure that unordered, reversed, disjoint and/or overlapping ranges are supported
// Duel with filters
public void testRandomRanges() throws Exception {
final int numDocs = scaledRandomIntBetween(500, 5000);
final double[][] docs = new double[numDocs][];
for (int i = 0; i < numDocs; ++i) {
final int numValues = randomInt(5);
docs[i] = new double[numValues];
for (int j = 0; j < numValues; ++j) {
docs[i][j] = randomDouble() * 100;
}
}
prepareCreate("idx").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("values").field("type", "double").endObject().endObject().endObject().endObject()).execute().actionGet();
for (int i = 0; i < docs.length; ++i) {
XContentBuilder source = jsonBuilder().startObject().startArray("values");
for (int j = 0; j < docs[i].length; ++j) {
source = source.value(docs[i][j]);
}
source = source.endArray().endObject();
client().prepareIndex("idx", "type").setSource(source).execute().actionGet();
}
assertNoFailures(client().admin().indices().prepareRefresh("idx").setIndicesOptions(IndicesOptions.lenientExpandOpen()).get());
final int numRanges = randomIntBetween(1, 20);
final double[][] ranges = new double[numRanges][];
for (int i = 0; i < ranges.length; ++i) {
switch(randomInt(2)) {
case 0:
ranges[i] = new double[] { Double.NEGATIVE_INFINITY, randomInt(100) };
break;
case 1:
ranges[i] = new double[] { randomInt(100), Double.POSITIVE_INFINITY };
break;
case 2:
ranges[i] = new double[] { randomInt(100), randomInt(100) };
break;
default:
throw new AssertionError();
}
}
RangeAggregationBuilder query = range("range").field("values");
for (int i = 0; i < ranges.length; ++i) {
String key = Integer.toString(i);
if (ranges[i][0] == Double.NEGATIVE_INFINITY) {
query.addUnboundedTo(key, ranges[i][1]);
} else if (ranges[i][1] == Double.POSITIVE_INFINITY) {
query.addUnboundedFrom(key, ranges[i][0]);
} else {
query.addRange(key, ranges[i][0], ranges[i][1]);
}
}
SearchRequestBuilder reqBuilder = client().prepareSearch("idx").addAggregation(query);
for (int i = 0; i < ranges.length; ++i) {
RangeQueryBuilder filter = QueryBuilders.rangeQuery("values");
if (ranges[i][0] != Double.NEGATIVE_INFINITY) {
filter = filter.from(ranges[i][0]);
}
if (ranges[i][1] != Double.POSITIVE_INFINITY) {
filter = filter.to(ranges[i][1]);
}
reqBuilder = reqBuilder.addAggregation(filter("filter" + i, filter));
}
SearchResponse resp = reqBuilder.execute().actionGet();
Range range = resp.getAggregations().get("range");
List<? extends Bucket> buckets = range.getBuckets();
HashMap<String, Bucket> bucketMap = new HashMap<>(buckets.size());
for (Bucket bucket : buckets) {
bucketMap.put(bucket.getKeyAsString(), bucket);
}
for (int i = 0; i < ranges.length; ++i) {
long count = 0;
for (double[] values : docs) {
for (double value : values) {
if (value >= ranges[i][0] && value < ranges[i][1]) {
++count;
break;
}
}
}
final Range.Bucket bucket = bucketMap.get(Integer.toString(i));
assertEquals(bucket.getKeyAsString(), Integer.toString(i), bucket.getKeyAsString());
assertEquals(bucket.getKeyAsString(), count, bucket.getDocCount());
final Filter filter = resp.getAggregations().get("filter" + i);
assertThat(filter.getDocCount(), equalTo(count));
}
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class FiltersAggsRewriteIT method testWrapperQueryIsRewritten.
public void testWrapperQueryIsRewritten() throws IOException {
createIndex("test", Settings.EMPTY, "test", "title", "type=text");
client().prepareIndex("test", "test", "1").setSource("title", "foo bar baz").get();
client().prepareIndex("test", "test", "2").setSource("title", "foo foo foo").get();
client().prepareIndex("test", "test", "3").setSource("title", "bar baz bax").get();
client().admin().indices().prepareRefresh("test").get();
XContentType xContentType = randomFrom(XContentType.values());
BytesReference bytesReference;
try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType)) {
builder.startObject();
{
builder.startObject("terms");
{
builder.array("title", "foo");
}
builder.endObject();
}
builder.endObject();
bytesReference = builder.bytes();
}
FiltersAggregationBuilder builder = new FiltersAggregationBuilder("titles", new FiltersAggregator.KeyedFilter("titleterms", new WrapperQueryBuilder(bytesReference)));
SearchResponse searchResponse = client().prepareSearch("test").setSize(0).addAggregation(builder).get();
assertEquals(3, searchResponse.getHits().getTotalHits());
InternalFilters filters = searchResponse.getAggregations().get("titles");
assertEquals(1, filters.getBuckets().size());
assertEquals(2, filters.getBuckets().get(0).getDocCount());
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class MissingValueIT method testGeoBounds.
public void testGeoBounds() {
SearchResponse response = client().prepareSearch("idx").addAggregation(geoBounds("bounds").field("location").missing("2,1")).get();
assertSearchResponse(response);
GeoBounds bounds = response.getAggregations().get("bounds");
assertThat(bounds.bottomRight().lat(), closeTo(1.0, 1E-5));
assertThat(bounds.bottomRight().lon(), closeTo(2.0, 1E-5));
assertThat(bounds.topLeft().lat(), closeTo(2.0, 1E-5));
assertThat(bounds.topLeft().lon(), closeTo(1.0, 1E-5));
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class MissingValueIT method testCardinality.
public void testCardinality() {
SearchResponse response = client().prepareSearch("idx").addAggregation(cardinality("card").field("long").missing(2)).get();
assertSearchResponse(response);
Cardinality cardinality = response.getAggregations().get("card");
assertEquals(2, cardinality.getValue());
}
Aggregations