Search in sources :

Example 1 with Aggregator

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

the class DiversifiedAggregatorFactory method registerAggregators.

public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
    builder.register(DiversifiedAggregationBuilder.REGISTRY_KEY, org.opensearch.common.collect.List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), (String name, int shardSize, AggregatorFactories factories, SearchContext context, Aggregator parent, Map<String, Object> metadata, ValuesSourceConfig valuesSourceConfig, int maxDocsPerValue, String executionHint) -> new DiversifiedNumericSamplerAggregator(name, shardSize, factories, context, parent, metadata, valuesSourceConfig, maxDocsPerValue), true);
    builder.register(DiversifiedAggregationBuilder.REGISTRY_KEY, CoreValuesSourceType.BYTES, (String name, int shardSize, AggregatorFactories factories, SearchContext context, Aggregator parent, Map<String, Object> metadata, ValuesSourceConfig valuesSourceConfig, int maxDocsPerValue, String executionHint) -> {
        ExecutionMode execution = null;
        if (executionHint != null) {
            execution = ExecutionMode.fromString(executionHint);
        }
        // In some cases using ordinals is just not supported: override it
        if (execution == null) {
            execution = ExecutionMode.GLOBAL_ORDINALS;
        }
        if ((execution.needsGlobalOrdinals()) && (valuesSourceConfig.hasGlobalOrdinals() == false)) {
            execution = ExecutionMode.MAP;
        }
        return execution.create(name, factories, shardSize, maxDocsPerValue, valuesSourceConfig, context, parent, metadata);
    }, true);
}
Also used : ValuesSourceConfig(org.opensearch.search.aggregations.support.ValuesSourceConfig) AggregatorFactories(org.opensearch.search.aggregations.AggregatorFactories) SearchContext(org.opensearch.search.internal.SearchContext) NonCollectingAggregator(org.opensearch.search.aggregations.NonCollectingAggregator) Aggregator(org.opensearch.search.aggregations.Aggregator) ExecutionMode(org.opensearch.search.aggregations.bucket.sampler.SamplerAggregator.ExecutionMode) Map(java.util.Map)

Example 2 with Aggregator

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

the class AggregationPath method resolveTopmostAggregator.

/**
 * Resolves the {@linkplain Aggregator} pointed to by the first element
 * of this path against the given root {@linkplain Aggregator}.
 */
public Aggregator resolveTopmostAggregator(Aggregator root) {
    AggregationPath.PathElement token = pathElements.get(0);
    // TODO both unwrap and subAggregator are only used here!
    Aggregator aggregator = ProfilingAggregator.unwrap(root.subAggregator(token.name));
    assert (aggregator instanceof SingleBucketAggregator) || (aggregator instanceof NumericMetricsAggregator) : "this should be picked up before aggregation execution - on validate";
    return aggregator;
}
Also used : NumericMetricsAggregator(org.opensearch.search.aggregations.metrics.NumericMetricsAggregator) SingleBucketAggregator(org.opensearch.search.aggregations.bucket.SingleBucketAggregator) NumericMetricsAggregator(org.opensearch.search.aggregations.metrics.NumericMetricsAggregator) ProfilingAggregator(org.opensearch.search.profile.aggregation.ProfilingAggregator) Aggregator(org.opensearch.search.aggregations.Aggregator) SingleBucketAggregator(org.opensearch.search.aggregations.bucket.SingleBucketAggregator)

Example 3 with Aggregator

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

the class TermsAggregatorTests method testUnmappedWithMissing.

public void testUnmappedWithMissing() throws Exception {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            Document document = new Document();
            document.add(new NumericDocValuesField("unrelated_value", 100));
            indexWriter.addDocument(document);
            try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
                MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType("unrelated_value");
                IndexSearcher indexSearcher = newIndexSearcher(indexReader);
                ValueType[] valueTypes = new ValueType[] { ValueType.STRING, ValueType.LONG, ValueType.DOUBLE };
                String[] fieldNames = new String[] { "string", "long", "double" };
                Object[] missingValues = new Object[] { "abc", 19L, 19.2 };
                for (int i = 0; i < fieldNames.length; i++) {
                    TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").userValueTypeHint(valueTypes[i]).field(fieldNames[i]).missing(missingValues[i]);
                    Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType1);
                    aggregator.preCollection();
                    indexSearcher.search(new MatchAllDocsQuery(), aggregator);
                    aggregator.postCollection();
                    Terms result = reduce(aggregator);
                    assertEquals("_name", result.getName());
                    assertEquals(1, result.getBuckets().size());
                    assertEquals(missingValues[i], result.getBuckets().get(0).getKey());
                    assertEquals(1, result.getBuckets().get(0).getDocCount());
                }
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ValueType(org.opensearch.search.aggregations.support.ValueType) Aggregator(org.opensearch.search.aggregations.Aggregator) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) GeoPoint(org.opensearch.common.geo.GeoPoint) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) 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 4 with Aggregator

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

the class TermsAggregatorTests method testNestedTermsAgg.

