use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder in project OpenSearch by opensearch-project.
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(() -> randomAlphaOfLengthBetween(1, 20), size)) {
filters[i++] = new KeyedFilter(key, QueryBuilders.termQuery(randomAlphaOfLengthBetween(5, 20), randomAlphaOfLengthBetween(5, 20)));
}
factory = new FiltersAggregationBuilder(randomAlphaOfLengthBetween(1, 20), filters);
} else {
QueryBuilder[] filters = new QueryBuilder[size];
for (int i = 0; i < size; i++) {
filters[i] = QueryBuilders.termQuery(randomAlphaOfLengthBetween(5, 20), randomAlphaOfLengthBetween(5, 20));
}
factory = new FiltersAggregationBuilder(randomAlphaOfLengthBetween(1, 20), filters);
}
if (randomBoolean()) {
factory.otherBucket(randomBoolean());
}
if (randomBoolean()) {
factory.otherBucketKey(randomAlphaOfLengthBetween(1, 20));
}
return factory;
}
use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder 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.FiltersAggregationBuilder in project OpenSearch by opensearch-project.
the class FiltersTests method testRewritePreservesOtherBucket.
public void testRewritePreservesOtherBucket() throws IOException {
FiltersAggregationBuilder originalFilters = new FiltersAggregationBuilder("my-agg", new BoolQueryBuilder());
originalFilters.otherBucket(randomBoolean());
originalFilters.otherBucketKey(randomAlphaOfLength(10));
AggregationBuilder rewritten = originalFilters.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
assertThat(rewritten, instanceOf(FiltersAggregationBuilder.class));
FiltersAggregationBuilder rewrittenFilters = (FiltersAggregationBuilder) rewritten;
assertEquals(originalFilters.otherBucket(), rewrittenFilters.otherBucket());
assertEquals(originalFilters.otherBucketKey(), rewrittenFilters.otherBucketKey());
}
use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder in project OpenSearch by opensearch-project.
the class FiltersTests method testOtherBucket.
public void testOtherBucket() throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
builder.startObject();
builder.startArray("filters").startObject().startObject("term").field("field", "foo").endObject().endObject().endArray();
builder.endObject();
try (XContentParser parser = createParser(shuffleXContent(builder))) {
parser.nextToken();
FiltersAggregationBuilder filters = FiltersAggregationBuilder.parse("agg_name", parser);
// The other bucket is disabled by default
assertFalse(filters.otherBucket());
builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
builder.startObject();
builder.startArray("filters").startObject().startObject("term").field("field", "foo").endObject().endObject().endArray();
builder.field("other_bucket_key", "some_key");
builder.endObject();
}
try (XContentParser parser = createParser(shuffleXContent(builder))) {
parser.nextToken();
FiltersAggregationBuilder filters = FiltersAggregationBuilder.parse("agg_name", parser);
// but setting a key enables it automatically
assertTrue(filters.otherBucket());
builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
builder.startObject();
builder.startArray("filters").startObject().startObject("term").field("field", "foo").endObject().endObject().endArray();
builder.field("other_bucket", false);
builder.field("other_bucket_key", "some_key");
builder.endObject();
}
try (XContentParser parser = createParser(shuffleXContent(builder))) {
parser.nextToken();
FiltersAggregationBuilder filters = FiltersAggregationBuilder.parse("agg_name", parser);
// unless the other bucket is explicitly disabled
assertFalse(filters.otherBucket());
}
}
use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder in project OpenSearch by opensearch-project.
the class SamplerAggregatorTests method testEmptyParentBucket.
/**
* Tests that the sampler aggregation works correctly if the parent bucket does not contain any hit.
*/
public void testEmptyParentBucket() throws Exception {
IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
try (Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, indexWriterConfig)) {
writer.addDocument(new Document());
try (IndexReader reader = DirectoryReader.open(writer)) {
IndexSearcher searcher = new IndexSearcher(reader);
QueryBuilder[] filters = new QueryBuilder[] { new MatchAllQueryBuilder(), new MatchNoneQueryBuilder() };
FiltersAggregationBuilder samplerParent = new FiltersAggregationBuilder("filters", filters);
TermsAggregationBuilder samplerChild = new TermsAggregationBuilder("child").field("field");
SamplerAggregationBuilder sampler = new SamplerAggregationBuilder("sampler").subAggregation(samplerChild);
samplerParent.subAggregation(sampler);
InternalFilters response = searchAndReduce(searcher, new MatchAllDocsQuery(), samplerParent);
assertEquals(response.getBuckets().size(), 2);
assertEquals(response.getBuckets().get(0).getDocCount(), 1);
assertEquals(response.getBuckets().get(1).getDocCount(), 0);
}
}
}
Aggregations