Search in sources :

Example 6 with MaxAggregationBuilder

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

the class NestedAggregatorTests method testNestedWithPipeline.

/**
 * This tests to make sure pipeline aggs embedded under a SingleBucket agg (like nested)
 * are properly reduced
 */
public void testNestedWithPipeline() throws IOException {
    int numRootDocs = randomIntBetween(1, 20);
    int expectedNestedDocs = 0;
    double expectedMaxValue = Double.NEGATIVE_INFINITY;
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
            for (int i = 0; i < numRootDocs; i++) {
                List<Document> documents = new ArrayList<>();
                expectedMaxValue = Math.max(expectedMaxValue, generateMaxDocs(documents, 1, i, NESTED_OBJECT, VALUE_FIELD_NAME));
                expectedNestedDocs += 1;
                Document document = new Document();
                document.add(new Field(IdFieldMapper.NAME, Uid.encodeId(Integer.toString(i)), IdFieldMapper.Defaults.FIELD_TYPE));
                document.add(sequenceIDFields.primaryTerm);
                documents.add(document);
                iw.addDocuments(documents);
            }
            iw.commit();
        }
        try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
            NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder(NESTED_AGG, NESTED_OBJECT).subAggregation(new TermsAggregationBuilder("terms").field(VALUE_FIELD_NAME).userValueTypeHint(ValueType.NUMERIC).subAggregation(new MaxAggregationBuilder(MAX_AGG_NAME).field(VALUE_FIELD_NAME)).subAggregation(new BucketScriptPipelineAggregationBuilder("bucketscript", Collections.singletonMap("_value", MAX_AGG_NAME), new Script(ScriptType.INLINE, MockScriptEngine.NAME, INVERSE_SCRIPT, Collections.emptyMap()))));
            MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG);
            InternalNested nested = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType);
            assertEquals(expectedNestedDocs, nested.getDocCount());
            assertEquals(NESTED_AGG, nested.getName());
            assertEquals(expectedNestedDocs, nested.getDocCount());
            InternalTerms<?, LongTerms.Bucket> terms = (InternalTerms) nested.getProperty("terms");
            assertNotNull(terms);
            for (LongTerms.Bucket bucket : terms.getBuckets()) {
                InternalMax max = (InternalMax) bucket.getAggregations().asMap().get(MAX_AGG_NAME);
                InternalSimpleValue bucketScript = (InternalSimpleValue) bucket.getAggregations().asMap().get("bucketscript");
                assertNotNull(max);
                assertNotNull(bucketScript);
                assertEquals(max.getValue(), -bucketScript.getValue(), Double.MIN_VALUE);
            }
            assertTrue(AggregationInspectionHelper.hasValue(nested));
        }
    }
}
Also used : BucketScriptPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Field(org.apache.lucene.document.Field) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Directory(org.apache.lucene.store.Directory) Script(org.opensearch.script.Script) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) InternalSimpleValue(org.opensearch.search.aggregations.pipeline.InternalSimpleValue) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) InternalTerms(org.opensearch.search.aggregations.bucket.terms.InternalTerms) IndexReader(org.apache.lucene.index.IndexReader) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 7 with MaxAggregationBuilder

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

the class NestedAggregatorTests method testNoDocs.

public void testNoDocs() throws IOException {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
        // intentionally not writing any docs
        }
        try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
            NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder(NESTED_AGG, NESTED_OBJECT);
            MaxAggregationBuilder maxAgg = new MaxAggregationBuilder(MAX_AGG_NAME).field(VALUE_FIELD_NAME);
            nestedBuilder.subAggregation(maxAgg);
            MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG);
            InternalNested nested = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType);
            assertEquals(NESTED_AGG, nested.getName());
            assertEquals(0, nested.getDocCount());
            InternalMax max = (InternalMax) nested.getProperty(MAX_AGG_NAME);
            assertEquals(MAX_AGG_NAME, max.getName());
            assertEquals(Double.NEGATIVE_INFINITY, max.getValue(), Double.MIN_VALUE);
            assertFalse(AggregationInspectionHelper.hasValue(nested));
        }
    }
}
Also used : MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 8 with MaxAggregationBuilder

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

the class ReverseNestedAggregatorTests method testNoDocs.

public void testNoDocs() throws IOException {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
        // intentionally not writing any docs
        }
        try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
            NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder(NESTED_AGG, NESTED_OBJECT);
            ReverseNestedAggregationBuilder reverseNestedBuilder = new ReverseNestedAggregationBuilder(REVERSE_AGG_NAME);
            nestedBuilder.subAggregation(reverseNestedBuilder);
            MaxAggregationBuilder maxAgg = new MaxAggregationBuilder(MAX_AGG_NAME).field(VALUE_FIELD_NAME);
            reverseNestedBuilder.subAggregation(maxAgg);
            MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG);
            Nested nested = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType);
            ReverseNested reverseNested = (ReverseNested) ((InternalAggregation) nested).getProperty(REVERSE_AGG_NAME);
            assertEquals(REVERSE_AGG_NAME, reverseNested.getName());
            assertEquals(0, reverseNested.getDocCount());
            InternalMax max = (InternalMax) ((InternalAggregation) reverseNested).getProperty(MAX_AGG_NAME);
            assertEquals(MAX_AGG_NAME, max.getName());
            assertEquals(Double.NEGATIVE_INFINITY, max.getValue(), Double.MIN_VALUE);
        }
    }
}
Also used : MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) AggregationBuilders.reverseNested(org.opensearch.search.aggregations.AggregationBuilders.reverseNested) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 9 with MaxAggregationBuilder

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

