Search in sources :

Example 11 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 12 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 13 with Aggregation

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

the class ScriptedMetricIT method testMapCombineReduceWithParams.

public void testMapCombineReduceWithParams() {
    Map<String, Object> varsMap = new HashMap<>();
    varsMap.put("multiplier", 1);
    Map<String, Object> params = new HashMap<>();
    params.put("vars", varsMap);
    Script mapScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "state.list.add(vars.multiplier)", Collections.emptyMap());
    Script combineScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum state values as a new aggregation", Collections.emptyMap());
    Script reduceScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum all states (lists) values as a new aggregation", Collections.emptyMap());
    SearchResponse response = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(scriptedMetric("scripted").params(params).mapScript(mapScript).combineScript(combineScript).reduceScript(reduceScript)).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits().value, equalTo(numDocs));
    Aggregation aggregation = response.getAggregations().get("scripted");
    assertThat(aggregation, notNullValue());
    assertThat(aggregation, instanceOf(ScriptedMetric.class));
    ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
    assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
    assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
    assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
    List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
    assertThat(aggregationList.size(), equalTo(1));
    Object object = aggregationList.get(0);
    assertThat(object, notNullValue());
    assertThat(object, instanceOf(Number.class));
    assertThat(((Number) object).longValue(), equalTo(numDocs));
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) Script(org.opensearch.script.Script) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 14 with Aggregation

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

the class ScriptedMetricIT method testInitMapCombineReduceWithParams.

public void testInitMapCombineReduceWithParams() {
    Map<String, Object> varsMap = new HashMap<>();
    varsMap.put("multiplier", 1);
    Map<String, Object> params = new HashMap<>();
    params.put("vars", varsMap);
    Script initScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "vars.multiplier = 3", Collections.emptyMap());
    Script mapScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "state.list.add(vars.multiplier)", Collections.emptyMap());
    Script combineScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum state values as a new aggregation", Collections.emptyMap());
    Script reduceScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum all states (lists) values as a new aggregation", Collections.emptyMap());
    SearchResponse response = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(scriptedMetric("scripted").params(params).initScript(initScript).mapScript(mapScript).combineScript(combineScript).reduceScript(reduceScript)).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits().value, equalTo(numDocs));
    Aggregation aggregation = response.getAggregations().get("scripted");
    assertThat(aggregation, notNullValue());
    assertThat(aggregation, instanceOf(ScriptedMetric.class));
    ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
    assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
    assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
    assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
    List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
    assertThat(aggregationList.size(), equalTo(1));
    Object object = aggregationList.get(0);
    assertThat(object, notNullValue());
    assertThat(object, instanceOf(Number.class));
    assertThat(((Number) object).longValue(), equalTo(numDocs * 3));
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) Script(org.opensearch.script.Script) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 15 with Aggregation

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

the class ScriptedMetricIT method testInitMutatesParams.

public void testInitMutatesParams() {
    Map<String, Object> varsMap = new HashMap<>();
    varsMap.put("multiplier", 1);
    Map<String, Object> params = new HashMap<>();
    params.put("vars", varsMap);
    SearchResponse response = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(scriptedMetric("scripted").params(params).initScript(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "vars.multiplier = 3", Collections.emptyMap())).mapScript(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "state.list.add(vars.multiplier)", Collections.emptyMap())).combineScript(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "no-op aggregation", Collections.emptyMap())).reduceScript(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "no-op list aggregation", Collections.emptyMap()))).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits().value, equalTo(numDocs));
    Aggregation aggregation = response.getAggregations().get("scripted");
    assertThat(aggregation, notNullValue());
    assertThat(aggregation, instanceOf(ScriptedMetric.class));
    ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) aggregation;
    assertThat(scriptedMetricAggregation.getName(), equalTo("scripted"));
    assertThat(scriptedMetricAggregation.aggregation(), notNullValue());
    assertThat(scriptedMetricAggregation.aggregation(), instanceOf(ArrayList.class));
    List<?> aggregationList = (List<?>) scriptedMetricAggregation.aggregation();
    assertThat(aggregationList.size(), equalTo(getNumShards("idx").numPrimaries));
    long totalCount = 0;
    for (Object object : aggregationList) {
        assertThat(object, notNullValue());
        assertThat(object, instanceOf(HashMap.class));
        Map<String, Object> map = (Map<String, Object>) object;
        assertThat(map, hasKey("list"));
        assertThat(map.get("list"), instanceOf(List.class));
        List<?> list = (List<?>) map.get("list");
        for (Object o : list) {
            assertThat(o, notNullValue());
            assertThat(o, instanceOf(Number.class));
            Number numberValue = (Number) o;
            assertThat(numberValue, equalTo((Number) 3));
            totalCount += numberValue.longValue();
        }
    }
    assertThat(totalCount, equalTo(numDocs * 3));
}
Also used : Script(org.opensearch.script.Script) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) Aggregation(org.opensearch.search.aggregations.Aggregation) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Aggregation (org.opensearch.search.aggregations.Aggregation)54 SearchResponse (org.opensearch.action.search.SearchResponse)31 HashMap (java.util.HashMap)30 List (java.util.List)30 ArrayList (java.util.ArrayList)29 Map (java.util.Map)20 InternalAggregation (org.opensearch.search.aggregations.InternalAggregation)19 Aggregations (org.opensearch.search.aggregations.Aggregations)18 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)16 Matchers.containsString (org.hamcrest.Matchers.containsString)14 IOException (java.io.IOException)13 Collections (java.util.Collections)13 ActionListener (org.opensearch.action.ActionListener)13 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)13 MultiBucketsAggregation (org.opensearch.search.aggregations.bucket.MultiBucketsAggregation)13 Terms (org.opensearch.search.aggregations.bucket.terms.Terms)13 Collectors (java.util.stream.Collectors)12 Script (org.opensearch.script.Script)12 Arrays (java.util.Arrays)11 SearchRequest (org.opensearch.action.search.SearchRequest)11