use of org.elasticsearch.script.Script in project elasticsearch by elastic.
the class HistogramIT method testScriptSingleValue.
public void testScriptSingleValue() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['l_value'].value", emptyMap())).interval(interval)).execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(numValueBuckets));
for (int i = 0; i < numValueBuckets; ++i) {
Histogram.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) i * interval));
assertThat(bucket.getDocCount(), equalTo(valueCounts[i]));
}
}
use of org.elasticsearch.script.Script in project elasticsearch by elastic.
the class HistogramIT method testSingleValuedFieldWithValueScript.
public void testSingleValuedFieldWithValueScript() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_value + 1", emptyMap())).interval(interval)).execute().actionGet();
assertSearchResponse(response);
final int numBuckets = (numDocs + 1) / interval - 2 / interval + 1;
final long[] counts = new long[(numDocs + 1) / interval + 1];
for (int i = 0; i < numDocs; ++i) {
++counts[(i + 2) / interval];
}
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(numBuckets));
for (int i = 0; i < numBuckets; i++) {
Histogram.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
int key = ((2 / interval) + i) * interval;
assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) key));
assertThat(bucket.getDocCount(), equalTo(counts[key / interval]));
}
}
use of org.elasticsearch.script.Script in project elasticsearch by elastic.
the class HistogramIT method testScriptMultiValued.
public void testScriptMultiValued() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['l_values']", emptyMap())).interval(interval)).execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(numValuesBuckets));
for (int i = 0; i < numValuesBuckets; ++i) {
Histogram.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) i * interval));
assertThat(bucket.getDocCount(), equalTo(valuesCounts[i]));
}
}
use of org.elasticsearch.script.Script in project elasticsearch by elastic.
the class RangeIT method testScriptSingleValue.
/*
[2, 3]
[3, 4]
[4, 5]
[5, 6]
[6, 7]
[7, 8j
[8, 9]
[9, 10]
[10, 11]
[11, 12]
r1: 2
r2: 3, 3, 4, 4, 5, 5
r3: 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12
*/
public void testScriptSingleValue() throws Exception {
Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['" + SINGLE_VALUED_FIELD_NAME + "'].value", Collections.emptyMap());
SearchResponse response = client().prepareSearch("idx").addAggregation(range("range").script(script).addUnboundedTo(3).addRange(3, 6).addUnboundedFrom(6)).get();
assertSearchResponse(response);
Range range = response.getAggregations().get("range");
assertThat(range, notNullValue());
assertThat(range.getName(), equalTo("range"));
List<? extends Bucket> buckets = range.getBuckets();
assertThat(range.getBuckets().size(), equalTo(3));
Range.Bucket bucket = buckets.get(0);
assertThat(bucket, notNullValue());
assertThat(bucket.getKey(), equalTo("*-3.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(3.0));
assertThat(bucket.getFromAsString(), nullValue());
assertThat(bucket.getToAsString(), equalTo("3.0"));
assertThat(bucket.getDocCount(), equalTo(2L));
bucket = buckets.get(1);
assertThat(bucket, notNullValue());
assertThat(bucket.getKey(), equalTo("3.0-6.0"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(3.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(6.0));
assertThat(bucket.getFromAsString(), equalTo("3.0"));
assertThat(bucket.getToAsString(), equalTo("6.0"));
assertThat(bucket.getDocCount(), equalTo(3L));
bucket = buckets.get(2);
assertThat(bucket, notNullValue());
assertThat(bucket.getKey(), equalTo("6.0-*"));
assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(6.0));
assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(bucket.getFromAsString(), equalTo("6.0"));
assertThat(bucket.getToAsString(), nullValue());
assertThat(bucket.getDocCount(), equalTo(numDocs - 5L));
}
use of org.elasticsearch.script.Script in project elasticsearch by elastic.
the class SignificantTermsSignificanceScoreIT method getScriptSignificanceHeuristic.
private ScriptHeuristic getScriptSignificanceHeuristic() throws IOException {
Script script;
if (randomBoolean()) {
Map<String, Object> params = new HashMap<>();
params.put("param", randomIntBetween(1, 100));
script = new Script(ScriptType.INLINE, "native", "native_significance_score_script_with_params", params);
} else {
script = new Script(ScriptType.INLINE, "native", "native_significance_score_script_no_params", Collections.emptyMap());
}
return new ScriptHeuristic(script);
}
Aggregations