Search in sources :

Example 81 with Script

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

the class CardinalityIT method testSingleValuedNumericValueScript.

public void testSingleValuedNumericValueScript() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(cardinality("cardinality").precisionThreshold(precisionThreshold).field(singleNumericField()).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_value", emptyMap()))).execute().actionGet();
    assertSearchResponse(response);
    Cardinality count = response.getAggregations().get("cardinality");
    assertThat(count, notNullValue());
    assertThat(count.getName(), equalTo("cardinality"));
    assertCount(count, numDocs);
}
Also used : Script(org.elasticsearch.script.Script) Cardinality(org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 82 with Script

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

the class CardinalityIT method testMultiValuedStringValueScript.

public void testMultiValuedStringValueScript() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(cardinality("cardinality").precisionThreshold(precisionThreshold).field("str_values").script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_value", emptyMap()))).execute().actionGet();
    assertSearchResponse(response);
    Cardinality count = response.getAggregations().get("cardinality");
    assertThat(count, notNullValue());
    assertThat(count.getName(), equalTo("cardinality"));
    assertCount(count, numDocs * 2);
}
Also used : Script(org.elasticsearch.script.Script) Cardinality(org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 83 with Script

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

the class PercentileRanksTests method createTestAggregatorBuilder.

@Override
protected PercentileRanksAggregationBuilder createTestAggregatorBuilder() {
    PercentileRanksAggregationBuilder factory = new PercentileRanksAggregationBuilder(randomAsciiOfLengthBetween(1, 20));
    if (randomBoolean()) {
        factory.keyed(randomBoolean());
    }
    int valuesSize = randomIntBetween(1, 20);
    double[] values = new double[valuesSize];
    for (int i = 0; i < valuesSize; i++) {
        values[i] = randomDouble() * 100;
    }
    factory.values(values);
    if (randomBoolean()) {
        factory.numberOfSignificantValueDigits(randomIntBetween(0, 5));
    }
    if (randomBoolean()) {
        factory.compression(randomIntBetween(1, 50000));
    }
    String field = randomNumericField();
    int randomFieldBranch = randomInt(3);
    switch(randomFieldBranch) {
        case 0:
            factory.field(field);
            break;
        case 1:
            factory.field(field);
            factory.script(new Script("_value + 1"));
            break;
        case 2:
            factory.script(new Script("doc[" + field + "] + 1"));
            break;
    }
    if (randomBoolean()) {
        factory.missing("MISSING");
    }
    return factory;
}
Also used : Script(org.elasticsearch.script.Script) PercentileRanksAggregationBuilder(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanksAggregationBuilder)

Example 84 with Script

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

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("_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 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(), 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.elasticsearch.script.Script) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) HashMap(java.util.HashMap) Aggregations(org.elasticsearch.search.aggregations.Aggregations) 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) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) ArrayList(java.util.ArrayList) List(java.util.List)

Example 85 with Script

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

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("_agg", new ArrayList<>());
    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, "_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, "multiplied sum aggs of agg 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)).execute().actionGet();
    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 * 12));
}
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)

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