use of org.opensearch.search.aggregations.bucket.filter.InternalFilters 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);
}
}
}
use of org.opensearch.search.aggregations.bucket.filter.InternalFilters in project OpenSearch by opensearch-project.
the class FiltersAggsRewriteIT method testWrapperQueryIsRewritten.
public void testWrapperQueryIsRewritten() throws IOException {
createIndex("test", Settings.EMPTY, "test", "title", "type=text");
client().prepareIndex("test").setId("1").setSource("title", "foo bar baz").get();
client().prepareIndex("test").setId("2").setSource("title", "foo foo foo").get();
client().prepareIndex("test").setId("3").setSource("title", "bar baz bax").get();
client().admin().indices().prepareRefresh("test").get();
XContentType xContentType = randomFrom(XContentType.values());
BytesReference bytesReference;
try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType)) {
builder.startObject();
{
builder.startObject("terms");
{
builder.array("title", "foo");
}
builder.endObject();
}
builder.endObject();
bytesReference = BytesReference.bytes(builder);
}
FiltersAggregationBuilder builder = new FiltersAggregationBuilder("titles", new FiltersAggregator.KeyedFilter("titleterms", new WrapperQueryBuilder(bytesReference)));
Map<String, Object> metadata = new HashMap<>();
metadata.put(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20));
builder.setMetadata(metadata);
SearchResponse searchResponse = client().prepareSearch("test").setSize(0).addAggregation(builder).get();
assertEquals(3, searchResponse.getHits().getTotalHits().value);
InternalFilters filters = searchResponse.getAggregations().get("titles");
assertEquals(1, filters.getBuckets().size());
assertEquals(2, filters.getBuckets().get(0).getDocCount());
assertEquals(metadata, filters.getMetadata());
}
use of org.opensearch.search.aggregations.bucket.filter.InternalFilters in project OpenSearch by opensearch-project.
the class BucketScriptAggregatorTests method testCase.
private void testCase(FiltersAggregationBuilder aggregationBuilder, Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalFilters> verify, MappedFieldType... fieldType) throws IOException {
try (Directory directory = newDirectory()) {
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
buildIndex.accept(indexWriter);
indexWriter.close();
try (IndexReader indexReader = DirectoryReader.open(directory)) {
IndexSearcher indexSearcher = newIndexSearcher(indexReader);
InternalFilters filters;
filters = searchAndReduce(indexSearcher, query, aggregationBuilder, fieldType);
verify.accept(filters);
}
}
}
Aggregations