Search in sources :

Example 91 with Script

use of org.elasticsearch.script.Script in project elasticsearch by elastic.

the class ScriptedMetricIT method testInitMapCombineWithParams.

public void testInitMapCombineWithParams() {
    Map<String, Object> varsMap = new HashMap<>();
    varsMap.put("multiplier", 1);
    Map<String, Object> params = new HashMap<>();
    params.put("_agg", new ArrayList<>());
    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, "_agg.add(vars.multiplier)", Collections.emptyMap());
    Script combineScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum agg 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)).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits(), 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(List.class));
        List<?> list = (List<?>) object;
        for (Object o : list) {
            assertThat(o, notNullValue());
            assertThat(o, instanceOf(Number.class));
            Number numberValue = (Number) o;
            // A particular shard may not have any documents stored on it so
            // we have to assume the lower bound may be 0. The check at the
            // bottom of the test method will make sure the count is correct
            assertThat(numberValue.longValue(), allOf(greaterThanOrEqualTo(0L), lessThanOrEqualTo(numDocs * 3)));
            totalCount += numberValue.longValue();
        }
    }
    assertThat(totalCount, equalTo(numDocs * 3));
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ScriptedMetric(org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Aggregation(org.elasticsearch.search.aggregations.Aggregation) ArrayList(java.util.ArrayList) List(java.util.List)

Example 92 with Script

use of org.elasticsearch.script.Script in project elasticsearch by elastic.

the class ScriptedMetricIT method testInitMapReduceWithParams.

public void testInitMapReduceWithParams() {
    Map<String, Object> varsMap = new HashMap<>();
    varsMap.put("multiplier", 1);
    Map<String, Object> params = new HashMap<>();
    params.put("_agg", new ArrayList<>());
    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, "_agg.add(vars.multiplier)", Collections.emptyMap());
    Script reduceScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum aggs of agg values as a new aggregation", Collections.emptyMap());
    SearchResponse response = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(scriptedMetric("scripted").params(params).initScript(initScript).mapScript(mapScript).reduceScript(reduceScript)).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits(), 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 : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ScriptedMetric(org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Aggregation(org.elasticsearch.search.aggregations.Aggregation) ArrayList(java.util.ArrayList) List(java.util.List)

Example 93 with Script

use of org.elasticsearch.script.Script in project elasticsearch by elastic.

the class ScriptedMetricIT method testInitMapCombineReduceGetProperty.

@SuppressWarnings("rawtypes")
public void testInitMapCombineReduceGetProperty() throws Exception {
    Map<String, Object> varsMap = new HashMap<>();
    varsMap.put("multiplier", 1);
    Map<String, Object> params = new HashMap<>();
    params.put("_agg", new ArrayList<>());
    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, "_agg.add(vars.multiplier)", Collections.emptyMap());
    Script combineScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum agg values as a new aggregation", Collections.emptyMap());
    Script reduceScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum aggs of agg values as a new aggregation", Collections.emptyMap());
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(scriptedMetric("scripted").params(params).initScript(initScript).mapScript(mapScript).combineScript(combineScript).reduceScript(reduceScript))).get();
    assertSearchResponse(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(numDocs));
    Global global = searchResponse.getAggregations().get("global");
    assertThat(global, notNullValue());
    assertThat(global.getName(), equalTo("global"));
    assertThat(global.getDocCount(), equalTo(numDocs));
    assertThat(global.getAggregations(), notNullValue());
    assertThat(global.getAggregations().asMap().size(), equalTo(1));
    ScriptedMetric scriptedMetricAggregation = global.getAggregations().get("scripted");
    assertThat(scriptedMetricAggregation, notNullValue());
    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));
    assertThat((ScriptedMetric) global.getProperty("scripted"), sameInstance(scriptedMetricAggregation));
    assertThat((List) global.getProperty("scripted.value"), sameInstance((List) aggregationList));
    assertThat((List) scriptedMetricAggregation.getProperty("value"), sameInstance((List) aggregationList));
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ScriptedMetric(org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) ArrayList(java.util.ArrayList) List(java.util.List)

Example 94 with Script

use of org.elasticsearch.script.Script in project elasticsearch by elastic.

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("_agg", new ArrayList<>());
    params.put("vars", varsMap);
    Script mapScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_agg.add(vars.multiplier)", Collections.emptyMap());
    Script combineScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum agg values as a new aggregation", Collections.emptyMap());
    Script reduceScript = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "sum aggs of agg 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(), 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 : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ScriptedMetric(org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Aggregation(org.elasticsearch.search.aggregations.Aggregation) ArrayList(java.util.ArrayList) List(java.util.List)

Example 95 with Script

use of org.elasticsearch.script.Script in project elasticsearch by elastic.

the class StatsIT method testMultiValuedFieldWithValueScriptWithParams.

@Override
public void testMultiValuedFieldWithValueScriptWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("dec", 1);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(stats("stats").field("values").script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value - dec", params))).get();
    assertShardExecutionState(searchResponse, 0);
    assertHitCount(searchResponse, 10);
    Stats stats = searchResponse.getAggregations().get("stats");
    assertThat(stats, notNullValue());
    assertThat(stats.getName(), equalTo("stats"));
    assertThat(stats.getAvg(), equalTo((double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11) / 20));
    assertThat(stats.getMin(), equalTo(1.0));
    assertThat(stats.getMax(), equalTo(11.0));
    assertThat(stats.getSum(), equalTo((double) 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11));
    assertThat(stats.getCount(), equalTo(20L));
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Script (org.elasticsearch.script.Script)307 SearchResponse (org.elasticsearch.action.search.SearchResponse)223 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)206 HashMap (java.util.HashMap)91 ExecutableScript (org.elasticsearch.script.ExecutableScript)52 CompiledScript (org.elasticsearch.script.CompiledScript)46 SearchScript (org.elasticsearch.script.SearchScript)41 ArrayList (java.util.ArrayList)32 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)32 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)26 LeafSearchScript (org.elasticsearch.script.LeafSearchScript)25 Matchers.containsString (org.hamcrest.Matchers.containsString)23 List (java.util.List)22 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)22 Map (java.util.Map)18 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)18 PercentileRanks (org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks)18 Percentiles (org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles)18 XContentParser (org.elasticsearch.common.xcontent.XContentParser)16 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)16