use of org.opensearch.search.aggregations.bucket.terms.TermsAggregator in project OpenSearch by opensearch-project.
the class AvgAggregatorTests method testOrderByEmptyAggregation.
public void testOrderByEmptyAggregation() throws IOException {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER);
AggregationBuilder aggregationBuilder = new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.NUMERIC).field("value").order(BucketOrder.compound(BucketOrder.aggregation("filter>avg", true))).subAggregation(AggregationBuilders.filter("filter", termQuery("value", 100)).subAggregation(AggregationBuilders.avg("avg").field("value")));
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
final int numDocs = 10;
for (int i = 0; i < numDocs; i++) {
indexWriter.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
}
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
TermsAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
aggregator.preCollection();
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
aggregator.postCollection();
Terms terms = (Terms) aggregator.buildTopLevel();
assertNotNull(terms);
List<? extends Terms.Bucket> buckets = terms.getBuckets();
assertNotNull(buckets);
assertEquals(10, buckets.size());
for (int i = 0; i < 10; i++) {
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());
Avg avg = filter.getAggregations().get("avg");
assertNotNull(avg);
assertEquals(Double.NaN, avg.getValue(), 0);
}
indexReader.close();
directory.close();
}
use of org.opensearch.search.aggregations.bucket.terms.TermsAggregator in project OpenSearch by opensearch-project.
the class MaxAggregatorTests method testOrderByEmptyAggregation.
public void testOrderByEmptyAggregation() throws IOException {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER);
TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.NUMERIC).field("value").order(BucketOrder.compound(BucketOrder.aggregation("filter>max", true))).subAggregation(AggregationBuilders.filter("filter", termQuery("value", 100)).subAggregation(AggregationBuilders.max("max").field("value")));
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
final int numDocs = 10;
for (int i = 0; i < numDocs; i++) {
indexWriter.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
}
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
TermsAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
aggregator.preCollection();
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
aggregator.postCollection();
Terms terms = (Terms) aggregator.buildTopLevel();
assertNotNull(terms);
List<? extends Terms.Bucket> buckets = terms.getBuckets();
assertNotNull(buckets);
assertEquals(10, buckets.size());
for (int i = 0; i < 10; i++) {
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());
Max max = filter.getAggregations().get("max");
assertNotNull(max);
assertEquals(Double.NEGATIVE_INFINITY, max.getValue(), 0);
}
indexReader.close();
directory.close();
}
use of org.opensearch.search.aggregations.bucket.terms.TermsAggregator in project OpenSearch by opensearch-project.
the class MaxAggregatorTests method testNestedEarlyTermination.
public void testNestedEarlyTermination() throws Exception {
MappedFieldType multiValuesfieldType = new NumberFieldMapper.NumberFieldType("values", NumberFieldMapper.NumberType.INTEGER);
MappedFieldType singleValueFieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER);
Directory directory = newDirectory();
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
final int numDocs = 10;
for (int i = 0; i < numDocs; i++) {
Document document = new Document();
document.add(new NumericDocValuesField("value", i + 1));
document.add(new SortedNumericDocValuesField("values", i + 2));
document.add(new SortedNumericDocValuesField("values", i + 3));
indexWriter.addDocument(document);
}
indexWriter.close();
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
for (Aggregator.SubAggCollectionMode collectionMode : Aggregator.SubAggCollectionMode.values()) {
MaxAggregationBuilder maxAggregationBuilder = new MaxAggregationBuilder("max").field("values");
ValueCountAggregationBuilder countAggregationBuilder = new ValueCountAggregationBuilder("count").field("values");
TermsAggregationBuilder termsAggregationBuilder = new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.NUMERIC).field("value").collectMode(collectionMode).subAggregation(new MaxAggregationBuilder("sub_max").field("invalid"));
MaxAggregator maxAggregator = createAggregator(maxAggregationBuilder, indexSearcher, multiValuesfieldType);
ValueCountAggregator countAggregator = createAggregator(countAggregationBuilder, indexSearcher, multiValuesfieldType);
TermsAggregator termsAggregator = createAggregator(termsAggregationBuilder, indexSearcher, singleValueFieldType);
BucketCollector bucketCollector = MultiBucketCollector.wrap(maxAggregator, countAggregator, termsAggregator);
bucketCollector.preCollection();
indexSearcher.search(new MatchAllDocsQuery(), bucketCollector);
bucketCollector.postCollection();
InternalMax max = (InternalMax) maxAggregator.buildTopLevel();
assertNotNull(max);
assertEquals(12.0, max.getValue(), 0);
assertEquals("max", max.getName());
InternalValueCount count = (InternalValueCount) countAggregator.buildTopLevel();
assertNotNull(count);
assertEquals(20L, count.getValue());
assertEquals("count", count.getName());
Terms terms = (Terms) termsAggregator.buildTopLevel();
assertNotNull(terms);
List<? extends Terms.Bucket> buckets = terms.getBuckets();
assertNotNull(buckets);
assertEquals(10, buckets.size());
for (Terms.Bucket b : buckets) {
InternalMax subMax = b.getAggregations().get("sub_max");
assertEquals(Double.NEGATIVE_INFINITY, subMax.getValue(), 0);
}
}
indexReader.close();
directory.close();
}
Aggregations