Search in sources :

Example 1 with MaxAggregationBuilder

use of org.opensearch.search.aggregations.metrics.MaxAggregationBuilder 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 2 with MaxAggregationBuilder

use of org.opensearch.search.aggregations.metrics.MaxAggregationBuilder 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)

Example 3 with MaxAggregationBuilder

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

the class AutoDateHistogramAggregatorTests method testAsSubAggWithIncreasedRounding.

public void testAsSubAggWithIncreasedRounding() 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 useC = dft.parse("2020-07-01T00:00Z");
        long anHour = dft.resolution().convert(Instant.ofEpochSecond(TimeUnit.HOURS.toSeconds(1)));
        List<List<IndexableField>> docs = new ArrayList<>();
        BytesRef aBytes = new BytesRef("a");
        BytesRef bBytes = new BytesRef("b");
        BytesRef cBytes = new BytesRef("c");
        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 SortedSetDocValuesField("k1", aBytes), new SortedSetDocValuesField("k1", d < useC ? bBytes : cBytes), new SortedNumericDocValuesField("n", 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 TermsAggregationBuilder("k1").field("k1").subAggregation(new AutoDateHistogramAggregationBuilder("dh").field(AGGREGABLE_DATE).setNumBuckets(4).subAggregation(new MaxAggregationBuilder("max").field("n")));
    asSubAggTestCase(builder, buildIndex, (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", 2184);
        expectedDocCount.put("2020-04-01T00:00:00.000Z", 2184);
        expectedDocCount.put("2020-07-01T00:00:00.000Z", 2208);
        expectedDocCount.put("2020-10-01T00:00:00.000Z", 2208);
        assertThat(bucketCountsAsMap(adh), equalTo(expectedDocCount));
        Map<String, Double> expectedMax = new TreeMap<>();
        expectedMax.put("2020-01-01T00:00:00.000Z", 2183.0);
        expectedMax.put("2020-04-01T00:00:00.000Z", 4367.0);
        expectedMax.put("2020-07-01T00:00:00.000Z", 6575.0);
        expectedMax.put("2020-10-01T00:00:00.000Z", 8783.0);
        assertThat(maxAsMap(adh), equalTo(expectedMax));
        StringTerms.Bucket b = terms.getBucketByKey("b");
        InternalAutoDateHistogram bdh = b.getAggregations().get("dh");
        expectedDocCount.clear();
        expectedDocCount.put("2020-01-01T00:00:00.000Z", 2184);
        expectedDocCount.put("2020-04-01T00:00:00.000Z", 2184);
        assertThat(bucketCountsAsMap(bdh), equalTo(expectedDocCount));
        expectedMax.clear();
        expectedMax.put("2020-01-01T00:00:00.000Z", 2183.0);
        expectedMax.put("2020-04-01T00:00:00.000Z", 4367.0);
        assertThat(maxAsMap(bdh), equalTo(expectedMax));
        StringTerms.Bucket c = terms.getBucketByKey("c");
        InternalAutoDateHistogram cdh = c.getAggregations().get("dh");
        expectedDocCount.clear();
        expectedDocCount.put("2020-07-01T00:00:00.000Z", 2208);
        expectedDocCount.put("2020-10-01T00:00:00.000Z", 2208);
        assertThat(bucketCountsAsMap(cdh), equalTo(expectedDocCount));
        expectedMax.clear();
        expectedMax.put("2020-07-01T00:00:00.000Z", 6575.0);
        expectedMax.put("2020-10-01T00:00:00.000Z", 8783.0);
        assertThat(maxAsMap(cdh), 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) IndexableField(org.apache.lucene.index.IndexableField) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) List(java.util.List) ArrayList(java.util.ArrayList) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef)

Example 4 with MaxAggregationBuilder

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

the class CompositeAggregatorTests method testWithTermsSubAggExecutionMode.

public void testWithTermsSubAggExecutionMode() throws Exception {
    // test with no bucket
    for (Aggregator.SubAggCollectionMode mode : Aggregator.SubAggCollectionMode.values()) {
        testSearchCase(Arrays.asList(new MatchAllDocsQuery(), new DocValuesFieldExistsQuery("keyword")), Collections.singletonList(createDocument()), () -> {
            TermsValuesSourceBuilder terms = new TermsValuesSourceBuilder("keyword").field("keyword");
            return new CompositeAggregationBuilder("name", Collections.singletonList(terms)).subAggregation(new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.STRING).field("terms").collectMode(mode).subAggregation(new MaxAggregationBuilder("max").field("long")));
        }, (result) -> {
            assertEquals(0, result.getBuckets().size());
        });
    }
    final List<Map<String, List<Object>>> dataset = new ArrayList<>();
    dataset.addAll(Arrays.asList(createDocument("keyword", "a", "terms", "a", "long", 50L), createDocument("keyword", "c", "terms", "d", "long", 78L), createDocument("keyword", "a", "terms", "w", "long", 78L), createDocument("keyword", "d", "terms", "y", "long", 76L), createDocument("keyword", "c", "terms", "y", "long", 70L)));
    for (Aggregator.SubAggCollectionMode mode : Aggregator.SubAggCollectionMode.values()) {
        testSearchCase(Arrays.asList(new MatchAllDocsQuery(), new DocValuesFieldExistsQuery("keyword")), dataset, () -> {
            TermsValuesSourceBuilder terms = new TermsValuesSourceBuilder("keyword").field("keyword");
            return new CompositeAggregationBuilder("name", Collections.singletonList(terms)).subAggregation(new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.STRING).field("terms").collectMode(mode).subAggregation(new MaxAggregationBuilder("max").field("long")));
        }, (result) -> {
            assertEquals(3, result.getBuckets().size());
            assertEquals("{keyword=a}", result.getBuckets().get(0).getKeyAsString());
            assertEquals(2L, result.getBuckets().get(0).getDocCount());
            StringTerms subTerms = result.getBuckets().get(0).getAggregations().get("terms");
            assertEquals(2, subTerms.getBuckets().size());
            assertEquals("a", subTerms.getBuckets().get(0).getKeyAsString());
            assertEquals("w", subTerms.getBuckets().get(1).getKeyAsString());
            InternalMax max = subTerms.getBuckets().get(0).getAggregations().get("max");
            assertEquals(50L, (long) max.getValue());
            max = subTerms.getBuckets().get(1).getAggregations().get("max");
            assertEquals(78L, (long) max.getValue());
            assertEquals("{keyword=c}", result.getBuckets().get(1).getKeyAsString());
            assertEquals(2L, result.getBuckets().get(1).getDocCount());
            subTerms = result.getBuckets().get(1).getAggregations().get("terms");
            assertEquals(2, subTerms.getBuckets().size());
            assertEquals("d", subTerms.getBuckets().get(0).getKeyAsString());
            assertEquals("y", subTerms.getBuckets().get(1).getKeyAsString());
            max = subTerms.getBuckets().get(0).getAggregations().get("max");
            assertEquals(78L, (long) max.getValue());
            max = subTerms.getBuckets().get(1).getAggregations().get("max");
            assertEquals(70L, (long) max.getValue());
            assertEquals("{keyword=d}", result.getBuckets().get(2).getKeyAsString());
            assertEquals(1L, result.getBuckets().get(2).getDocCount());
            subTerms = result.getBuckets().get(2).getAggregations().get("terms");
            assertEquals(1, subTerms.getBuckets().size());
            assertEquals("y", subTerms.getBuckets().get(0).getKeyAsString());
            max = subTerms.getBuckets().get(0).getAggregations().get("max");
            assertEquals(76L, (long) max.getValue());
        });
    }
}
Also used : InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) ArrayList(java.util.ArrayList) Aggregator(org.opensearch.search.aggregations.Aggregator) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with MaxAggregationBuilder

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