public void testNestedTermsAgg() throws Exception {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            Document document = new Document();
            document.add(new SortedDocValuesField("field1", new BytesRef("a")));
            document.add(new SortedDocValuesField("field2", new BytesRef("b")));
            indexWriter.addDocument(document);
            document = new Document();
            document.add(new SortedDocValuesField("field1", new BytesRef("c")));
            document.add(new SortedDocValuesField("field2", new BytesRef("d")));
            indexWriter.addDocument(document);
            document = new Document();
            document.add(new SortedDocValuesField("field1", new BytesRef("e")));
            document.add(new SortedDocValuesField("field2", new BytesRef("f")));
            indexWriter.addDocument(document);
            try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
                IndexSearcher indexSearcher = newIndexSearcher(indexReader);
                String executionHint = randomFrom(TermsAggregatorFactory.ExecutionMode.values()).toString();
                Aggregator.SubAggCollectionMode collectionMode = randomFrom(Aggregator.SubAggCollectionMode.values());
                TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name1").userValueTypeHint(ValueType.STRING).executionHint(executionHint).collectMode(collectionMode).field("field1").order(BucketOrder.key(true)).subAggregation(new TermsAggregationBuilder("_name2").userValueTypeHint(ValueType.STRING).executionHint(executionHint).collectMode(collectionMode).field("field2").order(BucketOrder.key(true)));
                MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType("field1");
                MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType("field2");
                Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType1, fieldType2);
                aggregator.preCollection();
                indexSearcher.search(new MatchAllDocsQuery(), aggregator);
                aggregator.postCollection();
                Terms result = reduce(aggregator);
                assertEquals(3, result.getBuckets().size());
                assertEquals("a", result.getBuckets().get(0).getKeyAsString());
                assertEquals(1L, result.getBuckets().get(0).getDocCount());
                Terms.Bucket nestedBucket = ((Terms) result.getBuckets().get(0).getAggregations().get("_name2")).getBuckets().get(0);
                assertEquals("b", nestedBucket.getKeyAsString());
                assertEquals("c", result.getBuckets().get(1).getKeyAsString());
                assertEquals(1L, result.getBuckets().get(1).getDocCount());
                nestedBucket = ((Terms) result.getBuckets().get(1).getAggregations().get("_name2")).getBuckets().get(0);
                assertEquals("d", nestedBucket.getKeyAsString());
                assertEquals("e", result.getBuckets().get(2).getKeyAsString());
                assertEquals(1L, result.getBuckets().get(2).getDocCount());
                nestedBucket = ((Terms) result.getBuckets().get(2).getAggregations().get("_name2")).getBuckets().get(0);
                assertEquals("f", nestedBucket.getKeyAsString());
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Aggregator(org.opensearch.search.aggregations.Aggregator) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 5 with Aggregator

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

the class TermsAggregatorTests method termsAggregatorWithNestedMaxAgg.

private <T> void termsAggregatorWithNestedMaxAgg(ValueType valueType, MappedFieldType fieldType, Function<Integer, T> valueFactory, Function<T, IndexableField> luceneFieldFactory) throws Exception {
    final Map<T, Long> counts = new HashMap<>();
    int numTerms = scaledRandomIntBetween(8, 128);
    for (int i = 0; i < numTerms; i++) {
        counts.put(valueFactory.apply(i), randomLong());
    }
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            for (Map.Entry<T, Long> entry : counts.entrySet()) {
                Document document = new Document();
                document.add(luceneFieldFactory.apply(entry.getKey()));
                document.add(new NumericDocValuesField("value", entry.getValue()));
                indexWriter.addDocument(document);
            }
            try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
                boolean order = randomBoolean();
                List<Map.Entry<T, Long>> expectedBuckets = new ArrayList<>();
                expectedBuckets.addAll(counts.entrySet());
                BucketOrder bucketOrder = BucketOrder.aggregation("_max", order);
                Comparator<Map.Entry<T, Long>> comparator = Comparator.comparing(Map.Entry::getValue, Long::compareTo);
                if (order == false) {
                    comparator = comparator.reversed();
                }
                expectedBuckets.sort(comparator);
                int size = randomIntBetween(1, counts.size());
                String executionHint = randomFrom(TermsAggregatorFactory.ExecutionMode.values()).toString();
                Aggregator.SubAggCollectionMode collectionMode = randomFrom(Aggregator.SubAggCollectionMode.values());
                logger.info("bucket_order={} size={} execution_hint={}, collect_mode={}", bucketOrder, size, executionHint, collectionMode);
                IndexSearcher indexSearcher = newIndexSearcher(indexReader);
                AggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").userValueTypeHint(valueType).executionHint(executionHint).collectMode(collectionMode).size(size).shardSize(size).field("field").order(bucketOrder).subAggregation(AggregationBuilders.max("_max").field("value"));
                MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.LONG);
                Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType, fieldType2);
                aggregator.preCollection();
                indexSearcher.search(new MatchAllDocsQuery(), aggregator);
                aggregator.postCollection();
                Terms result = reduce(aggregator);
                assertEquals(size, result.getBuckets().size());
                for (int i = 0; i < size; i++) {
                    Map.Entry<T, Long> expected = expectedBuckets.get(i);
                    Terms.Bucket actual = result.getBuckets().get(i);
                    assertEquals(expected.getKey(), actual.getKey());
                }
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder) BucketScriptPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) BucketOrder(org.opensearch.search.aggregations.BucketOrder) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Directory(org.apache.lucene.store.Directory) Aggregator(org.opensearch.search.aggregations.Aggregator) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) GeoPoint(org.opensearch.common.geo.GeoPoint) IndexReader(org.apache.lucene.index.IndexReader) Map(java.util.Map) HashMap(java.util.HashMap) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Aggregations

Aggregator (org.opensearch.search.aggregations.Aggregator)12 IndexReader (org.apache.lucene.index.IndexReader)9 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)9 IndexSearcher (org.apache.lucene.search.IndexSearcher)9 Directory (org.apache.lucene.store.Directory)9 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)8 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)8 Document (org.apache.lucene.document.Document)6 InetAddressPoint (org.apache.lucene.document.InetAddressPoint)4 GeoPoint (org.opensearch.common.geo.GeoPoint)4 Map (java.util.Map)3 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)3 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)3 BytesRef (org.apache.lucene.util.BytesRef)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)2 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)2 BucketOrder (org.opensearch.search.aggregations.BucketOrder)2 NonCollectingAggregator (org.opensearch.search.aggregations.NonCollectingAggregator)2