Search in sources :

Example 1 with InternalMin

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

the class RangeHistogramAggregatorTests method testAsSubAgg.

public void testAsSubAgg() throws IOException {
    AggregationBuilder request = new HistogramAggregationBuilder("outer").field("outer").interval(5).subAggregation(new HistogramAggregationBuilder("inner").field("inner").interval(5).subAggregation(new MinAggregationBuilder("min").field("n")));
    CheckedConsumer<RandomIndexWriter, IOException> buildIndex = iw -> {
        List<List<IndexableField>> docs = new ArrayList<>();
        for (int n = 0; n < 10000; n++) {
            BytesRef outerRange = RangeType.LONG.encodeRanges(org.opensearch.common.collect.Set.of(new RangeFieldMapper.Range(RangeType.LONG, n % 100, n % 100 + 10, true, true)));
            BytesRef innerRange = RangeType.LONG.encodeRanges(org.opensearch.common.collect.Set.of(new RangeFieldMapper.Range(RangeType.LONG, n / 100, n / 100 + 10, true, true)));
            docs.add(org.opensearch.common.collect.List.of(new BinaryDocValuesField("outer", outerRange), new BinaryDocValuesField("inner", innerRange), new SortedNumericDocValuesField("n", n)));
        }
        iw.addDocuments(docs);
    };
    Consumer<InternalHistogram> verify = outer -> {
        assertThat(outer.getBuckets(), hasSize(22));
        for (int outerIdx = 0; outerIdx < 22; outerIdx++) {
            InternalHistogram.Bucket outerBucket = outer.getBuckets().get(outerIdx);
            assertThat(outerBucket.getKey(), equalTo(5.0 * outerIdx));
            InternalHistogram inner = outerBucket.getAggregations().get("inner");
            assertThat(inner.getBuckets(), hasSize(22));
            for (int innerIdx = 0; innerIdx < 22; innerIdx++) {
                InternalHistogram.Bucket innerBucket = inner.getBuckets().get(innerIdx);
                assertThat(innerBucket.getKey(), equalTo(5.0 * innerIdx));
                InternalMin min = innerBucket.getAggregations().get("min");
                int minOuterIdxWithOverlappingRange = Math.max(0, outerIdx - 2);
                int minInnerIdxWithOverlappingRange = Math.max(0, innerIdx - 2);
                assertThat(min.getValue(), equalTo(minOuterIdxWithOverlappingRange * 5.0 + minInnerIdxWithOverlappingRange * 500.0));
            }
        }
    };
    testCase(request, new MatchAllDocsQuery(), buildIndex, verify, rangeField("outer", RangeType.LONG), rangeField("inner", RangeType.LONG), longField("n"));
}
Also used : InternalMin(org.opensearch.search.aggregations.metrics.InternalMin) InetAddresses(org.opensearch.common.network.InetAddresses) IndexableField(org.apache.lucene.index.IndexableField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) CheckedConsumer(org.opensearch.common.CheckedConsumer) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Document(org.apache.lucene.document.Document) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) Directory(org.apache.lucene.store.Directory) Matchers.hasSize(org.hamcrest.Matchers.hasSize) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) BytesRef(org.apache.lucene.util.BytesRef) Set(java.util.Set) IOException(java.io.IOException) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Consumer(java.util.function.Consumer) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) RangeType(org.opensearch.index.mapper.RangeType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Collections(java.util.Collections) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) RangeFieldMapper(org.opensearch.index.mapper.RangeFieldMapper) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) IndexableField(org.apache.lucene.index.IndexableField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) InternalMin(org.opensearch.search.aggregations.metrics.InternalMin) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) ArrayList(java.util.ArrayList) List(java.util.List) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with InternalMin

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

the class NumericHistogramAggregatorTests method testAsSubAgg.

