use of org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter in project elasticsearch by elastic.
the class FiltersTests method testFiltersSortedByKey.
/**
* Test that when passing in keyed filters as list or array, the list stored internally is sorted by key
* Also check the list passed in is not modified by this but rather copied
*/
public void testFiltersSortedByKey() {
KeyedFilter[] original = new KeyedFilter[] { new KeyedFilter("bbb", new MatchNoneQueryBuilder()), new KeyedFilter("aaa", new MatchNoneQueryBuilder()) };
FiltersAggregationBuilder builder;
builder = new FiltersAggregationBuilder("my-agg", original);
assertEquals("aaa", builder.filters().get(0).key());
assertEquals("bbb", builder.filters().get(1).key());
// original should be unchanged
assertEquals("bbb", original[0].key());
assertEquals("aaa", original[1].key());
}
use of org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter in project elasticsearch by elastic.
the class FiltersTests method createTestAggregatorBuilder.
@Override
protected FiltersAggregationBuilder createTestAggregatorBuilder() {
int size = randomIntBetween(1, 20);
FiltersAggregationBuilder factory;
if (randomBoolean()) {
KeyedFilter[] filters = new KeyedFilter[size];
int i = 0;
for (String key : randomUnique(() -> randomAsciiOfLengthBetween(1, 20), size)) {
filters[i++] = new KeyedFilter(key, QueryBuilders.termQuery(randomAsciiOfLengthBetween(5, 20), randomAsciiOfLengthBetween(5, 20)));
}
factory = new FiltersAggregationBuilder(randomAsciiOfLengthBetween(1, 20), filters);
} else {
QueryBuilder[] filters = new QueryBuilder[size];
for (int i = 0; i < size; i++) {
filters[i] = QueryBuilders.termQuery(randomAsciiOfLengthBetween(5, 20), randomAsciiOfLengthBetween(5, 20));
}
factory = new FiltersAggregationBuilder(randomAsciiOfLengthBetween(1, 20), filters);
}
if (randomBoolean()) {
factory.otherBucket(randomBoolean());
}
if (randomBoolean()) {
factory.otherBucketKey(randomAsciiOfLengthBetween(1, 20));
}
return factory;
}
use of org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter in project elasticsearch by elastic.
the class FiltersIT method testOtherNamedBucket.
public void testOtherNamedBucket() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("tag1", termQuery("tag", "tag1")), new KeyedFilter("tag2", termQuery("tag", "tag2")))).otherBucket(true).otherBucketKey("foobar")).execute().actionGet();
assertSearchResponse(response);
Filters filters = response.getAggregations().get("tags");
assertThat(filters, notNullValue());
assertThat(filters.getName(), equalTo("tags"));
assertThat(filters.getBuckets().size(), equalTo(3));
Filters.Bucket bucket = filters.getBucketByKey("tag1");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
bucket = filters.getBucketByKey("tag2");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo((long) numTag2Docs));
bucket = filters.getBucketByKey("foobar");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo((long) numOtherDocs));
}
use of org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter in project elasticsearch by elastic.
the class FiltersIT method testEmptyAggregationWithOtherBucket.
public void testEmptyAggregationWithOtherBucket() throws Exception {
SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx").setQuery(matchAllQuery()).addAggregation(histogram("histo").field("value").interval(1L).minDocCount(0).subAggregation(filters("filters", new KeyedFilter("foo", matchAllQuery())).otherBucket(true).otherBucketKey("bar"))).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());
Filters filters = bucket.getAggregations().get("filters");
assertThat(filters, notNullValue());
Filters.Bucket other = filters.getBucketByKey("bar");
assertThat(other, Matchers.notNullValue());
assertThat(other.getKeyAsString(), equalTo("bar"));
assertThat(other.getDocCount(), is(0L));
}
use of org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter in project elasticsearch by elastic.
the class FiltersIT method testOtherWithSubAggregation.
public void testOtherWithSubAggregation() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("tag1", termQuery("tag", "tag1")), new KeyedFilter("tag2", termQuery("tag", "tag2")))).otherBucket(true).subAggregation(avg("avg_value").field("value"))).execute().actionGet();
assertSearchResponse(response);
Filters filters = response.getAggregations().get("tags");
assertThat(filters, notNullValue());
assertThat(filters.getName(), equalTo("tags"));
assertThat(filters.getBuckets().size(), equalTo(3));
assertThat(filters.getProperty("_bucket_count"), equalTo(3));
Object[] propertiesKeys = (Object[]) filters.getProperty("_key");
Object[] propertiesDocCounts = (Object[]) filters.getProperty("_count");
Object[] propertiesCounts = (Object[]) filters.getProperty("avg_value.value");
Filters.Bucket bucket = filters.getBucketByKey("tag1");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
long sum = 0;
for (int i = 0; i < numTag1Docs; ++i) {
sum += i + 1;
}
assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
Avg avgValue = bucket.getAggregations().get("avg_value");
assertThat(avgValue, notNullValue());
assertThat(avgValue.getName(), equalTo("avg_value"));
assertThat(avgValue.getValue(), equalTo((double) sum / numTag1Docs));
assertThat(propertiesKeys[0], equalTo("tag1"));
assertThat(propertiesDocCounts[0], equalTo((long) numTag1Docs));
assertThat(propertiesCounts[0], equalTo((double) sum / numTag1Docs));
bucket = filters.getBucketByKey("tag2");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo((long) numTag2Docs));
sum = 0;
for (int i = numTag1Docs; i < (numTag1Docs + numTag2Docs); ++i) {
sum += i;
}
assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
avgValue = bucket.getAggregations().get("avg_value");
assertThat(avgValue, notNullValue());
assertThat(avgValue.getName(), equalTo("avg_value"));
assertThat(avgValue.getValue(), equalTo((double) sum / numTag2Docs));
assertThat(propertiesKeys[1], equalTo("tag2"));
assertThat(propertiesDocCounts[1], equalTo((long) numTag2Docs));
assertThat(propertiesCounts[1], equalTo((double) sum / numTag2Docs));
bucket = filters.getBucketByKey("_other_");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo((long) numOtherDocs));
sum = 0;
for (int i = numTag1Docs + numTag2Docs; i < numDocs; ++i) {
sum += i;
}
assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
avgValue = bucket.getAggregations().get("avg_value");
assertThat(avgValue, notNullValue());
assertThat(avgValue.getName(), equalTo("avg_value"));
assertThat(avgValue.getValue(), equalTo((double) sum / numOtherDocs));
assertThat(propertiesKeys[2], equalTo("_other_"));
assertThat(propertiesDocCounts[2], equalTo((long) numOtherDocs));
assertThat(propertiesCounts[2], equalTo((double) sum / numOtherDocs));
}
Aggregations