Search in sources :

Example 1 with Aggregation

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

the class MatrixStatsNamedXContentProvider method getNamedXContentParsers.

@Override
public List<NamedXContentRegistry.Entry> getNamedXContentParsers() {
    ParseField parseField = new ParseField(MatrixStatsAggregationBuilder.NAME);
    ContextParser<Object, Aggregation> contextParser = (p, name) -> ParsedMatrixStats.fromXContent(p, (String) name);
    return singletonList(new NamedXContentRegistry.Entry(Aggregation.class, parseField, contextParser));
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) List(java.util.List) Aggregation(org.opensearch.search.aggregations.Aggregation) ParsedMatrixStats(org.opensearch.search.aggregations.matrix.stats.ParsedMatrixStats) MatrixStatsAggregationBuilder(org.opensearch.search.aggregations.matrix.stats.MatrixStatsAggregationBuilder) NamedXContentProvider(org.opensearch.plugins.spi.NamedXContentProvider) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ContextParser(org.opensearch.common.xcontent.ContextParser) ParseField(org.opensearch.common.ParseField) Collections.singletonList(java.util.Collections.singletonList) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ParseField(org.opensearch.common.ParseField)

Example 2 with Aggregation

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

the class NaNSortingIT method assertCorrectlySorted.

private void assertCorrectlySorted(Histogram histo, boolean asc, SubAggregation agg) {
    assertThat(histo, notNullValue());
    double previousValue = asc ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
    for (Histogram.Bucket bucket : histo.getBuckets()) {
        Aggregation sub = bucket.getAggregations().get(agg.name);
        double value = agg.getValue(sub);
        assertTrue(Comparators.compareDiscardNaN(previousValue, value, asc) <= 0);
        previousValue = value;
    }
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram)

Example 3 with Aggregation

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

the class ParentIT method testSimpleParentAgg.

public void testSimpleParentAgg() throws Exception {
    final SearchRequestBuilder searchRequest = client().prepareSearch("test").setSize(10000).setQuery(matchQuery("randomized", true)).addAggregation(parent("to_article", "comment").subAggregation(terms("category").field("category").size(10000)));
    SearchResponse searchResponse = searchRequest.get();
    assertSearchResponse(searchResponse);
    long articlesWithComment = articleToControl.values().stream().filter(parentControl -> !parentControl.commentIds.isEmpty()).count();
    Parent parentAgg = searchResponse.getAggregations().get("to_article");
    assertThat("Request: " + searchRequest + "\nResponse: " + searchResponse + "\n", parentAgg.getDocCount(), equalTo(articlesWithComment));
    Terms categoryTerms = parentAgg.getAggregations().get("category");
    long categoriesWithComments = categoryToControl.values().stream().filter(control -> !control.commentIds.isEmpty()).count();
    assertThat("Buckets: " + categoryTerms.getBuckets().stream().map((Function<Terms.Bucket, String>) MultiBucketsAggregation.Bucket::getKeyAsString).collect(Collectors.toList()) + "\nCategories: " + categoryToControl.keySet(), (long) categoryTerms.getBuckets().size(), equalTo(categoriesWithComments));
    for (Map.Entry<String, Control> entry : categoryToControl.entrySet()) {
        // no children for this category -> no entry in the child to parent-aggregation
        if (entry.getValue().commentIds.isEmpty()) {
            assertNull(categoryTerms.getBucketByKey(entry.getKey()));
            continue;
        }
        final Terms.Bucket categoryBucket = categoryTerms.getBucketByKey(entry.getKey());
        assertNotNull("Failed for category " + entry.getKey(), categoryBucket);
        assertThat("Failed for category " + entry.getKey(), categoryBucket.getKeyAsString(), equalTo(entry.getKey()));
        // count all articles in this category which have at least one comment
        long articlesForCategory = articleToControl.values().stream().filter(parentControl -> parentControl.category.equals(entry.getKey())).filter(parentControl -> !parentControl.commentIds.isEmpty()).count();
        assertThat("Failed for category " + entry.getKey(), categoryBucket.getDocCount(), equalTo(articlesForCategory));
    }
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) Set(java.util.Set) HashMap(java.util.HashMap) JoinAggregationBuilders.parent(org.opensearch.join.aggregations.JoinAggregationBuilders.parent) MultiBucketsAggregation(org.opensearch.search.aggregations.bucket.MultiBucketsAggregation) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) QueryBuilders.matchQuery(org.opensearch.index.query.QueryBuilders.matchQuery) AggregationBuilders.terms(org.opensearch.search.aggregations.AggregationBuilders.terms) HashSet(java.util.HashSet) AggregationBuilders.topHits(org.opensearch.search.aggregations.AggregationBuilders.topHits) List(java.util.List) Stream(java.util.stream.Stream) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) SearchResponse(org.opensearch.action.search.SearchResponse) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) Function(java.util.function.Function) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Aggregation

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

