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);
}
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;
}
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());
}
}
}
}
}
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());
}
}
}
}
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());
}
}
}
}
}
Aggregations