the class NestedAggregatorTests method testNestedOrdering.

public void testNestedOrdering() throws IOException {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
            iw.addDocuments(generateBook("1", new String[] { "a" }, new int[] { 12, 13, 14 }));
            iw.addDocuments(generateBook("2", new String[] { "b" }, new int[] { 5, 50 }));
            iw.addDocuments(generateBook("3", new String[] { "c" }, new int[] { 39, 19 }));
            iw.addDocuments(generateBook("4", new String[] { "d" }, new int[] { 2, 1, 3 }));
            iw.addDocuments(generateBook("5", new String[] { "a" }, new int[] { 70, 10 }));
            iw.addDocuments(generateBook("6", new String[] { "e" }, new int[] { 23, 21 }));
            iw.addDocuments(generateBook("7", new String[] { "e", "a" }, new int[] { 8, 8 }));
            iw.addDocuments(generateBook("8", new String[] { "f" }, new int[] { 12, 14 }));
            iw.addDocuments(generateBook("9", new String[] { "g", "c", "e" }, new int[] { 18, 8 }));
        }
        try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
            MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("num_pages", NumberFieldMapper.NumberType.LONG);
            MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType("author");
            TermsAggregationBuilder termsBuilder = new TermsAggregationBuilder("authors").userValueTypeHint(ValueType.STRING).field("author").order(BucketOrder.aggregation("chapters>num_pages.value", true));
            NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder("chapters", "nested_chapters");
            MaxAggregationBuilder maxAgg = new MaxAggregationBuilder("num_pages").field("num_pages");
            nestedBuilder.subAggregation(maxAgg);
            termsBuilder.subAggregation(nestedBuilder);
            Terms terms = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), termsBuilder, fieldType1, fieldType2);
            assertEquals(7, terms.getBuckets().size());
            assertEquals("authors", terms.getName());
            Terms.Bucket bucket = terms.getBuckets().get(0);
            assertEquals("d", bucket.getKeyAsString());
            Max numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(3, (int) numPages.getValue());
            bucket = terms.getBuckets().get(1);
            assertEquals("f", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(14, (int) numPages.getValue());
            bucket = terms.getBuckets().get(2);
            assertEquals("g", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(18, (int) numPages.getValue());
            bucket = terms.getBuckets().get(3);
            assertEquals("e", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(23, (int) numPages.getValue());
            bucket = terms.getBuckets().get(4);
            assertEquals("c", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(39, (int) numPages.getValue());
            bucket = terms.getBuckets().get(5);
            assertEquals("b", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(50, (int) numPages.getValue());
            bucket = terms.getBuckets().get(6);
            assertEquals("a", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(70, (int) numPages.getValue());
            // reverse order:
            termsBuilder = new TermsAggregationBuilder("authors").userValueTypeHint(ValueType.STRING).field("author").order(BucketOrder.aggregation("chapters>num_pages.value", false));
            nestedBuilder = new NestedAggregationBuilder("chapters", "nested_chapters");
            maxAgg = new MaxAggregationBuilder("num_pages").field("num_pages");
            nestedBuilder.subAggregation(maxAgg);
            termsBuilder.subAggregation(nestedBuilder);
            terms = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), termsBuilder, fieldType1, fieldType2);
            assertEquals(7, terms.getBuckets().size());
            assertEquals("authors", terms.getName());
            bucket = terms.getBuckets().get(0);
            assertEquals("a", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(70, (int) numPages.getValue());
            bucket = terms.getBuckets().get(1);
            assertEquals("b", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(50, (int) numPages.getValue());
            bucket = terms.getBuckets().get(2);
            assertEquals("c", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(39, (int) numPages.getValue());
            bucket = terms.getBuckets().get(3);
            assertEquals("e", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(23, (int) numPages.getValue());
            bucket = terms.getBuckets().get(4);
            assertEquals("g", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(18, (int) numPages.getValue());
            bucket = terms.getBuckets().get(5);
            assertEquals("f", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(14, (int) numPages.getValue());
            bucket = terms.getBuckets().get(6);
            assertEquals("d", bucket.getKeyAsString());
            numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
            assertEquals(3, (int) numPages.getValue());
        }
    }
}
Also used : Max(org.opensearch.search.aggregations.metrics.Max) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) InternalTerms(org.opensearch.search.aggregations.bucket.terms.InternalTerms) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

MaxAggregationBuilder (org.opensearch.search.aggregations.metrics.MaxAggregationBuilder)18 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)12 InternalMax (org.opensearch.search.aggregations.metrics.InternalMax)12 IndexReader (org.apache.lucene.index.IndexReader)10 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)10 Directory (org.apache.lucene.store.Directory)10 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)10 ArrayList (java.util.ArrayList)9 Document (org.apache.lucene.document.Document)7 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)7 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)6 Field (org.apache.lucene.document.Field)5 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)5 StringTerms (org.opensearch.search.aggregations.bucket.terms.StringTerms)5 HashMap (java.util.HashMap)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)3 Query (org.apache.lucene.search.Query)3 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)3