Search in sources :

Example 1 with InternalTerms

use of org.opensearch.search.aggregations.bucket.terms.InternalTerms in project OpenSearch by opensearch-project.

the class InternalMultiBucketAggregationTests method testResolveToSpecificBucket.

public void testResolveToSpecificBucket() {
    AggregationPath path = AggregationPath.parse("string_terms['foo']>the_avg.value");
    List<LongTerms.Bucket> buckets = new ArrayList<>();
    InternalAggregation agg = new InternalAvg("the_avg", 2, 1, DocValueFormat.RAW, Collections.emptyMap());
    InternalAggregations internalStringAggs = InternalAggregations.from(Collections.singletonList(agg));
    List<StringTerms.Bucket> stringBuckets = Collections.singletonList(new StringTerms.Bucket(new BytesRef("foo".getBytes(StandardCharsets.UTF_8), 0, "foo".getBytes(StandardCharsets.UTF_8).length), 1, internalStringAggs, false, 0, DocValueFormat.RAW));
    InternalTerms termsAgg = new StringTerms("string_terms", BucketOrder.count(false), BucketOrder.count(false), 1, 0, Collections.emptyMap(), DocValueFormat.RAW, 1, false, 0, stringBuckets, 0);
    InternalAggregations internalAggregations = InternalAggregations.from(Collections.singletonList(termsAgg));
    LongTerms.Bucket bucket = new LongTerms.Bucket(19, 1, internalAggregations, false, 0, DocValueFormat.RAW);
    buckets.add(bucket);
    Object[] value = (Object[]) resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms");
    assertThat(value[0], equalTo(2.0));
}
Also used : ArrayList(java.util.ArrayList) AggregationPath(org.opensearch.search.aggregations.support.AggregationPath) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) InternalTerms(org.opensearch.search.aggregations.bucket.terms.InternalTerms) InternalAvg(org.opensearch.search.aggregations.metrics.InternalAvg) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with InternalTerms

use of org.opensearch.search.aggregations.bucket.terms.InternalTerms 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 3 with InternalTerms

use of org.opensearch.search.aggregations.bucket.terms.InternalTerms in project OpenSearch by opensearch-project.

the class MinAggregatorTests method testOrderByEmptyAggregation.

public void testOrderByEmptyAggregation() throws IOException {
    AggregationBuilder termsBuilder = new TermsAggregationBuilder("terms").field("number").order(BucketOrder.compound(BucketOrder.aggregation("filter>min", true))).subAggregation(new FilterAggregationBuilder("filter", termQuery("number", 100)).subAggregation(new MinAggregationBuilder("min").field("number")));
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER);
    int numDocs = 10;
    testCase(termsBuilder, new MatchAllDocsQuery(), iw -> {
        for (int i = 0; i < numDocs; i++) {
            iw.addDocument(singleton(new NumericDocValuesField("number", i + 1)));
        }
    }, (Consumer<InternalTerms<?, LongTerms.Bucket>>) terms -> {
        for (int i = 0; i < numDocs; i++) {
            List<LongTerms.Bucket> buckets = terms.getBuckets();
            Terms.Bucket bucket = buckets.get(i);
            assertNotNull(bucket);
            assertEquals((long) i + 1, bucket.getKeyAsNumber());
            assertEquals(1L, bucket.getDocCount());
            Filter filter = bucket.getAggregations().get("filter");
            assertNotNull(filter);
            assertEquals(0L, filter.getDocCount());
            InternalMin min = filter.getAggregations().get("min");
            assertNotNull(min);
            assertEquals(Double.POSITIVE_INFINITY, min.getValue(), 0);
            assertFalse(AggregationInspectionHelper.hasValue(min));
        }
    }, fieldType);
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Query(org.apache.lucene.search.Query) NoMergePolicy(org.apache.lucene.index.NoMergePolicy) Arrays(java.util.Arrays) InternalHistogram(org.opensearch.search.aggregations.bucket.histogram.InternalHistogram) ScriptModule(org.opensearch.script.ScriptModule) ScriptEngine(org.opensearch.script.ScriptEngine) BiFunction(java.util.function.BiFunction) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Term(org.apache.lucene.index.Term) ScriptType(org.opensearch.script.ScriptType) DoublePoint(org.apache.lucene.document.DoublePoint) Document(org.apache.lucene.document.Document) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) MapperService(org.opensearch.index.mapper.MapperService) Collections.singleton(java.util.Collections.singleton) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) MockScriptEngine(org.opensearch.script.MockScriptEngine) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) AggregationInspectionHelper(org.opensearch.search.aggregations.support.AggregationInspectionHelper) QueryBuilders.termQuery(org.opensearch.index.query.QueryBuilders.termQuery) FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) ScriptService(org.opensearch.script.ScriptService) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IpFieldMapper(org.opensearch.index.mapper.IpFieldMapper) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DirectoryReader(org.apache.lucene.index.DirectoryReader) Script(org.opensearch.script.Script) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder) Settings(org.opensearch.common.settings.Settings) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) Tuple(org.opensearch.common.collect.Tuple) InternalTerms(org.opensearch.search.aggregations.bucket.terms.InternalTerms) IndexWriter(org.apache.lucene.index.IndexWriter) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) QueryShardContext(org.opensearch.index.query.QueryShardContext) BigArrays(org.opensearch.common.util.BigArrays) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) BucketOrder(org.opensearch.search.aggregations.BucketOrder) LongPoint(org.apache.lucene.document.LongPoint) HistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder) StringField(org.apache.lucene.document.StringField) CheckedConsumer(org.opensearch.common.CheckedConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) IntPoint(org.apache.lucene.document.IntPoint) CoreValuesSourceType(org.opensearch.search.aggregations.support.CoreValuesSourceType) ValuesSourceType(org.opensearch.search.aggregations.support.ValuesSourceType) FloatPoint(org.apache.lucene.document.FloatPoint) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) IOException(java.io.IOException) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) InternalGlobal(org.opensearch.search.aggregations.bucket.global.InternalGlobal) Consumer(java.util.function.Consumer) Field(org.apache.lucene.document.Field) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Comparator(java.util.Comparator) Collections(java.util.Collections) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder) HistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DoublePoint(org.apache.lucene.document.DoublePoint) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) InternalTerms(org.opensearch.search.aggregations.bucket.terms.InternalTerms) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with InternalTerms

