use of org.elasticsearch.search.aggregations.bucket.filter.Filter in project elasticsearch by elastic.
the class StringTermsIT method testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevelsSpecialChars.
public void testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevelsSpecialChars() throws Exception {
StringBuilder filter2NameBuilder = new StringBuilder("filt.er2");
filter2NameBuilder.append(randomAsciiOfLengthBetween(3, 10).replace("[", "").replace("]", "").replace(">", ""));
String filter2Name = filter2NameBuilder.toString();
StringBuilder statsNameBuilder = new StringBuilder("st.ats");
statsNameBuilder.append(randomAsciiOfLengthBetween(3, 10).replace("[", "").replace("]", "").replace(">", ""));
String statsName = statsNameBuilder.toString();
boolean asc = randomBoolean();
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("tags").executionHint(randomExecutionHint()).field("tag").collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("filter1>" + filter2Name + ">" + statsName + ".max", asc)).subAggregation(filter("filter1", QueryBuilders.matchAllQuery()).subAggregation(filter(filter2Name, QueryBuilders.matchAllQuery()).subAggregation(stats(statsName).field("i"))))).execute().actionGet();
assertSearchResponse(response);
Terms tags = response.getAggregations().get("tags");
assertThat(tags, notNullValue());
assertThat(tags.getName(), equalTo("tags"));
assertThat(tags.getBuckets().size(), equalTo(2));
Iterator<Terms.Bucket> iters = tags.getBuckets().iterator();
// the max for "more" is 2
// the max for "less" is 4
Terms.Bucket tag = iters.next();
assertThat(tag, notNullValue());
assertThat(key(tag), equalTo(asc ? "more" : "less"));
assertThat(tag.getDocCount(), equalTo(asc ? 3L : 2L));
Filter filter1 = tag.getAggregations().get("filter1");
assertThat(filter1, notNullValue());
assertThat(filter1.getDocCount(), equalTo(asc ? 3L : 2L));
Filter filter2 = filter1.getAggregations().get(filter2Name);
assertThat(filter2, notNullValue());
assertThat(filter2.getDocCount(), equalTo(asc ? 3L : 2L));
Stats stats = filter2.getAggregations().get(statsName);
assertThat(stats, notNullValue());
assertThat(stats.getMax(), equalTo(asc ? 2.0 : 4.0));
tag = iters.next();
assertThat(tag, notNullValue());
assertThat(key(tag), equalTo(asc ? "less" : "more"));
assertThat(tag.getDocCount(), equalTo(asc ? 2L : 3L));
filter1 = tag.getAggregations().get("filter1");
assertThat(filter1, notNullValue());
assertThat(filter1.getDocCount(), equalTo(asc ? 2L : 3L));
filter2 = filter1.getAggregations().get(filter2Name);
assertThat(filter2, notNullValue());
assertThat(filter2.getDocCount(), equalTo(asc ? 2L : 3L));
stats = filter2.getAggregations().get(statsName);
assertThat(stats, notNullValue());
assertThat(stats.getMax(), equalTo(asc ? 4.0 : 2.0));
}
use of org.elasticsearch.search.aggregations.bucket.filter.Filter in project elasticsearch by elastic.
the class StringTermsIT method testStringTermsNestedIntoPerBucketAggregator.
public void testStringTermsNestedIntoPerBucketAggregator() throws Exception {
// no execution hint so that the logic that decides whether or not to use ordinals is executed
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(filter("filter", termQuery(MULTI_VALUED_FIELD_NAME, "val3")).subAggregation(terms("terms").field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())))).execute().actionGet();
assertThat(response.getFailedShards(), equalTo(0));
Filter filter = response.getAggregations().get("filter");
Terms terms = filter.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(3));
for (int i = 2; i <= 4; i++) {
Terms.Bucket bucket = terms.getBucketByKey("val" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("val" + i));
assertThat(bucket.getDocCount(), equalTo(i == 3 ? 2L : 1L));
}
}
use of org.elasticsearch.search.aggregations.bucket.filter.Filter in project elasticsearch by elastic.
the class FilterIT method testEmptyAggregation.
public void testEmptyAggregation() throws Exception {
SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx").setQuery(matchAllQuery()).addAggregation(histogram("histo").field("value").interval(1L).minDocCount(0).subAggregation(filter("filter", matchAllQuery()))).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
Histogram histo = searchResponse.getAggregations().get("histo");
assertThat(histo, Matchers.notNullValue());
Histogram.Bucket bucket = histo.getBuckets().get(1);
assertThat(bucket, Matchers.notNullValue());
Filter filter = bucket.getAggregations().get("filter");
assertThat(filter, Matchers.notNullValue());
assertThat(filter.getName(), equalTo("filter"));
assertThat(filter.getDocCount(), is(0L));
}
use of org.elasticsearch.search.aggregations.bucket.filter.Filter in project elasticsearch by elastic.
the class FilterIT method testEmptyFilterDeclarations.
// See NullPointer issue when filters are empty:
// https://github.com/elastic/elasticsearch/issues/8438
public void testEmptyFilterDeclarations() throws Exception {
QueryBuilder emptyFilter = new BoolQueryBuilder();
SearchResponse response = client().prepareSearch("idx").addAggregation(filter("tag1", emptyFilter)).execute().actionGet();
assertSearchResponse(response);
Filter filter = response.getAggregations().get("tag1");
assertThat(filter, notNullValue());
assertThat(filter.getDocCount(), equalTo((long) numDocs));
}
use of org.elasticsearch.search.aggregations.bucket.filter.Filter in project elasticsearch by elastic.
the class GeoHashGridIT method testFiltered.
public void testFiltered() throws Exception {
GeoBoundingBoxQueryBuilder bbox = new GeoBoundingBoxQueryBuilder("location");
bbox.setCorners(smallestGeoHash).queryName("bbox");
for (int precision = 1; precision <= PRECISION; precision++) {
SearchResponse response = client().prepareSearch("idx").addAggregation(AggregationBuilders.filter("filtered", bbox).subAggregation(geohashGrid("geohashgrid").field("location").precision(precision))).execute().actionGet();
assertSearchResponse(response);
Filter filter = response.getAggregations().get("filtered");
GeoHashGrid geoGrid = filter.getAggregations().get("geohashgrid");
for (GeoHashGrid.Bucket cell : geoGrid.getBuckets()) {
String geohash = cell.getKeyAsString();
long bucketCount = cell.getDocCount();
int expectedBucketCount = expectedDocCountsForGeoHash.get(geohash);
assertNotSame(bucketCount, 0);
assertTrue("Buckets must be filtered", geohash.startsWith(smallestGeoHash));
assertEquals("Geohash " + geohash + " has wrong doc count ", expectedBucketCount, bucketCount);
}
}
}
Aggregations