Search in sources :

Example 86 with Terms

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

the class TDigestPercentilesIT method testOrderByEmptyAggregation.

@Override
public void testOrderByEmptyAggregation() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(terms("terms").field("value").order(Terms.Order.compound(Terms.Order.aggregation("filter>percentiles.99", true))).subAggregation(filter("filter", termQuery("value", 100)).subAggregation(percentiles("percentiles").method(PercentilesMethod.TDIGEST).field("value")))).get();
    assertHitCount(searchResponse, 10);
    Terms terms = searchResponse.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    List<Terms.Bucket> buckets = terms.getBuckets();
    assertThat(buckets, notNullValue());
    assertThat(buckets.size(), equalTo(10));
    for (int i = 0; i < 10; i++) {
        Terms.Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat(bucket.getKeyAsNumber(), equalTo((long) i + 1));
        assertThat(bucket.getDocCount(), equalTo(1L));
        Filter filter = bucket.getAggregations().get("filter");
        assertThat(filter, notNullValue());
        assertThat(filter.getDocCount(), equalTo(0L));
        Percentiles percentiles = filter.getAggregations().get("percentiles");
        assertThat(percentiles, notNullValue());
        assertThat(percentiles.percentile(99), equalTo(Double.NaN));
    }
}
Also used : Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Percentiles(org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 87 with Terms

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

the class SumIT method testOrderByEmptyAggregation.

@Override
public void testOrderByEmptyAggregation() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(terms("terms").field("value").order(Order.compound(Order.aggregation("filter>sum", true))).subAggregation(filter("filter", termQuery("value", 100)).subAggregation(sum("sum").field("value")))).get();
    assertHitCount(searchResponse, 10);
    Terms terms = searchResponse.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    List<Terms.Bucket> buckets = terms.getBuckets();
    assertThat(buckets, notNullValue());
    assertThat(buckets.size(), equalTo(10));
    for (int i = 0; i < 10; i++) {
        Terms.Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat(bucket.getKeyAsNumber(), equalTo((long) i + 1));
        assertThat(bucket.getDocCount(), equalTo(1L));
        Filter filter = bucket.getAggregations().get("filter");
        assertThat(filter, notNullValue());
        assertThat(filter.getDocCount(), equalTo(0L));
        Sum sum = filter.getAggregations().get("sum");
        assertThat(sum, notNullValue());
        assertThat(sum.value(), equalTo(0.0));
    }
}
Also used : Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 88 with Terms

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

the class TopHitsIT method testBreadthFirstWithScoreNeeded.

public void testBreadthFirstWithScoreNeeded() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).collectMode(SubAggCollectionMode.BREADTH_FIRST).field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").size(3))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    for (int i = 0; i < 5; i++) {
        Terms.Bucket bucket = terms.getBucketByKey("val" + i);
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + i));
        assertThat(bucket.getDocCount(), equalTo(10L));
        TopHits topHits = bucket.getAggregations().get("hits");
        SearchHits hits = topHits.getHits();
        assertThat(hits.getTotalHits(), equalTo(10L));
        assertThat(hits.getHits().length, equalTo(3));
        assertThat(hits.getAt(0).getSourceAsMap().size(), equalTo(4));
    }
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 89 with Terms

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

the class TopHitsIT method testPagination.

public void testPagination() throws Exception {
    int size = randomIntBetween(1, 10);
    int from = randomIntBetween(0, 10);
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").sort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC)).from(from).size(size))).get();
    assertSearchResponse(response);
    SearchResponse control = client().prepareSearch("idx").setTypes("type").setFrom(from).setSize(size).setPostFilter(QueryBuilders.termQuery(TERMS_AGGS_FIELD, "val0")).addSort(SORT_FIELD, SortOrder.DESC).get();
    assertSearchResponse(control);
    SearchHits controlHits = control.getHits();
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    Terms.Bucket bucket = terms.getBucketByKey("val0");
    assertThat(bucket, notNullValue());
    assertThat(bucket.getDocCount(), equalTo(10L));
    TopHits topHits = bucket.getAggregations().get("hits");
    SearchHits hits = topHits.getHits();
    assertThat(hits.getTotalHits(), equalTo(controlHits.getTotalHits()));
    assertThat(hits.getHits().length, equalTo(controlHits.getHits().length));
    for (int i = 0; i < hits.getHits().length; i++) {
        logger.info("{}: top_hits: [{}][{}] control: [{}][{}]", i, hits.getAt(i).getId(), hits.getAt(i).getSortValues()[0], controlHits.getAt(i).getId(), controlHits.getAt(i).getSortValues()[0]);
        assertThat(hits.getAt(i).getId(), equalTo(controlHits.getAt(i).getId()));
        assertThat(hits.getAt(i).getSortValues()[0], equalTo(controlHits.getAt(i).getSortValues()[0]));
    }
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 90 with Terms

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

the class TopHitsIT method testFieldCollapsing.

public void testFieldCollapsing() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("field-collapsing").setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(matchQuery("text", "term rare")).addAggregation(terms("terms").executionHint(randomExecutionHint()).field("group").order(Terms.Order.aggregation("max_score", false)).subAggregation(topHits("hits").size(1)).subAggregation(max("max_score").field("value"))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(3));
    Iterator<Terms.Bucket> bucketIterator = terms.getBuckets().iterator();
    Terms.Bucket bucket = bucketIterator.next();
    assertThat(key(bucket), equalTo("b"));
    TopHits topHits = bucket.getAggregations().get("hits");
    SearchHits hits = topHits.getHits();
    assertThat(hits.getTotalHits(), equalTo(4L));
    assertThat(hits.getHits().length, equalTo(1));
    assertThat(hits.getAt(0).getId(), equalTo("6"));
    bucket = bucketIterator.next();
    assertThat(key(bucket), equalTo("c"));
    topHits = bucket.getAggregations().get("hits");
    hits = topHits.getHits();
    assertThat(hits.getTotalHits(), equalTo(3L));
    assertThat(hits.getHits().length, equalTo(1));
    assertThat(hits.getAt(0).getId(), equalTo("9"));
    bucket = bucketIterator.next();
    assertThat(key(bucket), equalTo("a"));
    topHits = bucket.getAggregations().get("hits");
    hits = topHits.getHits();
    assertThat(hits.getTotalHits(), equalTo(2L));
    assertThat(hits.getHits().length, equalTo(1));
    assertThat(hits.getAt(0).getId(), equalTo("2"));
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

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