public void testAsSubAgg() throws IOException {
    AggregationBuilder request = new HistogramAggregationBuilder("outer").field("outer").interval(5).subAggregation(new HistogramAggregationBuilder("inner").field("inner").interval(5).subAggregation(new MinAggregationBuilder("min").field("n")));
    CheckedConsumer<RandomIndexWriter, IOException> buildIndex = iw -> {
        List<List<IndexableField>> docs = new ArrayList<>();
        for (int n = 0; n < 10000; n++) {
            docs.add(org.opensearch.common.collect.List.of(new SortedNumericDocValuesField("outer", n % 100), new SortedNumericDocValuesField("inner", n / 100), new SortedNumericDocValuesField("n", n)));
        }
        iw.addDocuments(docs);
    };
    Consumer<InternalHistogram> verify = outer -> {
        assertThat(outer.getBuckets(), hasSize(20));
        for (int outerIdx = 0; outerIdx < 20; outerIdx++) {
            InternalHistogram.Bucket outerBucket = outer.getBuckets().get(outerIdx);
            assertThat(outerBucket.getKey(), equalTo(5.0 * outerIdx));
            InternalHistogram inner = outerBucket.getAggregations().get("inner");
            assertThat(inner.getBuckets(), hasSize(20));
            for (int innerIdx = 0; innerIdx < 20; innerIdx++) {
                InternalHistogram.Bucket innerBucket = inner.getBuckets().get(innerIdx);
                assertThat(innerBucket.getKey(), equalTo(5.0 * innerIdx));
                InternalMin min = innerBucket.getAggregations().get("min");
                assertThat(min.getValue(), equalTo(outerIdx * 5.0 + innerIdx * 500.0));
            }
        }
    };
    testCase(request, new MatchAllDocsQuery(), buildIndex, verify, longField("outer"), longField("inner"), longField("n"));
}
Also used : InternalMin(org.opensearch.search.aggregations.metrics.InternalMin) Arrays(java.util.Arrays) IndexableField(org.apache.lucene.index.IndexableField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) CheckedConsumer(org.opensearch.common.CheckedConsumer) NumericUtils(org.apache.lucene.util.NumericUtils) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) Directory(org.apache.lucene.store.Directory) Matchers.hasSize(org.hamcrest.Matchers.hasSize) AggregationInspectionHelper(org.opensearch.search.aggregations.support.AggregationInspectionHelper) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Consumer(java.util.function.Consumer) List(java.util.List) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) Matchers.equalTo(org.hamcrest.Matchers.equalTo) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IndexableField(org.apache.lucene.index.IndexableField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) InternalMin(org.opensearch.search.aggregations.metrics.InternalMin) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) ArrayList(java.util.ArrayList) List(java.util.List) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 3 with InternalMin

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

the class InternalSingleBucketAggregationTestCase method mutateInstance.

@Override
protected T mutateInstance(T instance) {
    String name = instance.getName();
    long docCount = instance.getDocCount();
    InternalAggregations aggregations = instance.getAggregations();
    Map<String, Object> metadata = instance.getMetadata();
    switch(between(0, 3)) {
        case 0:
            name += randomAlphaOfLength(5);
            break;
        case 1:
            docCount += between(1, 2000);
            break;
        case 2:
            List<InternalAggregation> aggs = new ArrayList<>();
            aggs.add(new InternalMax("new_max", randomDouble(), randomNumericDocValueFormat(), emptyMap()));
            aggs.add(new InternalMin("new_min", randomDouble(), randomNumericDocValueFormat(), emptyMap()));
            aggregations = InternalAggregations.from(aggs);
            break;
        case 3:
        default:
            if (metadata == null) {
                metadata = new HashMap<>(1);
            } else {
                metadata = new HashMap<>(instance.getMetadata());
            }
            metadata.put(randomAlphaOfLength(15), randomInt());
            break;
    }
    return createTestInstance(name, docCount, aggregations, metadata);
}
Also used : InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) InternalMin(org.opensearch.search.aggregations.metrics.InternalMin) ArrayList(java.util.ArrayList)

Example 4 with InternalMin

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

the class InternalSingleBucketAggregationTestCase method assertReduced.

@Override
protected final void assertReduced(T reduced, List<T> inputs) {
    assertEquals(inputs.stream().mapToLong(InternalSingleBucketAggregation::getDocCount).sum(), reduced.getDocCount());
    if (hasInternalMax) {
        double expected = inputs.stream().mapToDouble(i -> {
            InternalMax max = i.getAggregations().get("max");
            return max.getValue();
        }).max().getAsDouble();
        InternalMax reducedMax = reduced.getAggregations().get("max");
        assertEquals(expected, reducedMax.getValue(), 0);
    }
    if (hasInternalMin) {
        double expected = inputs.stream().mapToDouble(i -> {
            InternalMin min = i.getAggregations().get("min");
            return min.getValue();
        }).min().getAsDouble();
        InternalMin reducedMin = reduced.getAggregations().get("min");
        assertEquals(expected, reducedMin.getValue(), 0);
    }
    extraAssertReduced(reduced, inputs);
}
Also used : InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) InternalMin(org.opensearch.search.aggregations.metrics.InternalMin) InternalSingleBucketAggregation(org.opensearch.search.aggregations.bucket.InternalSingleBucketAggregation)

