Search in sources :

Example 1 with MinAggregationBuilder

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

the class EsAbstractConditionAggregation method regMinA.

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

Example 2 with MinAggregationBuilder

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

the class EsAbstractConditionAggregation method regMinA.

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

Example 3 with MinAggregationBuilder

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

the class EsAbstractConditionAggregation method regMinA.

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

Example 4 with MinAggregationBuilder

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

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

the class NestedAggregatorTests method testNestedOrdering_random.

public void testNestedOrdering_random() throws IOException {
    int numBooks = randomIntBetween(32, 512);
    List<Tuple<String, int[]>> books = new ArrayList<>();
    for (int i = 0; i < numBooks; i++) {
        int numChapters = randomIntBetween(1, 8);
        int[] chapters = new int[numChapters];
        for (int j = 0; j < numChapters; j++) {
            chapters[j] = randomIntBetween(2, 64);
        }
        books.add(Tuple.tuple(String.format(Locale.ROOT, "%03d", i), chapters));
    }
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
            int id = 0;
            for (Tuple<String, int[]> book : books) {
                iw.addDocuments(generateBook(String.format(Locale.ROOT, "%03d", id), new String[] { book.v1() }, book.v2()));
                id++;
            }
        }
        for (Tuple<String, int[]> book : books) {
            Arrays.sort(book.v2());
        }
        books.sort((o1, o2) -> {
            int cmp = Integer.compare(o1.v2()[0], o2.v2()[0]);
            if (cmp == 0) {
                return o1.v1().compareTo(o2.v1());
            } else {
                return cmp;
            }
        });
        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).size(books.size()).field("author").order(BucketOrder.compound(BucketOrder.aggregation("chapters>num_pages.value", true), BucketOrder.key(true)));
            NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder("chapters", "nested_chapters");
            MinAggregationBuilder minAgg = new MinAggregationBuilder("num_pages").field("num_pages");
            nestedBuilder.subAggregation(minAgg);
            termsBuilder.subAggregation(nestedBuilder);
            Terms terms = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), termsBuilder, fieldType1, fieldType2);
            assertEquals(books.size(), terms.getBuckets().size());
            assertEquals("authors", terms.getName());
            for (int i = 0; i < books.size(); i++) {
                Tuple<String, int[]> book = books.get(i);
                Terms.Bucket bucket = terms.getBuckets().get(i);
                assertEquals(book.v1(), bucket.getKeyAsString());
                Min numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
                assertEquals(book.v2()[0], (int) numPages.getValue());
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) 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) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Min(org.opensearch.search.aggregations.metrics.Min) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) Tuple(org.opensearch.common.collect.Tuple) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

MinAggregationBuilder (org.opensearch.search.aggregations.metrics.MinAggregationBuilder)13 IndexReader (org.apache.lucene.index.IndexReader)7 Directory (org.apache.lucene.store.Directory)7 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)7 IndexSearcher (org.apache.lucene.search.IndexSearcher)6 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)5 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)5 Document (org.apache.lucene.document.Document)4 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)4 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)4 InternalMin (org.opensearch.search.aggregations.metrics.InternalMin)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 List (java.util.List)2 Consumer (java.util.function.Consumer)2 Field (org.apache.lucene.document.Field)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)2 IndexableField (org.apache.lucene.index.IndexableField)2 Term (org.apache.lucene.index.Term)2