Search in sources :

Example 1 with InternalFilters

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);
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) InternalFilters(org.opensearch.search.aggregations.bucket.filter.InternalFilters) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 2 with InternalFilters

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());
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) FiltersAggregator(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator) HashMap(java.util.HashMap) SearchResponse(org.opensearch.action.search.SearchResponse) InternalFilters(org.opensearch.search.aggregations.bucket.filter.InternalFilters) XContentType(org.opensearch.common.xcontent.XContentType) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 3 with InternalFilters

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);
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) InternalFilters(org.opensearch.search.aggregations.bucket.filter.InternalFilters) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

InternalFilters (org.opensearch.search.aggregations.bucket.filter.InternalFilters)3 IndexReader (org.apache.lucene.index.IndexReader)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 Directory (org.apache.lucene.store.Directory)2 FiltersAggregationBuilder (org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder)2 HashMap (java.util.HashMap)1 Document (org.apache.lucene.document.Document)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 BytesReference (org.opensearch.common.bytes.BytesReference)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1 XContentType (org.opensearch.common.xcontent.XContentType)1 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)1 MatchNoneQueryBuilder (org.opensearch.index.query.MatchNoneQueryBuilder)1 QueryBuilder (org.opensearch.index.query.QueryBuilder)1 WrapperQueryBuilder (org.opensearch.index.query.WrapperQueryBuilder)1 FiltersAggregator (org.opensearch.search.aggregations.bucket.filter.FiltersAggregator)1