Search in sources :

Example 46 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class RangeIT method testRangeAsSubAggregation.

public void testRangeAsSubAggregation() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field(MULTI_VALUED_FIELD_NAME).size(100).collectMode(randomFrom(SubAggCollectionMode.values())).subAggregation(range("range").field(SINGLE_VALUED_FIELD_NAME).addUnboundedTo(3).addRange(3, 6).addUnboundedFrom(6))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getBuckets().size(), equalTo(numDocs + 1));
    for (int i = 1; i < numDocs + 2; ++i) {
        Terms.Bucket bucket = terms.getBucketByKey("" + i);
        assertThat(bucket, notNullValue());
        final long docCount = i == 1 || i == numDocs + 1 ? 1 : 2;
        assertThat(bucket.getDocCount(), equalTo(docCount));
        Range range = bucket.getAggregations().get("range");
        List<? extends Bucket> buckets = range.getBuckets();
        Range.Bucket rangeBucket = buckets.get(0);
        assertThat(rangeBucket.getKey(), equalTo("*-3.0"));
        assertThat(rangeBucket.getKeyAsString(), equalTo("*-3.0"));
        assertThat(rangeBucket, notNullValue());
        assertThat(rangeBucket.getFromAsString(), nullValue());
        assertThat(rangeBucket.getToAsString(), equalTo("3.0"));
        if (i == 1 || i == 3) {
            assertThat(rangeBucket.getDocCount(), equalTo(1L));
        } else if (i == 2) {
            assertThat(rangeBucket.getDocCount(), equalTo(2L));
        } else {
            assertThat(rangeBucket.getDocCount(), equalTo(0L));
        }
        rangeBucket = buckets.get(1);
        assertThat(rangeBucket.getKey(), equalTo("3.0-6.0"));
        assertThat(rangeBucket.getKeyAsString(), equalTo("3.0-6.0"));
        assertThat(rangeBucket, notNullValue());
        assertThat(rangeBucket.getFromAsString(), equalTo("3.0"));
        assertThat(rangeBucket.getToAsString(), equalTo("6.0"));
        if (i == 3 || i == 6) {
            assertThat(rangeBucket.getDocCount(), equalTo(1L));
        } else if (i == 4 || i == 5) {
            assertThat(rangeBucket.getDocCount(), equalTo(2L));
        } else {
            assertThat(rangeBucket.getDocCount(), equalTo(0L));
        }
        rangeBucket = buckets.get(2);
        assertThat(rangeBucket.getKey(), equalTo("6.0-*"));
        assertThat(rangeBucket.getKeyAsString(), equalTo("6.0-*"));
        assertThat(rangeBucket, notNullValue());
        assertThat(rangeBucket.getFromAsString(), equalTo("6.0"));
        assertThat(rangeBucket.getToAsString(), nullValue());
        if (i == 6 || i == numDocs + 1) {
            assertThat(rangeBucket.getDocCount(), equalTo(1L));
        } else if (i < 6) {
            assertThat(rangeBucket.getDocCount(), equalTo(0L));
        } else {
            assertThat(rangeBucket.getDocCount(), equalTo(2L));
        }
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.range.Range.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Range(org.elasticsearch.search.aggregations.bucket.range.Range) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 47 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class NaNSortingIT method assertCorrectlySorted.

private void assertCorrectlySorted(Terms terms, boolean asc, SubAggregation agg) {
    assertThat(terms, notNullValue());
    double previousValue = asc ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
    for (Terms.Bucket bucket : terms.getBuckets()) {
        Aggregation sub = bucket.getAggregations().get(agg.name);
        double value = agg.getValue(sub);
        assertTrue(Comparators.compareDiscardNaN(previousValue, value, asc) <= 0);
        previousValue = value;
    }
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms)

Example 48 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class NestedIT method testNestedSameDocIdProcessedMultipleTime.

public void testNestedSameDocIdProcessedMultipleTime() throws Exception {
    assertAcked(prepareCreate("idx4").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0)).addMapping("product", "categories", "type=keyword", "name", "type=text", "property", "type=nested"));
    ensureGreen("idx4");
    client().prepareIndex("idx4", "product", "1").setSource(jsonBuilder().startObject().field("name", "product1").array("categories", "1", "2", "3", "4").startArray("property").startObject().field("id", 1).endObject().startObject().field("id", 2).endObject().startObject().field("id", 3).endObject().endArray().endObject()).get();
    client().prepareIndex("idx4", "product", "2").setSource(jsonBuilder().startObject().field("name", "product2").array("categories", "1", "2").startArray("property").startObject().field("id", 1).endObject().startObject().field("id", 5).endObject().startObject().field("id", 4).endObject().endArray().endObject()).get();
    refresh();
    SearchResponse response = client().prepareSearch("idx4").setTypes("product").addAggregation(terms("category").field("categories").subAggregation(nested("property", "property").subAggregation(terms("property_id").field("property.id")))).get();
    assertNoFailures(response);
    assertHitCount(response, 2);
    Terms category = response.getAggregations().get("category");
    assertThat(category.getBuckets().size(), equalTo(4));
    Terms.Bucket bucket = category.getBucketByKey("1");
    assertThat(bucket.getDocCount(), equalTo(2L));
    Nested property = bucket.getAggregations().get("property");
    assertThat(property.getDocCount(), equalTo(6L));
    Terms propertyId = property.getAggregations().get("property_id");
    assertThat(propertyId.getBuckets().size(), equalTo(5));
    assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(2L));
    assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("4").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("5").getDocCount(), equalTo(1L));
    bucket = category.getBucketByKey("2");
    assertThat(bucket.getDocCount(), equalTo(2L));
    property = bucket.getAggregations().get("property");
    assertThat(property.getDocCount(), equalTo(6L));
    propertyId = property.getAggregations().get("property_id");
    assertThat(propertyId.getBuckets().size(), equalTo(5));
    assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(2L));
    assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("4").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("5").getDocCount(), equalTo(1L));
    bucket = category.getBucketByKey("3");
    assertThat(bucket.getDocCount(), equalTo(1L));
    property = bucket.getAggregations().get("property");
    assertThat(property.getDocCount(), equalTo(3L));
    propertyId = property.getAggregations().get("property_id");
    assertThat(propertyId.getBuckets().size(), equalTo(3));
    assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
    bucket = category.getBucketByKey("4");
    assertThat(bucket.getDocCount(), equalTo(1L));
    property = bucket.getAggregations().get("property");
    assertThat(property.getDocCount(), equalTo(3L));
    propertyId = property.getAggregations().get("property_id");
    assertThat(propertyId.getBuckets().size(), equalTo(3));
    assertThat(propertyId.getBucketByKey("1").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("2").getDocCount(), equalTo(1L));
    assertThat(propertyId.getBucketByKey("3").getDocCount(), equalTo(1L));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) LongTerms(org.elasticsearch.search.aggregations.bucket.terms.LongTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 49 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class ShardReduceIT method testStringTerms.

public void testStringTerms() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setQuery(QueryBuilders.matchAllQuery()).addAggregation(terms("terms").field("term-s").collectMode(randomFrom(SubAggCollectionMode.values())).subAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.DAY).minDocCount(0))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    Histogram histo = terms.getBucketByKey("term").getAggregations().get("histo");
    assertThat(histo.getBuckets().size(), equalTo(4));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 50 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class ShardSizeTermsIT method testNoShardSizeString.

