Search in sources :

Example 1 with TermsAggregator

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) TermsAggregator(org.opensearch.search.aggregations.bucket.terms.TermsAggregator) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IntPoint(org.apache.lucene.document.IntPoint) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 2 with TermsAggregator

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermsAggregator(org.opensearch.search.aggregations.bucket.terms.TermsAggregator) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DoublePoint(org.apache.lucene.document.DoublePoint) 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) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 3 with TermsAggregator

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Document(org.apache.lucene.document.Document) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Directory(org.apache.lucene.store.Directory) TermsAggregator(org.opensearch.search.aggregations.bucket.terms.TermsAggregator) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) GlobalAggregator(org.opensearch.search.aggregations.bucket.global.GlobalAggregator) TermsAggregator(org.opensearch.search.aggregations.bucket.terms.TermsAggregator) Aggregator(org.opensearch.search.aggregations.Aggregator) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) MultiBucketCollector(org.opensearch.search.aggregations.MultiBucketCollector) BucketCollector(org.opensearch.search.aggregations.BucketCollector) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Aggregations

IntPoint (org.apache.lucene.document.IntPoint)3 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)3 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)3 IndexReader (org.apache.lucene.index.IndexReader)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 Directory (org.apache.lucene.store.Directory)3 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)3 Terms (org.opensearch.search.aggregations.bucket.terms.Terms)3 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)3 TermsAggregator (org.opensearch.search.aggregations.bucket.terms.TermsAggregator)3 DoublePoint (org.apache.lucene.document.DoublePoint)2 FloatPoint (org.apache.lucene.document.FloatPoint)2 LongPoint (org.apache.lucene.document.LongPoint)2 Filter (org.opensearch.search.aggregations.bucket.filter.Filter)2 Document (org.apache.lucene.document.Document)1 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)1 Aggregator (org.opensearch.search.aggregations.Aggregator)1 BucketCollector (org.opensearch.search.aggregations.BucketCollector)1