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));
}
}
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));
}
}
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));
}
}
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]));
}
}
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"));
}
Aggregations