public void testNoShardSizeString() throws Exception {
    createIdx("type=keyword");
    indexData();
    SearchResponse response = client().prepareSearch("idx").setTypes("type").setQuery(matchAllQuery()).addAggregation(terms("keys").field("key").size(3).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count(false))).execute().actionGet();
    Terms terms = response.getAggregations().get("keys");
    Collection<Terms.Bucket> buckets = terms.getBuckets();
    assertThat(buckets.size(), equalTo(3));
    Map<String, Long> expected = new HashMap<>();
    expected.put("1", 8L);
    expected.put("3", 8L);
    expected.put("2", 5L);
    for (Terms.Bucket bucket : buckets) {
        assertThat(bucket.getDocCount(), equalTo(expected.get(bucket.getKeyAsString())));
    }
}
Also used : HashMap(java.util.HashMap) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)252 SearchResponse (org.elasticsearch.action.search.SearchResponse)238 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)209 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)97 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)37 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)36 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)30 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)26 Script (org.elasticsearch.script.Script)22 ArrayList (java.util.ArrayList)21 HashMap (java.util.HashMap)21 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)16 ExtendedStats (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats)16 SearchHits (org.elasticsearch.search.SearchHits)13 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)13 InternalBucketMetricValue (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue)13 Avg (org.elasticsearch.search.aggregations.metrics.avg.Avg)12 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)11 Sampler (org.elasticsearch.search.aggregations.bucket.sampler.Sampler)10 PipelineAggregatorBuilders.percentilesBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.percentilesBucket)9