Search in sources :

Example 11 with SearchResponse

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());
    }
}
Also used : Script(org.elasticsearch.script.Script) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) HashMap(java.util.HashMap) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 12 with SearchResponse

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));
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.range.Range.Bucket) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) HashMap(java.util.HashMap) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) Range(org.elasticsearch.search.aggregations.bucket.range.Range) RangeAggregationBuilder(org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Bucket(org.elasticsearch.search.aggregations.bucket.range.Range.Bucket) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 13 with SearchResponse

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());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) InternalFilters(org.elasticsearch.search.aggregations.bucket.filters.InternalFilters) XContentType(org.elasticsearch.common.xcontent.XContentType) FiltersAggregator(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator) FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder) WrapperQueryBuilder(org.elasticsearch.index.query.WrapperQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 14 with SearchResponse

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));
}
Also used : GeoBounds(org.elasticsearch.search.aggregations.metrics.geobounds.GeoBounds) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 15 with SearchResponse

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());
}
Also used : Cardinality(org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1451 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)239 Script (org.elasticsearch.script.Script)223 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)208 Matchers.containsString (org.hamcrest.Matchers.containsString)156 ArrayList (java.util.ArrayList)143 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)131 SearchHit (org.elasticsearch.search.SearchHit)112 HashMap (java.util.HashMap)102 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)93 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)78 GeoPoint (org.elasticsearch.common.geo.GeoPoint)70 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)69 SearchHits (org.elasticsearch.search.SearchHits)60 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)60 DateTime (org.joda.time.DateTime)59 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)58 Range (org.elasticsearch.search.aggregations.bucket.range.Range)50