Search in sources :

Example 1 with LongTerms

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

the class NestedIT method testNestedAsSubAggregation.

public void testNestedAsSubAggregation() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("top_values").field("value").size(100).collectMode(aggCollectionMode).subAggregation(nested("nested", "nested").subAggregation(max("max_value").field("nested.value")))).get();
    assertSearchResponse(response);
    LongTerms values = response.getAggregations().get("top_values");
    assertThat(values, notNullValue());
    assertThat(values.getName(), equalTo("top_values"));
    assertThat(values.getBuckets(), notNullValue());
    assertThat(values.getBuckets().size(), equalTo(numParents));
    for (int i = 0; i < numParents; i++) {
        String topValue = "" + (i + 1);
        assertThat(values.getBucketByKey(topValue), notNullValue());
        Nested nested = values.getBucketByKey(topValue).getAggregations().get("nested");
        assertThat(nested, notNullValue());
        Max max = nested.getAggregations().get("max_value");
        assertThat(max, notNullValue());
        assertThat(max.getValue(), equalTo(numChildren[i] == 0 ? Double.NEGATIVE_INFINITY : (double) i + numChildren[i]));
    }
}
Also used : Max(org.opensearch.search.aggregations.metrics.Max) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) Nested(org.opensearch.search.aggregations.bucket.nested.Nested) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with LongTerms

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

the class ChildrenToParentAggregatorTests method testTermsParentChildTerms.

public void testTermsParentChildTerms() throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    final Map<String, Tuple<Integer, Integer>> expectedParentChildRelations = setupIndex(indexWriter);
    indexWriter.close();
    SortedMap<Integer, Long> sortedValues = new TreeMap<>();
    for (Tuple<Integer, Integer> value : expectedParentChildRelations.values()) {
        Long l = sortedValues.computeIfAbsent(value.v2(), integer -> 0L);
        sortedValues.put(value.v2(), l + 1);
    }
    IndexReader indexReader = OpenSearchDirectoryReader.wrap(DirectoryReader.open(directory), new ShardId(new Index("foo", "_na_"), 1));
    // TODO set "maybeWrap" to true for IndexSearcher once #23338 is resolved
    IndexSearcher indexSearcher = newSearcher(indexReader, false, true);
    // verify a terms-aggregation inside the parent-aggregation which itself is inside a
    // terms-aggregation on the child-documents
    testCaseTermsParentTerms(new MatchAllDocsQuery(), indexSearcher, longTerms -> {
        assertNotNull(longTerms);
        for (LongTerms.Bucket bucket : longTerms.getBuckets()) {
            assertNotNull(bucket);
            assertNotNull(bucket.getKeyAsString());
        }
    });
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Index(org.opensearch.index.Index) TreeMap(java.util.TreeMap) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ShardId(org.opensearch.index.shard.ShardId) IndexReader(org.apache.lucene.index.IndexReader) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Tuple(org.opensearch.common.collect.Tuple) Directory(org.apache.lucene.store.Directory)

Example 3 with LongTerms

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

the class VariableWidthHistogramAggregatorTests method testSubAggregationReduction.

public void testSubAggregationReduction() throws IOException {
    final List<Number> dataset = Arrays.asList(1L, 1L, 1L, 2L, 2L);
    testSearchCase(DEFAULT_QUERY, dataset, false, aggregation -> aggregation.field(NUMERIC_FIELD).setNumBuckets(3).setInitialBuffer(12).setShardSize(4).subAggregation(new TermsAggregationBuilder("terms").field(NUMERIC_FIELD).shardSize(2).size(1)), histogram -> {
        double deltaError = 1d / 10000d;
        // This is a test to make sure that the sub aggregations get reduced
        // This terms sub aggregation has shardSize (2) != size (1), so we will get 1 bucket only if
        // InternalVariableWidthHistogram reduces the sub aggregations.
        LongTerms terms = histogram.getBuckets().get(0).getAggregations().get("terms");
        assertEquals(1L, terms.getBuckets().size(), deltaError);
        assertEquals(1L, terms.getBuckets().get(0).getKey());
    });
}
Also used : TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms)

Example 4 with LongTerms

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