Example 5 with InternalMin

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

the class ParentToChildrenAggregatorTests method testParentChildAsSubAgg.

public void testParentChildAsSubAgg() throws IOException {
    try (Directory directory = newDirectory()) {
        RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
        final Map<String, Tuple<Integer, Integer>> expectedParentChildRelations = setupIndex(indexWriter);
        indexWriter.close();
        try (IndexReader indexReader = OpenSearchDirectoryReader.wrap(DirectoryReader.open(directory), new ShardId(new Index("foo", "_na_"), 1))) {
            IndexSearcher indexSearcher = newSearcher(indexReader, false, true);
            AggregationBuilder request = new TermsAggregationBuilder("t").field("kwd").subAggregation(new ChildrenAggregationBuilder("children", CHILD_TYPE).subAggregation(new MinAggregationBuilder("min").field("number")));
            long expectedEvenChildCount = 0;
            double expectedEvenMin = Double.MAX_VALUE;
            long expectedOddChildCount = 0;
            double expectedOddMin = Double.MAX_VALUE;
            for (Map.Entry<String, Tuple<Integer, Integer>> e : expectedParentChildRelations.entrySet()) {
                if (Integer.valueOf(e.getKey().substring("parent".length())) % 2 == 0) {
                    expectedEvenChildCount += e.getValue().v1();
                    expectedEvenMin = Math.min(expectedEvenMin, e.getValue().v2());
                } else {
                    expectedOddChildCount += e.getValue().v1();
                    expectedOddMin = Math.min(expectedOddMin, e.getValue().v2());
                }
            }
            StringTerms result = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), request, longField("number"), keywordField("kwd"));
            StringTerms.Bucket evenBucket = result.getBucketByKey("even");
            InternalChildren evenChildren = evenBucket.getAggregations().get("children");
            InternalMin evenMin = evenChildren.getAggregations().get("min");
            assertThat(evenChildren.getDocCount(), equalTo(expectedEvenChildCount));
            assertThat(evenMin.getValue(), equalTo(expectedEvenMin));
            if (expectedOddChildCount > 0) {
                StringTerms.Bucket oddBucket = result.getBucketByKey("odd");
                InternalChildren oddChildren = oddBucket.getAggregations().get("children");
                InternalMin oddMin = oddChildren.getAggregations().get("min");
                assertThat(oddChildren.getDocCount(), equalTo(expectedOddChildCount));
                assertThat(oddMin.getValue(), equalTo(expectedOddMin));
            } else {
                assertNull(result.getBucketByKey("odd"));
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Index(org.opensearch.index.Index) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ShardId(org.opensearch.index.shard.ShardId) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) InternalMin(org.opensearch.search.aggregations.metrics.InternalMin) IndexReader(org.apache.lucene.index.IndexReader) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) Map(java.util.Map) HashMap(java.util.HashMap) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Tuple(org.opensearch.common.collect.Tuple) Directory(org.apache.lucene.store.Directory)

Aggregations

InternalMin (org.opensearch.search.aggregations.metrics.InternalMin)7 ArrayList (java.util.ArrayList)4 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)4 InternalMax (org.opensearch.search.aggregations.metrics.InternalMax)4 MinAggregationBuilder (org.opensearch.search.aggregations.metrics.MinAggregationBuilder)4 IndexReader (org.apache.lucene.index.IndexReader)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 Directory (org.apache.lucene.store.Directory)3 IOException (java.io.IOException)2 List (java.util.List)2 Consumer (java.util.function.Consumer)2 Document (org.apache.lucene.document.Document)2 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)2 IndexableField (org.apache.lucene.index.IndexableField)2 BytesRef (org.apache.lucene.util.BytesRef)2 Matchers.equalTo (org.hamcrest.Matchers.equalTo)2 Matchers.hasSize (org.hamcrest.Matchers.hasSize)2 CheckedConsumer (org.opensearch.common.CheckedConsumer)2