use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter in project OpenSearch by opensearch-project.
the class FiltersTests method testRewrite.
public void testRewrite() throws IOException {
// test non-keyed filter that doesn't rewrite
AggregationBuilder original = new FiltersAggregationBuilder("my-agg", new MatchAllQueryBuilder());
original.setMetadata(Collections.singletonMap(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)));
AggregationBuilder rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertSame(original, rewritten);
// test non-keyed filter that does rewrite
original = new FiltersAggregationBuilder("my-agg", new BoolQueryBuilder());
rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertNotSame(original, rewritten);
assertThat(rewritten, instanceOf(FiltersAggregationBuilder.class));
assertEquals("my-agg", ((FiltersAggregationBuilder) rewritten).getName());
assertEquals(1, ((FiltersAggregationBuilder) rewritten).filters().size());
assertEquals("0", ((FiltersAggregationBuilder) rewritten).filters().get(0).key());
assertThat(((FiltersAggregationBuilder) rewritten).filters().get(0).filter(), instanceOf(MatchAllQueryBuilder.class));
assertFalse(((FiltersAggregationBuilder) rewritten).isKeyed());
// test keyed filter that doesn't rewrite
original = new FiltersAggregationBuilder("my-agg", new KeyedFilter("my-filter", new MatchAllQueryBuilder()));
rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertSame(original, rewritten);
// test non-keyed filter that does rewrite
original = new FiltersAggregationBuilder("my-agg", new KeyedFilter("my-filter", new BoolQueryBuilder()));
rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertNotSame(original, rewritten);
assertThat(rewritten, instanceOf(FiltersAggregationBuilder.class));
assertEquals("my-agg", ((FiltersAggregationBuilder) rewritten).getName());
assertEquals(1, ((FiltersAggregationBuilder) rewritten).filters().size());
assertEquals("my-filter", ((FiltersAggregationBuilder) rewritten).filters().get(0).key());
assertThat(((FiltersAggregationBuilder) rewritten).filters().get(0).filter(), instanceOf(MatchAllQueryBuilder.class));
assertTrue(((FiltersAggregationBuilder) rewritten).isKeyed());
// test sub-agg filter that does rewrite
original = new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.BOOLEAN).subAggregation(new FiltersAggregationBuilder("my-agg", new KeyedFilter("my-filter", new BoolQueryBuilder())));
rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertNotSame(original, rewritten);
assertNotEquals(original, rewritten);
assertThat(rewritten, instanceOf(TermsAggregationBuilder.class));
assertThat(rewritten.getSubAggregations().size(), equalTo(1));
AggregationBuilder subAgg = rewritten.getSubAggregations().iterator().next();
assertThat(subAgg, instanceOf(FiltersAggregationBuilder.class));
assertNotSame(original.getSubAggregations().iterator().next(), subAgg);
assertEquals("my-agg", subAgg.getName());
assertSame(rewritten, rewritten.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L)));
}
use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter in project OpenSearch by opensearch-project.
the class FiltersIT method testWithContextBasedSubAggregation.
public void testWithContextBasedSubAggregation() throws Exception {
try {
client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("tag1", termQuery("tag", "tag1")), new KeyedFilter("tag2", termQuery("tag", "tag2")))).subAggregation(avg("avg_value"))).get();
fail("expected execution to fail - an attempt to have a context based numeric sub-aggregation, but there is not value source" + "context which the sub-aggregation can inherit");
} catch (OpenSearchException e) {
assertThat(e.getMessage(), is("all shards failed"));
}
}
use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter in project OpenSearch by opensearch-project.
the class FiltersIT method testOtherBucket.
public void testOtherBucket() 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)).get();
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("_other_");
assertThat(bucket, Matchers.notNullValue());
assertThat(bucket.getDocCount(), equalTo((long) numOtherDocs));
}
use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter in project OpenSearch by opensearch-project.
the class FiltersIT method testSimple.
public void testSimple() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("tag1", termQuery("tag", "tag1")), new KeyedFilter("tag2", termQuery("tag", "tag2"))))).get();
assertSearchResponse(response);
Filters filters = response.getAggregations().get("tags");
assertThat(filters, notNullValue());
assertThat(filters.getName(), equalTo("tags"));
assertThat(filters.getBuckets().size(), equalTo(2));
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));
}
use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter in project OpenSearch by opensearch-project.
the class FiltersAggregationBuilder method parse.
public static FiltersAggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
List<FiltersAggregator.KeyedFilter> filters = new ArrayList<>();
XContentParser.Token token;
String currentFieldName = null;
String otherBucketKey = null;
Boolean otherBucket = null;
boolean keyed = false;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_BOOLEAN) {
if (OTHER_BUCKET_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
otherBucket = parser.booleanValue();
} else {
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else if (token == XContentParser.Token.VALUE_STRING) {
if (OTHER_BUCKET_KEY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
otherBucketKey = parser.text();
} else {
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else if (token == XContentParser.Token.START_OBJECT) {
if (FILTERS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
String key = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
key = parser.currentName();
} else {
QueryBuilder filter = parseInnerQueryBuilder(parser);
filters.add(new FiltersAggregator.KeyedFilter(key, filter));
}
}
keyed = true;
} else {
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else if (token == XContentParser.Token.START_ARRAY) {
if (FILTERS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
List<QueryBuilder> builders = new ArrayList<>();
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
QueryBuilder filter = parseInnerQueryBuilder(parser);
builders.add(filter);
}
for (int i = 0; i < builders.size(); i++) {
filters.add(new KeyedFilter(String.valueOf(i), builders.get(i)));
}
} else {
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
} else {
throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
}
}
if (filters.isEmpty()) {
throw new IllegalArgumentException("[" + FILTERS_FIELD + "] cannot be empty.");
}
FiltersAggregationBuilder factory = new FiltersAggregationBuilder(aggregationName, filters, keyed);
if (otherBucket == null && otherBucketKey != null) {
// automatically enable the other bucket if a key is set, as per the doc
otherBucket = true;
}
if (otherBucket != null) {
factory.otherBucket(otherBucket);
}
if (otherBucketKey != null) {
factory.otherBucketKey(otherBucketKey);
}
return factory;
}
Aggregations