use of org.opensearch.search.aggregations.bucket.terms.InternalTerms in project OpenSearch by opensearch-project.

the class InternalMultiBucketAggregationTests method testResolveToMissingSpecificBucket.

public void testResolveToMissingSpecificBucket() {
    AggregationPath path = AggregationPath.parse("string_terms['bar']>the_avg.value");
    List<LongTerms.Bucket> buckets = new ArrayList<>();
    InternalAggregation agg = new InternalAvg("the_avg", 2, 1, DocValueFormat.RAW, Collections.emptyMap());
    InternalAggregations internalStringAggs = InternalAggregations.from(Collections.singletonList(agg));
    List<StringTerms.Bucket> stringBuckets = Collections.singletonList(new StringTerms.Bucket(new BytesRef("foo".getBytes(StandardCharsets.UTF_8), 0, "foo".getBytes(StandardCharsets.UTF_8).length), 1, internalStringAggs, false, 0, DocValueFormat.RAW));
    InternalTerms termsAgg = new StringTerms("string_terms", BucketOrder.count(false), BucketOrder.count(false), 1, 0, Collections.emptyMap(), DocValueFormat.RAW, 1, false, 0, stringBuckets, 0);
    InternalAggregations internalAggregations = InternalAggregations.from(Collections.singletonList(termsAgg));
    LongTerms.Bucket bucket = new LongTerms.Bucket(19, 1, internalAggregations, false, 0, DocValueFormat.RAW);
    buckets.add(bucket);
    InvalidAggregationPathException e = expectThrows(InvalidAggregationPathException.class, () -> resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms"));
    assertThat(e.getMessage(), equalTo("Cannot find an key ['bar'] in [string_terms]"));
}
Also used : ArrayList(java.util.ArrayList) AggregationPath(org.opensearch.search.aggregations.support.AggregationPath) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) InternalTerms(org.opensearch.search.aggregations.bucket.terms.InternalTerms) InternalAvg(org.opensearch.search.aggregations.metrics.InternalAvg) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

ArrayList (java.util.ArrayList)4 InternalTerms (org.opensearch.search.aggregations.bucket.terms.InternalTerms)4 LongTerms (org.opensearch.search.aggregations.bucket.terms.LongTerms)4 BytesRef (org.apache.lucene.util.BytesRef)3 Document (org.apache.lucene.document.Document)2 Field (org.apache.lucene.document.Field)2 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)2 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)2 IndexReader (org.apache.lucene.index.IndexReader)2 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 Directory (org.apache.lucene.store.Directory)2 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)2 Script (org.opensearch.script.Script)2 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Collections.singleton (java.util.Collections.singleton)1 Comparator (java.util.Comparator)1