the class ReverseNestedAggregatorTests method testFieldAlias.

public void testFieldAlias() throws IOException {
    int numParentDocs = randomIntBetween(1, 20);
    int expectedParentDocs = 0;
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG);
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
            for (int i = 0; i < numParentDocs; i++) {
                List<Document> documents = new ArrayList<>();
                int numNestedDocs = randomIntBetween(0, 20);
                if (numNestedDocs > 0) {
                    expectedParentDocs++;
                }
                for (int nested = 0; nested < numNestedDocs; nested++) {
                    Document document = new Document();
                    document.add(new Field(IdFieldMapper.NAME, Uid.encodeId(Integer.toString(i)), IdFieldMapper.Defaults.NESTED_FIELD_TYPE));
                    document.add(new Field(TypeFieldMapper.NAME, "__" + NESTED_OBJECT, TypeFieldMapper.Defaults.NESTED_FIELD_TYPE));
                    documents.add(document);
                }
                Document document = new Document();
                document.add(new Field(IdFieldMapper.NAME, Uid.encodeId(Integer.toString(i)), IdFieldMapper.Defaults.FIELD_TYPE));
                long value = randomNonNegativeLong() % 10000;
                document.add(new SortedNumericDocValuesField(VALUE_FIELD_NAME, value));
                document.add(SeqNoFieldMapper.SequenceIDFields.emptySeqID().primaryTerm);
                documents.add(document);
                iw.addDocuments(documents);
            }
            iw.commit();
        }
        try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
            MaxAggregationBuilder maxAgg = max(MAX_AGG_NAME).field(VALUE_FIELD_NAME);
            MaxAggregationBuilder aliasMaxAgg = max(MAX_AGG_NAME).field(VALUE_FIELD_NAME + "-alias");
            NestedAggregationBuilder agg = nested(NESTED_AGG, NESTED_OBJECT).subAggregation(reverseNested(REVERSE_AGG_NAME).subAggregation(maxAgg));
            NestedAggregationBuilder aliasAgg = nested(NESTED_AGG, NESTED_OBJECT).subAggregation(reverseNested(REVERSE_AGG_NAME).subAggregation(aliasMaxAgg));
            Nested nested = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), agg, fieldType);
            Nested aliasNested = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), aliasAgg, fieldType);
            ReverseNested reverseNested = nested.getAggregations().get(REVERSE_AGG_NAME);
            ReverseNested aliasReverseNested = aliasNested.getAggregations().get(REVERSE_AGG_NAME);
            assertEquals(reverseNested, aliasReverseNested);
            assertEquals(expectedParentDocs, reverseNested.getDocCount());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) AggregationBuilders.reverseNested(org.opensearch.search.aggregations.AggregationBuilders.reverseNested) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Field(org.apache.lucene.document.Field) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 10 with MaxAggregationBuilder

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

the class RareTermsAggregatorTests method testEmbeddedMaxAgg.

public void testEmbeddedMaxAgg() throws IOException {
    Query query = new MatchAllDocsQuery();
    testSearchCase(query, dataset, aggregation -> {
        MaxAggregationBuilder max = new MaxAggregationBuilder("the_max").field(LONG_FIELD);
        aggregation.field(LONG_FIELD).maxDocCount(1).subAggregation(max);
    }, agg -> {
        assertEquals(1, agg.getBuckets().size());
        LongRareTerms.Bucket bucket = (LongRareTerms.Bucket) agg.getBuckets().get(0);
        assertThat(bucket.getKey(), equalTo(1L));
        assertThat(bucket.getDocCount(), equalTo(1L));
        Aggregations children = bucket.getAggregations();
        assertThat(children.asList().size(), equalTo(1));
        assertThat(children.asList().get(0).getName(), equalTo("the_max"));
        assertThat(((Max) (children.asList().get(0))).getValue(), equalTo(1.0));
    });
    testSearchCase(query, dataset, aggregation -> {
        MaxAggregationBuilder max = new MaxAggregationBuilder("the_max").field(LONG_FIELD);
        aggregation.field(KEYWORD_FIELD).maxDocCount(1).subAggregation(max);
    }, agg -> {
        assertEquals(1, agg.getBuckets().size());
        StringRareTerms.Bucket bucket = (StringRareTerms.Bucket) agg.getBuckets().get(0);
        assertThat(bucket.getKey(), equalTo("1"));
        assertThat(bucket.getDocCount(), equalTo(1L));
        Aggregations children = bucket.getAggregations();
        assertThat(children.asList().size(), equalTo(1));
        assertThat(children.asList().get(0).getName(), equalTo("the_max"));
        assertThat(((Max) (children.asList().get(0))).getValue(), equalTo(1.0));
    });
}
Also used : MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) Aggregations(org.opensearch.search.aggregations.Aggregations) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

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