the class ChildrenToParentAggregatorTests method testCaseTermsParentTerms.

// run a terms aggregation on the number in child-documents, then a parent aggregation and then terms on the parent-number
private void testCaseTermsParentTerms(Query query, IndexSearcher indexSearcher, Consumer<LongTerms> verify) throws IOException {
    AggregationBuilder aggregationBuilder = new TermsAggregationBuilder("subvalue_terms").field("subNumber").subAggregation(new ParentAggregationBuilder("to_parent", CHILD_TYPE).subAggregation(new TermsAggregationBuilder("value_terms").field("number")));
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
    MappedFieldType subFieldType = new NumberFieldMapper.NumberFieldType("subNumber", NumberFieldMapper.NumberType.LONG);
    LongTerms result = searchAndReduce(indexSearcher, query, aggregationBuilder, fieldType, subFieldType);
    verify.accept(result);
}
Also used : TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms)

Example 5 with LongTerms

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

the class ChildrenToParentAggregatorTests method testParentChildTerms.

public void testParentChildTerms() throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    final Map<String, Tuple<Integer, Integer>> expectedParentChildRelations = setupIndex(indexWriter);
    indexWriter.close();
    SortedMap<Integer, Long> entries = new TreeMap<>();
    for (Tuple<Integer, Integer> value : expectedParentChildRelations.values()) {
        Long l = entries.computeIfAbsent(value.v2(), integer -> 0L);
        entries.put(value.v2(), l + 1);
    }
    List<Map.Entry<Integer, Long>> sortedValues = new ArrayList<>(entries.entrySet());
    sortedValues.sort((o1, o2) -> {
        // sort larger values first
        int ret = o2.getValue().compareTo(o1.getValue());
        if (ret != 0) {
            return ret;
        }
        // on equal value, sort by key
        return o1.getKey().compareTo(o2.getKey());
    });
    IndexReader indexReader = OpenSearchDirectoryReader.wrap(DirectoryReader.open(directory), new ShardId(new Index("foo", "_na_"), 1));
    // TODO set "maybeWrap" to true for IndexSearcher once #23338 is resolved
    IndexSearcher indexSearcher = newSearcher(indexReader, false, true);
    // verify a terms-aggregation inside the parent-aggregation
    testCaseTerms(new MatchAllDocsQuery(), indexSearcher, parent -> {
        assertNotNull(parent);
        assertTrue(JoinAggregationInspectionHelper.hasValue(parent));
        LongTerms valueTerms = parent.getAggregations().get("value_terms");
        assertNotNull(valueTerms);
        List<LongTerms.Bucket> valueTermsBuckets = valueTerms.getBuckets();
        assertNotNull(valueTermsBuckets);
        assertEquals("Had: " + parent, sortedValues.size(), valueTermsBuckets.size());
        int i = 0;
        for (Map.Entry<Integer, Long> entry : sortedValues) {
            LongTerms.Bucket bucket = valueTermsBuckets.get(i);
            assertEquals(entry.getKey().longValue(), bucket.getKeyAsNumber());
            assertEquals(entry.getValue(), (Long) bucket.getDocCount());
            i++;
        }
    });
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ArrayList(java.util.ArrayList) Index(org.opensearch.index.Index) ShardId(org.opensearch.index.shard.ShardId) Directory(org.apache.lucene.store.Directory) TreeMap(java.util.TreeMap) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) IndexReader(org.apache.lucene.index.IndexReader) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Tuple(org.opensearch.common.collect.Tuple)

Aggregations

LongTerms (org.opensearch.search.aggregations.bucket.terms.LongTerms)10 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)6 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)5 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)4 IndexReader (org.apache.lucene.index.IndexReader)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 Directory (org.apache.lucene.store.Directory)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 SearchResponse (org.opensearch.action.search.SearchResponse)2 Tuple (org.opensearch.common.collect.Tuple)2 Index (org.opensearch.index.Index)2 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)2 MaxAggregationBuilder (org.opensearch.search.aggregations.metrics.MaxAggregationBuilder)2 MinAggregationBuilder (org.opensearch.search.aggregations.metrics.MinAggregationBuilder)2 IOException (java.io.IOException)1