Search in sources :

Example 1 with FiltersAggregationBuilder

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;
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder)

Example 2 with FiltersAggregationBuilder

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)));
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) QueryRewriteContext(org.opensearch.index.query.QueryRewriteContext) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 3 with FiltersAggregationBuilder

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());
}
Also used : FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) QueryRewriteContext(org.opensearch.index.query.QueryRewriteContext)

Example 4 with FiltersAggregationBuilder

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());
    }
}
Also used : FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 5 with FiltersAggregationBuilder

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);
        }
    }
}
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)

Aggregations

FiltersAggregationBuilder (org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder)11 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)5 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)4 MatchNoneQueryBuilder (org.opensearch.index.query.MatchNoneQueryBuilder)4 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)4 KeyedFilter (org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter)4 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 Document (org.apache.lucene.document.Document)2 SearchResponse (org.opensearch.action.search.SearchResponse)2 QueryBuilder (org.opensearch.index.query.QueryBuilder)2 QueryRewriteContext (org.opensearch.index.query.QueryRewriteContext)2 RangeQueryBuilder (org.opensearch.index.query.RangeQueryBuilder)2 TermQueryBuilder (org.opensearch.index.query.TermQueryBuilder)2 WrapperQueryBuilder (org.opensearch.index.query.WrapperQueryBuilder)2 CompositeAggregationBuilder (org.opensearch.search.aggregations.bucket.composite.CompositeAggregationBuilder)2 HistogramAggregationBuilder (org.opensearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder)2 SumAggregationBuilder (org.opensearch.search.aggregations.metrics.SumAggregationBuilder)2 IOException (java.io.IOException)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1