Search in sources :

Example 16 with MaxAggregationBuilder

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

the class NestedAggregatorTests method testDoubleNestingMax.

public void testDoubleNestingMax() 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<>();
                int numNestedDocs = randomIntBetween(0, 20);
                expectedMaxValue = Math.max(expectedMaxValue, generateMaxDocs(documents, numNestedDocs, i, NESTED_OBJECT + "." + NESTED_OBJECT2, VALUE_FIELD_NAME));
                expectedNestedDocs += numNestedDocs;
                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 + "." + NESTED_OBJECT2);
            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(expectedNestedDocs, nested.getDocCount());
            assertEquals(NESTED_AGG, nested.getName());
            assertEquals(expectedNestedDocs, nested.getDocCount());
            InternalMax max = (InternalMax) nested.getProperty(MAX_AGG_NAME);
            assertEquals(MAX_AGG_NAME, max.getName());
            assertEquals(expectedMaxValue, max.getValue(), Double.MIN_VALUE);
            if (expectedNestedDocs > 0) {
                assertTrue(AggregationInspectionHelper.hasValue(nested));
            } else {
                assertFalse(AggregationInspectionHelper.hasValue(nested));
            }
        }
    }
}
Also used : InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) 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) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 17 with MaxAggregationBuilder

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

the class ReverseNestedAggregatorTests method testMaxFromParentDocs.

public void testMaxFromParentDocs() throws IOException {
    int numParentDocs = randomIntBetween(1, 20);
    int expectedParentDocs = 0;
    int expectedNestedDocs = 0;
    double expectedMaxValue = Double.NEGATIVE_INFINITY;
    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);
                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);
                    expectedNestedDocs++;
                }
                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);
                if (numNestedDocs > 0) {
                    expectedMaxValue = Math.max(expectedMaxValue, value);
                    expectedParentDocs++;
                }
                documents.add(document);
                iw.addDocuments(documents);
            }
            iw.commit();
        }
        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);
            assertEquals(expectedNestedDocs, nested.getDocCount());
            ReverseNested reverseNested = (ReverseNested) ((InternalAggregation) nested).getProperty(REVERSE_AGG_NAME);
            assertEquals(REVERSE_AGG_NAME, reverseNested.getName());
            assertEquals(expectedParentDocs, reverseNested.getDocCount());
            InternalMax max = (InternalMax) ((InternalAggregation) reverseNested).getProperty(MAX_AGG_NAME);
            assertEquals(MAX_AGG_NAME, max.getName());
            assertEquals(expectedMaxValue, max.getValue(), Double.MIN_VALUE);
        }
    }
}
Also used : InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) 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) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 18 with MaxAggregationBuilder

use of org.opensearch.search.aggregations.metrics.MaxAggregationBuilder in project fess by codelibs.

the class EsAbstractConditionAggregation method regMaxA.

protected MaxAggregationBuilder regMaxA(String name, String field) {
    MaxAggregationBuilder builder = AggregationBuilders.max(name).field(field);
    regA(builder);
    return builder;
}
Also used : MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder)

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