Search in sources :

Example 1 with Min

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

Example 2 with Min

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

the class SamplerAggregatorTests method testRidiculousSize.

public void testRidiculousSize() throws IOException {
    TextFieldType textFieldType = new TextFieldType("text");
    MappedFieldType numericFieldType = new NumberFieldMapper.NumberFieldType("int", NumberFieldMapper.NumberType.LONG);
    IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
    indexWriterConfig.setMaxBufferedDocs(100);
    // flush on open to have a single segment with predictable docIds
    indexWriterConfig.setRAMBufferSizeMB(100);
    try (Directory dir = newDirectory();
        IndexWriter w = new IndexWriter(dir, indexWriterConfig)) {
        for (long value : new long[] { 7, 3, -10, -6, 5, 50 }) {
            Document doc = new Document();
            StringBuilder text = new StringBuilder();
            for (int i = 0; i < value; i++) {
                text.append("good ");
            }
            doc.add(new Field("text", text.toString(), TextFieldMapper.Defaults.FIELD_TYPE));
            doc.add(new SortedNumericDocValuesField("int", value));
            w.addDocument(doc);
        }
        // Test with an outrageously large size to ensure that the maxDoc protection works
        SamplerAggregationBuilder aggBuilder = new SamplerAggregationBuilder("sampler").shardSize(Integer.MAX_VALUE).subAggregation(new MinAggregationBuilder("min").field("int"));
        try (IndexReader reader = DirectoryReader.open(w)) {
            assertEquals("test expects a single segment", 1, reader.leaves().size());
            IndexSearcher searcher = new IndexSearcher(reader);
            InternalSampler sampler = searchAndReduce(searcher, new TermQuery(new Term("text", "good")), aggBuilder, textFieldType, numericFieldType);
            Min min = sampler.getAggregations().get("min");
            assertEquals(3.0, min.getValue(), 0);
            assertTrue(AggregationInspectionHelper.hasValue(sampler));
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) TextFieldType(org.opensearch.index.mapper.TextFieldMapper.TextFieldType) Document(org.apache.lucene.document.Document) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Field(org.apache.lucene.document.Field) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Min(org.opensearch.search.aggregations.metrics.Min) IndexWriter(org.apache.lucene.index.IndexWriter) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) IndexReader(org.apache.lucene.index.IndexReader) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory)

Example 3 with Min

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

the class SamplerAggregatorTests method testSampler.

/**
 * Uses the sampler aggregation to find the minimum value of a field out of the top 3 scoring documents in a search.
 */
public void testSampler() throws IOException {
    TextFieldType textFieldType = new TextFieldType("text");
    MappedFieldType numericFieldType = new NumberFieldMapper.NumberFieldType("int", NumberFieldMapper.NumberType.LONG);
    IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
    indexWriterConfig.setMaxBufferedDocs(100);
    // flush on open to have a single segment with predictable docIds
    indexWriterConfig.setRAMBufferSizeMB(100);
    try (Directory dir = newDirectory();
        IndexWriter w = new IndexWriter(dir, indexWriterConfig)) {
        for (long value : new long[] { 7, 3, -10, -6, 5, 50 }) {
            Document doc = new Document();
            StringBuilder text = new StringBuilder();
            for (int i = 0; i < value; i++) {
                text.append("good ");
            }
            doc.add(new Field("text", text.toString(), TextFieldMapper.Defaults.FIELD_TYPE));
            doc.add(new SortedNumericDocValuesField("int", value));
            w.addDocument(doc);
        }
        SamplerAggregationBuilder aggBuilder = new SamplerAggregationBuilder("sampler").shardSize(3).subAggregation(new MinAggregationBuilder("min").field("int"));
        try (IndexReader reader = DirectoryReader.open(w)) {
            assertEquals("test expects a single segment", 1, reader.leaves().size());
            IndexSearcher searcher = new IndexSearcher(reader);
            InternalSampler sampler = searchAndReduce(searcher, new TermQuery(new Term("text", "good")), aggBuilder, textFieldType, numericFieldType);
            Min min = sampler.getAggregations().get("min");
            assertEquals(5.0, min.getValue(), 0);
            assertTrue(AggregationInspectionHelper.hasValue(sampler));
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) TextFieldType(org.opensearch.index.mapper.TextFieldMapper.TextFieldType) Document(org.apache.lucene.document.Document) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Field(org.apache.lucene.document.Field) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Min(org.opensearch.search.aggregations.metrics.Min) IndexWriter(org.apache.lucene.index.IndexWriter) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) IndexReader(org.apache.lucene.index.IndexReader) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory)

Aggregations

IndexReader (org.apache.lucene.index.IndexReader)3 Directory (org.apache.lucene.store.Directory)3 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)3 Min (org.opensearch.search.aggregations.metrics.Min)3 MinAggregationBuilder (org.opensearch.search.aggregations.metrics.MinAggregationBuilder)3 Document (org.apache.lucene.document.Document)2 Field (org.apache.lucene.document.Field)2 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)2 Term (org.apache.lucene.index.Term)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 TermQuery (org.apache.lucene.search.TermQuery)2 TextFieldType (org.opensearch.index.mapper.TextFieldMapper.TextFieldType)2 ArrayList (java.util.ArrayList)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 Tuple (org.opensearch.common.collect.Tuple)1 InternalTerms (org.opensearch.search.aggregations.bucket.terms.InternalTerms)1 LongTerms (org.opensearch.search.aggregations.bucket.terms.LongTerms)1