use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class MoreExpressionTests method testSparseField.
public void testSparseField() throws Exception {
ElasticsearchAssertions.assertAcked(prepareCreate("test").addMapping("doc", "x", "type=long", "y", "type=long"));
ensureGreen("test");
indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("x", 4), client().prepareIndex("test", "doc", "2").setSource("y", 2));
SearchResponse rsp = buildRequest("doc['x'] + 1").get();
ElasticsearchAssertions.assertSearchResponse(rsp);
SearchHits hits = rsp.getHits();
assertEquals(2, rsp.getHits().getTotalHits());
assertEquals(5.0, hits.getAt(0).field("foo").getValue(), 0.0D);
assertEquals(1.0, hits.getAt(1).field("foo").getValue(), 0.0D);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class MoreExpressionTests method testPipelineAggregationScript.
// test to make sure expressions are allowed to be used for reduce in pipeline aggregations
public void testPipelineAggregationScript() throws Exception {
createIndex("agg_index");
ensureGreen("agg_index");
indexRandom(true, client().prepareIndex("agg_index", "doc", "1").setSource("one", 1.0, "two", 2.0, "three", 3.0, "four", 4.0), client().prepareIndex("agg_index", "doc", "2").setSource("one", 2.0, "two", 2.0, "three", 3.0, "four", 4.0), client().prepareIndex("agg_index", "doc", "3").setSource("one", 3.0, "two", 2.0, "three", 3.0, "four", 4.0), client().prepareIndex("agg_index", "doc", "4").setSource("one", 4.0, "two", 2.0, "three", 3.0, "four", 4.0), client().prepareIndex("agg_index", "doc", "5").setSource("one", 5.0, "two", 2.0, "three", 3.0, "four", 4.0));
SearchResponse response = client().prepareSearch("agg_index").addAggregation(histogram("histogram").field("one").interval(2).subAggregation(sum("twoSum").field("two")).subAggregation(sum("threeSum").field("three")).subAggregation(sum("fourSum").field("four")).subAggregation(bucketScript("totalSum", new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "_value0 + _value1 + _value2", Collections.emptyMap()), "twoSum", "threeSum", "fourSum"))).execute().actionGet();
Histogram histogram = response.getAggregations().get("histogram");
assertThat(histogram, notNullValue());
assertThat(histogram.getName(), equalTo("histogram"));
List<Histogram.Bucket> buckets = histogram.getBuckets();
for (int bucketCount = 0; bucketCount < buckets.size(); ++bucketCount) {
Histogram.Bucket bucket = buckets.get(bucketCount);
if (bucket.getDocCount() == 1) {
SimpleValue seriesArithmetic = bucket.getAggregations().get("totalSum");
assertThat(seriesArithmetic, notNullValue());
double seriesArithmeticValue = seriesArithmetic.value();
assertEquals(9.0, seriesArithmeticValue, 0.001);
} else if (bucket.getDocCount() == 2) {
SimpleValue seriesArithmetic = bucket.getAggregations().get("totalSum");
assertThat(seriesArithmetic, notNullValue());
double seriesArithmeticValue = seriesArithmetic.value();
assertEquals(18.0, seriesArithmeticValue, 0.001);
} else {
fail("Incorrect number of documents in a bucket in the histogram.");
}
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class MoreExpressionTests method testBoolean.
public void testBoolean() throws Exception {
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("vip").field("type", "boolean");
xContentBuilder.endObject().endObject().endObject().endObject();
assertAcked(prepareCreate("test").addMapping("type1", xContentBuilder));
ensureGreen();
indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("price", 1.0, "vip", true), client().prepareIndex("test", "doc", "2").setSource("price", 2.0, "vip", false), client().prepareIndex("test", "doc", "3").setSource("price", 2.0, "vip", false));
// access .value
SearchResponse rsp = buildRequest("doc['vip'].value").get();
assertSearchResponse(rsp);
assertEquals(3, rsp.getHits().getTotalHits());
assertEquals(1.0D, rsp.getHits().getAt(0).field("foo").getValue(), 1.0D);
assertEquals(0.0D, rsp.getHits().getAt(1).field("foo").getValue(), 1.0D);
assertEquals(0.0D, rsp.getHits().getAt(2).field("foo").getValue(), 1.0D);
// access .empty
rsp = buildRequest("doc['vip'].empty ? 1 : 0").get();
assertSearchResponse(rsp);
assertEquals(3, rsp.getHits().getTotalHits());
assertEquals(0.0D, rsp.getHits().getAt(0).field("foo").getValue(), 1.0D);
assertEquals(0.0D, rsp.getHits().getAt(1).field("foo").getValue(), 1.0D);
assertEquals(1.0D, rsp.getHits().getAt(2).field("foo").getValue(), 1.0D);
// ternary operator
// vip's have a 50% discount
rsp = buildRequest("doc['vip'] ? doc['price']/2 : doc['price']").get();
assertSearchResponse(rsp);
assertEquals(3, rsp.getHits().getTotalHits());
assertEquals(0.5D, rsp.getHits().getAt(0).field("foo").getValue(), 1.0D);
assertEquals(2.0D, rsp.getHits().getAt(1).field("foo").getValue(), 1.0D);
assertEquals(2.0D, rsp.getHits().getAt(2).field("foo").getValue(), 1.0D);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class MoreExpressionTests method testSpecialValueVariable.
public void testSpecialValueVariable() throws Exception {
// i.e. _value for aggregations
createIndex("test");
ensureGreen("test");
indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("x", 5, "y", 1.2), client().prepareIndex("test", "doc", "2").setSource("x", 10, "y", 1.4), client().prepareIndex("test", "doc", "3").setSource("x", 13, "y", 1.8));
SearchRequestBuilder req = client().prepareSearch().setIndices("test");
req.setQuery(QueryBuilders.matchAllQuery()).addAggregation(AggregationBuilders.stats("int_agg").field("x").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "_value * 3", Collections.emptyMap()))).addAggregation(AggregationBuilders.stats("double_agg").field("y").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "_value - 1.1", Collections.emptyMap()))).addAggregation(// specifically to test a script w/o _value
AggregationBuilders.stats("const_agg").field("x").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "3.0", Collections.emptyMap())));
SearchResponse rsp = req.get();
assertEquals(3, rsp.getHits().getTotalHits());
Stats stats = rsp.getAggregations().get("int_agg");
assertEquals(39.0, stats.getMax(), 0.0001);
assertEquals(15.0, stats.getMin(), 0.0001);
stats = rsp.getAggregations().get("double_agg");
assertEquals(0.7, stats.getMax(), 0.0001);
assertEquals(0.1, stats.getMin(), 0.0001);
stats = rsp.getAggregations().get("const_agg");
assertThat(stats.getMax(), equalTo(3.0));
assertThat(stats.getMin(), equalTo(3.0));
assertThat(stats.getAvg(), equalTo(3.0));
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchTemplateIT method testIndexedTemplate.
public void testIndexedTemplate() throws Exception {
assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("1a").setContent(new BytesArray("{" + "\"template\":{" + " \"query\":{" + " \"match\":{" + " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}" + "}"), XContentType.JSON));
assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("2").setContent(new BytesArray("{" + "\"template\":{" + " \"query\":{" + " \"match\":{" + " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}" + "}"), XContentType.JSON));
assertAcked(client().admin().cluster().preparePutStoredScript().setLang(MustacheScriptEngineService.NAME).setId("3").setContent(new BytesArray("{" + "\"template\":{" + " \"match\":{" + " \"theField\" : \"{{fieldParam}}\"}" + " }" + "}"), XContentType.JSON));
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
bulkRequestBuilder.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
bulkRequestBuilder.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}", XContentType.JSON));
bulkRequestBuilder.get();
client().admin().indices().prepareRefresh().get();
Map<String, Object> templateParams = new HashMap<>();
templateParams.put("fieldParam", "foo");
SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest().indices("test").types("type")).setScript("1a").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get();
assertHitCount(searchResponse.getResponse(), 4);
expectThrows(IllegalArgumentException.class, () -> new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest().indices("test").types("type")).setScript("/template_index/mustache/1000").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get());
expectThrows(IllegalArgumentException.class, () -> new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest().indices("test").types("type")).setScript("/myindex/mustache/1").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get());
templateParams.put("fieldParam", "bar");
searchResponse = new SearchTemplateRequestBuilder(client()).setRequest(new SearchRequest("test").types("type")).setScript("/mustache/2").setScriptType(ScriptType.STORED).setScriptParams(templateParams).get();
assertHitCount(searchResponse.getResponse(), 1);
assertWarnings("use of </lang/id> [/mustache/2] for looking up" + " stored scripts/templates has been deprecated, use only <id> [2] instead");
Map<String, Object> vars = new HashMap<>();
vars.put("fieldParam", "bar");
TemplateQueryBuilder builder = new TemplateQueryBuilder("3", ScriptType.STORED, vars);
SearchResponse sr = client().prepareSearch().setQuery(builder).execute().actionGet();
assertHitCount(sr, 1);
assertWarnings("[template] query is deprecated, use search template api instead");
}
Aggregations