Search in sources :

Example 1 with AggregationBuilder

use of org.opensearch.search.aggregations.AggregationBuilder in project OpenSearch by opensearch-project.

the class SignificantTermsSignificanceScoreIT method testSubAggregations.

/**
 * A simple test that adds a sub-aggregation to a significant terms aggregation,
 * to help check that sub-aggregation collection is handled correctly.
 */
public void testSubAggregations() throws Exception {
    indexEqualTestData();
    QueryBuilder query = QueryBuilders.termsQuery(TEXT_FIELD, "a", "b");
    AggregationBuilder subAgg = terms("class").field(CLASS_FIELD);
    AggregationBuilder agg = significantTerms("significant_terms").field(TEXT_FIELD).executionHint(randomExecutionHint()).significanceHeuristic(new ChiSquare(true, true)).minDocCount(1).shardSize(1000).size(1000).subAggregation(subAgg);
    SearchResponse response = client().prepareSearch("test").setQuery(query).addAggregation(agg).get();
    assertSearchResponse(response);
    SignificantTerms sigTerms = response.getAggregations().get("significant_terms");
    assertThat(sigTerms.getBuckets().size(), equalTo(2));
    for (SignificantTerms.Bucket bucket : sigTerms) {
        StringTerms terms = bucket.getAggregations().get("class");
        assertThat(terms.getBuckets().size(), equalTo(2));
    }
}
Also used : SignificantTerms(org.opensearch.search.aggregations.bucket.terms.SignificantTerms) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) ChiSquare(org.opensearch.search.aggregations.bucket.terms.heuristic.ChiSquare) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) QueryBuilder(org.opensearch.index.query.QueryBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with AggregationBuilder

use of org.opensearch.search.aggregations.AggregationBuilder 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 AggregationBuilder

use of org.opensearch.search.aggregations.AggregationBuilder 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 AggregationBuilder

use of org.opensearch.search.aggregations.AggregationBuilder in project OpenSearch by opensearch-project.

the class AutoDateHistogramAggregatorTests method testAsSubAggInManyBuckets.

public void testAsSubAggInManyBuckets() throws IOException {
    CheckedBiConsumer<RandomIndexWriter, DateFieldMapper.DateFieldType, IOException> buildIndex = (iw, dft) -> {
        long start = dft.parse("2020-01-01T00:00:00Z");
        long end = dft.parse("2021-01-01T00:00:00Z");
        long anHour = dft.resolution().convert(Instant.ofEpochSecond(TimeUnit.HOURS.toSeconds(1)));
        List<List<IndexableField>> docs = new ArrayList<>();
        int n = 0;
        for (long d = start; d < end; d += anHour) {
            docs.add(org.opensearch.common.collect.List.of(new SortedNumericDocValuesField(AGGREGABLE_DATE, d), new SortedNumericDocValuesField("n", n % 100)));
            n++;
        }
        /*
             * Intentionally add all documents at once to put them on the
             * same shard to make the reduce behavior consistent.
             */
        iw.addDocuments(docs);
    };
    AggregationBuilder builder = new HistogramAggregationBuilder("n").field("n").interval(1).subAggregation(new AutoDateHistogramAggregationBuilder("dh").field(AGGREGABLE_DATE).setNumBuckets(4).subAggregation(new MaxAggregationBuilder("max").field("n")));
    asSubAggTestCase(builder, buildIndex, (InternalHistogram histo) -> {
        assertThat(histo.getBuckets(), hasSize(100));
        for (int n = 0; n < 100; n++) {
            InternalHistogram.Bucket b = histo.getBuckets().get(n);
            InternalAutoDateHistogram dh = b.getAggregations().get("dh");
            assertThat(bucketCountsAsMap(dh), hasEntry(equalTo("2020-01-01T00:00:00.000Z"), either(equalTo(21)).or(equalTo(22))));
            assertThat(bucketCountsAsMap(dh), hasEntry(equalTo("2020-04-01T00:00:00.000Z"), either(equalTo(21)).or(equalTo(22))));
            assertThat(bucketCountsAsMap(dh), hasEntry(equalTo("2020-07-01T00:00:00.000Z"), either(equalTo(22)).or(equalTo(23))));
            assertThat(bucketCountsAsMap(dh), hasEntry(equalTo("2020-10-01T00:00:00.000Z"), either(equalTo(22)).or(equalTo(23))));
            Map<String, Double> expectedMax = new TreeMap<>();
            expectedMax.put("2020-01-01T00:00:00.000Z", (double) n);
            expectedMax.put("2020-04-01T00:00:00.000Z", (double) n);
            expectedMax.put("2020-07-01T00:00:00.000Z", (double) n);
            expectedMax.put("2020-10-01T00:00:00.000Z", (double) n);
            assertThat(maxAsMap(dh), equalTo(expectedMax));
        }
    });
}
Also used : Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) Aggregation(org.opensearch.search.aggregations.Aggregation) Matchers.either(org.hamcrest.Matchers.either) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexableField(org.apache.lucene.index.IndexableField) ZonedDateTime(java.time.ZonedDateTime) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Version(org.opensearch.Version) Document(org.apache.lucene.document.Document) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) Locale(java.util.Locale) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) ZoneOffset(java.time.ZoneOffset) AggregationInspectionHelper(org.opensearch.search.aggregations.support.AggregationInspectionHelper) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DirectoryReader(org.apache.lucene.index.DirectoryReader) DerivativePipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.DerivativePipelineAggregationBuilder) Settings(org.opensearch.common.settings.Settings) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Instant(java.time.Instant) List(java.util.List) LocalDate(java.time.LocalDate) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) InternalStats(org.opensearch.search.aggregations.metrics.InternalStats) MultiBucketConsumerService(org.opensearch.search.aggregations.MultiBucketConsumerService) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) InternalSimpleValue(org.opensearch.search.aggregations.pipeline.InternalSimpleValue) LongPoint(org.apache.lucene.document.LongPoint) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) CheckedBiConsumer(org.opensearch.common.CheckedBiConsumer) HashMap(java.util.HashMap) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) TreeMap(java.util.TreeMap) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) YearMonth(java.time.YearMonth) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Collections(java.util.Collections) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) DerivativePipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.DerivativePipelineAggregationBuilder) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IOException(java.io.IOException) TreeMap(java.util.TreeMap) LongPoint(org.apache.lucene.document.LongPoint) IndexableField(org.apache.lucene.index.IndexableField) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) List(java.util.List) ArrayList(java.util.ArrayList) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 5 with AggregationBuilder

