Search in sources :

Example 36 with Aggregation

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

the class SignificantTermsSignificanceScoreIT method testXContentResponse.

public void testXContentResponse() throws Exception {
    String type = randomBoolean() ? "text" : "long";
    String settings = "{\"index.number_of_shards\": 1, \"index.number_of_replicas\": 0}";
    SharedSignificantTermsTestMethods.index01Docs(type, settings, this);
    SearchRequestBuilder request;
    if ("text".equals(type) && randomBoolean()) {
        // Use significant_text on text fields but occasionally run with alternative of
        // significant_terms on legacy fieldData=true too.
        request = client().prepareSearch(INDEX_NAME).addAggregation(terms("class").field(CLASS_FIELD).subAggregation(significantText("sig_terms", TEXT_FIELD)));
    } else {
        request = client().prepareSearch(INDEX_NAME).addAggregation(terms("class").field(CLASS_FIELD).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD)));
    }
    SearchResponse response = request.get();
    assertSearchResponse(response);
    StringTerms classes = response.getAggregations().get("class");
    assertThat(classes.getBuckets().size(), equalTo(2));
    for (Terms.Bucket classBucket : classes.getBuckets()) {
        Map<String, Aggregation> aggs = classBucket.getAggregations().asMap();
        assertTrue(aggs.containsKey("sig_terms"));
        SignificantTerms agg = (SignificantTerms) aggs.get("sig_terms");
        assertThat(agg.getBuckets().size(), equalTo(1));
        String term = agg.iterator().next().getKeyAsString();
        String classTerm = classBucket.getKeyAsString();
        assertTrue(term.equals(classTerm));
    }
    XContentBuilder responseBuilder = XContentFactory.jsonBuilder();
    responseBuilder.startObject();
    classes.toXContent(responseBuilder, ToXContent.EMPTY_PARAMS);
    responseBuilder.endObject();
    String result = "{\"class\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0," + "\"buckets\":[" + "{" + "\"key\":\"0\"," + "\"doc_count\":4," + "\"sig_terms\":{" + "\"doc_count\":4," + "\"bg_count\":7," + "\"buckets\":[" + "{" + "\"key\":" + (type.equals("long") ? "0," : "\"0\",") + "\"doc_count\":4," + "\"score\":0.39999999999999997," + "\"bg_count\":5" + "}" + "]" + "}" + "}," + "{" + "\"key\":\"1\"," + "\"doc_count\":3," + "\"sig_terms\":{" + "\"doc_count\":3," + "\"bg_count\":7," + "\"buckets\":[" + "{" + "\"key\":" + (type.equals("long") ? "1," : "\"1\",") + "\"doc_count\":3," + "\"score\":0.75," + "\"bg_count\":4" + "}]}}]}}";
    assertThat(Strings.toString(responseBuilder), equalTo(result));
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) SignificantTerms(org.opensearch.search.aggregations.bucket.terms.SignificantTerms) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) SignificantTerms(org.opensearch.search.aggregations.bucket.terms.SignificantTerms) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) AggregationBuilders.significantTerms(org.opensearch.search.aggregations.AggregationBuilders.significantTerms) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 37 with Aggregation

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

the class ScriptedMetricIT method testInitMapCombineReduceWithParamsAsSubAgg.

public void testInitMapCombineReduceWithParamsAsSubAgg() {
    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()).setSize(1000).addAggregation(histogram("histo").field("l_value").interval(1).subAggregation(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("histo");
    assertThat(aggregation, notNullValue());
    assertThat(aggregation, instanceOf(Histogram.class));
    Histogram histoAgg = (Histogram) aggregation;
    assertThat(histoAgg.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histoAgg.getBuckets();
    assertThat(buckets, notNullValue());
    for (Bucket b : buckets) {
        assertThat(b, notNullValue());
        assertThat(b.getDocCount(), equalTo(1L));
        Aggregations subAggs = b.getAggregations();
        assertThat(subAggs, notNullValue());
        assertThat(subAggs.asList().size(), equalTo(1));
        Aggregation subAgg = subAggs.get("scripted");
        assertThat(subAgg, notNullValue());
        assertThat(subAgg, instanceOf(ScriptedMetric.class));
        ScriptedMetric scriptedMetricAggregation = (ScriptedMetric) subAgg;
        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(3L));
    }
}
Also used : Script(org.opensearch.script.Script) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) HashMap(java.util.HashMap) Aggregations(org.opensearch.search.aggregations.Aggregations) 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) Bucket(org.opensearch.search.aggregations.bucket.histogram.Histogram.Bucket) List(java.util.List) ArrayList(java.util.ArrayList)

Example 38 with Aggregation

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

the class ScriptedMetricIT method testInitMapCombineReduceWithParamsAndReduceParams.

public void testInitMapCombineReduceWithParamsAndReduceParams() {
    Map<String, Object> varsMap = new HashMap<>();
    varsMap.put("multiplier", 1);
    Map<String, Object> params = new HashMap<>();
    params.put("vars", varsMap);
    Map<String, Object> reduceParams = new HashMap<>();
    reduceParams.put("multiplier", 4);
    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, "multiplied sum all states (lists) values as a new aggregation", reduceParams);
    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 * 12));
}
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 39 with Aggregation

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

the class ScriptedMetricIT method testInitMapCombineReduceWithParamsStored.

public void testInitMapCombineReduceWithParamsStored() {
    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.STORED, null, "initScript_stored", Collections.emptyMap())).mapScript(new Script(ScriptType.STORED, null, "mapScript_stored", Collections.emptyMap())).combineScript(new Script(ScriptType.STORED, null, "combineScript_stored", Collections.emptyMap())).reduceScript(new Script(ScriptType.STORED, null, "reduceScript_stored", 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(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 40 with Aggregation

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

the class ScriptedMetricIT method testMap.

public void testMap() {
    Script mapScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "state['count'] = 1", Collections.emptyMap());
    Script combineScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "no-op aggregation", Collections.emptyMap());
    Script reduceScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "no-op list aggregation", Collections.emptyMap());
    SearchResponse response = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(scriptedMetric("scripted").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(getNumShards("idx").numPrimaries));
    int numShardsRun = 0;
    for (Object object : aggregationList) {
        assertThat(object, notNullValue());
        assertThat(object, instanceOf(Map.class));
        Map<?, ?> map = (Map<?, ?>) object;
        assertThat(map.size(), lessThanOrEqualTo(1));
        if (map.size() == 1) {
            assertThat(map.get("count"), notNullValue());
            assertThat(map.get("count"), instanceOf(Number.class));
            assertThat(map.get("count"), equalTo((Number) 1));
            numShardsRun++;
        }
    }
    // We don't know how many shards will have documents but we need to make
    // sure that at least one shard ran the map script
    assertThat(numShardsRun, greaterThan(0));
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) Script(org.opensearch.script.Script) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

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