the class ChildrenToParentAggregatorTests method testNoDocs.

public void testNoDocs() throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    // intentionally not writing any docs
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    testCase(new MatchAllDocsQuery(), newSearcher(indexReader, false, true), childrenToParent -> {
        assertEquals(0, childrenToParent.getDocCount());
        Aggregation parentAggregation = childrenToParent.getAggregations().get("in_parent");
        assertEquals(0, childrenToParent.getDocCount());
        assertNotNull("Aggregations: " + childrenToParent.getAggregations().asMap(), parentAggregation);
        assertEquals(Double.POSITIVE_INFINITY, ((InternalMin) parentAggregation).getValue(), Double.MIN_VALUE);
        assertFalse(JoinAggregationInspectionHelper.hasValue(childrenToParent));
    });
    indexReader.close();
    directory.close();
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) IndexReader(org.apache.lucene.index.IndexReader) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 5 with Aggregation

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

the class ParentJoinNamedXContentProvider method getNamedXContentParsers.

@Override
public List<NamedXContentRegistry.Entry> getNamedXContentParsers() {
    ParseField parseFieldChildren = new ParseField(ChildrenAggregationBuilder.NAME);
    ParseField parseFieldParent = new ParseField(ParentAggregationBuilder.NAME);
    ContextParser<Object, Aggregation> contextParserChildren = (p, name) -> ParsedChildren.fromXContent(p, (String) name);
    ContextParser<Object, Aggregation> contextParserParent = (p, name) -> ParsedParent.fromXContent(p, (String) name);
    return Arrays.asList(new NamedXContentRegistry.Entry(Aggregation.class, parseFieldChildren, contextParserChildren), new NamedXContentRegistry.Entry(Aggregation.class, parseFieldParent, contextParserParent));
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) Arrays(java.util.Arrays) List(java.util.List) ParentAggregationBuilder(org.opensearch.join.aggregations.ParentAggregationBuilder) ParsedParent(org.opensearch.join.aggregations.ParsedParent) Aggregation(org.opensearch.search.aggregations.Aggregation) NamedXContentProvider(org.opensearch.plugins.spi.NamedXContentProvider) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ContextParser(org.opensearch.common.xcontent.ContextParser) ChildrenAggregationBuilder(org.opensearch.join.aggregations.ChildrenAggregationBuilder) ParsedChildren(org.opensearch.join.aggregations.ParsedChildren) ParseField(org.opensearch.common.ParseField) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ParseField(org.opensearch.common.ParseField)

Aggregations

Aggregation (org.opensearch.search.aggregations.Aggregation)37 List (java.util.List)19 InternalAggregation (org.opensearch.search.aggregations.InternalAggregation)19 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)18 SearchResponse (org.opensearch.action.search.SearchResponse)18 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)16 Matchers.containsString (org.hamcrest.Matchers.containsString)12 Script (org.opensearch.script.Script)12 Map (java.util.Map)8 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6 IndexReader (org.apache.lucene.index.IndexReader)5 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)5 Query (org.apache.lucene.search.Query)5 Directory (org.apache.lucene.store.Directory)5 Aggregations (org.opensearch.search.aggregations.Aggregations)5 ParsedAggregation (org.opensearch.search.aggregations.ParsedAggregation)5 StringTerms (org.opensearch.search.aggregations.bucket.terms.StringTerms)5 Terms (org.opensearch.search.aggregations.bucket.terms.Terms)5 IOException (java.io.IOException)4