use of org.opensearch.search.aggregations.AggregationBuilder in project OpenSearch by opensearch-project.

the class AutoDateHistogramAggregatorTests method testAsSubAgg.

public void testAsSubAgg() throws IOException {
    AggregationBuilder builder = new TermsAggregationBuilder("k1").field("k1").subAggregation(new AutoDateHistogramAggregationBuilder("dh").field(AGGREGABLE_DATE).setNumBuckets(3).subAggregation(new MaxAggregationBuilder("max").field("n")));
    asSubAggTestCase(builder, (StringTerms terms) -> {
        StringTerms.Bucket a = terms.getBucketByKey("a");
        InternalAutoDateHistogram adh = a.getAggregations().get("dh");
        Map<String, Integer> expectedDocCount = new TreeMap<>();
        expectedDocCount.put("2020-01-01T00:00:00.000Z", 2);
        expectedDocCount.put("2021-01-01T00:00:00.000Z", 2);
        assertThat(bucketCountsAsMap(adh), equalTo(expectedDocCount));
        Map<String, Double> expectedMax = new TreeMap<>();
        expectedMax.put("2020-01-01T00:00:00.000Z", 2.0);
        expectedMax.put("2021-01-01T00:00:00.000Z", 4.0);
        assertThat(maxAsMap(adh), equalTo(expectedMax));
        StringTerms.Bucket b = terms.getBucketByKey("b");
        InternalAutoDateHistogram bdh = b.getAggregations().get("dh");
        expectedDocCount.clear();
        expectedDocCount.put("2020-02-01T00:00:00.000Z", 1);
        assertThat(bucketCountsAsMap(bdh), equalTo(expectedDocCount));
        expectedMax.clear();
        expectedMax.put("2020-02-01T00:00:00.000Z", 5.0);
        assertThat(maxAsMap(bdh), equalTo(expectedMax));
    });
    builder = new TermsAggregationBuilder("k2").field("k2").subAggregation(builder);
    asSubAggTestCase(builder, (StringTerms terms) -> {
        StringTerms.Bucket a = terms.getBucketByKey("a");
        StringTerms ak1 = a.getAggregations().get("k1");
        StringTerms.Bucket ak1a = ak1.getBucketByKey("a");
        InternalAutoDateHistogram ak1adh = ak1a.getAggregations().get("dh");
        Map<String, Integer> expectedDocCount = new TreeMap<>();
        expectedDocCount.put("2020-01-01T00:00:00.000Z", 2);
        expectedDocCount.put("2021-01-01T00:00:00.000Z", 1);
        assertThat(bucketCountsAsMap(ak1adh), equalTo(expectedDocCount));
        Map<String, Double> expectedMax = new TreeMap<>();
        expectedMax.put("2020-01-01T00:00:00.000Z", 2.0);
        expectedMax.put("2021-01-01T00:00:00.000Z", 3.0);
        assertThat(maxAsMap(ak1adh), equalTo(expectedMax));
        StringTerms.Bucket b = terms.getBucketByKey("b");
        StringTerms bk1 = b.getAggregations().get("k1");
        StringTerms.Bucket bk1a = bk1.getBucketByKey("a");
        InternalAutoDateHistogram bk1adh = bk1a.getAggregations().get("dh");
        expectedDocCount.clear();
        expectedDocCount.put("2021-03-01T00:00:00.000Z", 1);
        assertThat(bucketCountsAsMap(bk1adh), equalTo(expectedDocCount));
        expectedMax.clear();
        expectedMax.put("2021-03-01T00:00:00.000Z", 4.0);
        assertThat(maxAsMap(bk1adh), equalTo(expectedMax));
        StringTerms.Bucket bk1b = bk1.getBucketByKey("b");
        InternalAutoDateHistogram bk1bdh = bk1b.getAggregations().get("dh");
        expectedDocCount.clear();
        expectedDocCount.put("2020-02-01T00:00:00.000Z", 1);
        assertThat(bucketCountsAsMap(bk1bdh), equalTo(expectedDocCount));
        expectedMax.clear();
        expectedMax.put("2020-02-01T00:00:00.000Z", 5.0);
        assertThat(maxAsMap(bk1bdh), equalTo(expectedMax));
    });
}
Also used : AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) DerivativePipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.DerivativePipelineAggregationBuilder) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) TreeMap(java.util.TreeMap) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms)

Aggregations

AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)62 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)39 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)35 ArrayList (java.util.ArrayList)31 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)31 Map (java.util.Map)30 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)30 List (java.util.List)29 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)29 IndexReader (org.apache.lucene.index.IndexReader)29 IndexSearcher (org.apache.lucene.search.IndexSearcher)29 Directory (org.apache.lucene.store.Directory)29 Collections (java.util.Collections)28 Settings (org.opensearch.common.settings.Settings)27 Arrays (java.util.Arrays)26 GlobalAggregationBuilder (org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder)25 Document (org.apache.lucene.document.Document)24 IOException (java.io.IOException)23 HashMap (java.util.HashMap)23 Consumer (java.util.function.Consumer)23