Merge branch 'pr/update-docs-51'

This commit is contained in:
David Pilato 2016-12-16 16:22:07 +01:00
commit 43f9cd1fd4
3 changed files with 22 additions and 33 deletions

View File

@ -12,43 +12,32 @@ Here is an example on how to create the aggregation request:
[source,java]
--------------------------------------------------
ScriptedMetricAggregationBuilder aggregation = AggregationBuilders
.scriptedMetric("agg")
.initScript(new Script("_agg['heights'] = []"))
.mapScript(new Script("if (doc['gender'].value == \"male\") " +
"{ _agg.heights.add(doc['height'].value) } " +
"else " +
"{ _agg.heights.add(-1 * doc['height'].value) }"));
.scriptedMetric("agg")
.initScript(new Script("params._agg.heights = []"))
.mapScript(new Script("params._agg.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"));
--------------------------------------------------
You can also specify a `combine` script which will be executed on each shard:
[source,java]
--------------------------------------------------
ScriptedMetricAggregationBuilder aggregation =
AggregationBuilders
.scriptedMetric("agg")
.initScript(new Script("_agg['heights'] = []"))
.mapScript(new Script("if (doc['gender'].value == \"male\") " +
"{ _agg.heights.add(doc['height'].value) } " +
"else " +
"{ _agg.heights.add(-1 * doc['height'].value) }"))
.combineScript(new Script("heights_sum = 0; for (t in _agg.heights) { heights_sum += t }; return heights_sum"));
ScriptedMetricAggregationBuilder aggregation = AggregationBuilders
.scriptedMetric("agg")
.initScript(new Script("params._agg.heights = []"))
.mapScript(new Script("params._agg.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"))
.combineScript(new Script("double heights_sum = 0.0; for (t in params._agg.heights) { heights_sum += t } return heights_sum"));
--------------------------------------------------
You can also specify a `reduce` script which will be executed on the node which gets the request:
[source,java]
--------------------------------------------------
ScriptedMetricAggregationBuilder aggregation =
AggregationBuilders
.scriptedMetric("agg")
.initScript(new Script("_agg['heights'] = []"))
.mapScript(new Script("if (doc['gender'].value == \"male\") " +
"{ _agg.heights.add(doc['height'].value) } " +
"else " +
"{ _agg.heights.add(-1 * doc['height'].value) }"))
.combineScript(new Script("heights_sum = 0; for (t in _agg.heights) { heights_sum += t }; return heights_sum"))
.reduceScript(new Script("heights_sum = 0; for (a in _aggs) { heights_sum += a }; return heights_sum"));
ScriptedMetricAggregationBuilder aggregation = AggregationBuilders
.scriptedMetric("agg")
.initScript(new Script("params._agg.heights = []"))
.mapScript(new Script("params._agg.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"))
.combineScript(new Script("double heights_sum = 0.0; for (t in params._agg.heights) { heights_sum += t } return heights_sum"))
.reduceScript(new Script("double heights_sum = 0.0; for (a in params._aggs) { heights_sum += a } return heights_sum"));
--------------------------------------------------

View File

@ -25,13 +25,13 @@ You can use it then with:
--------------------------------------------------
QueryBuilder qb = scriptQuery(
new Script(
"myscript", <1>
ScriptType.FILE, <2>
"painless", <3>
ScriptType.FILE, <1>
"painless", <2>
"myscript", <3>
Collections.singletonMap("param1", 5)) <4>
);
--------------------------------------------------
<1> Script name
<2> Script type: either `ScriptType.FILE`, `ScriptType.INLINE` or `ScriptType.INDEXED`
<3> Scripting engine
<1> Script type: either `ScriptType.FILE`, `ScriptType.INLINE` or `ScriptType.INDEXED`
<2> Scripting engine
<3> Script name
<4> Parameters as a `Map` of `<String, Object>`

View File

@ -216,7 +216,7 @@ To execute a stored templates, use `ScriptService.ScriptType.STORED`:
--------------------------------------------------
SearchResponse sr = new SearchTemplateRequestBuilder(client)
.setScript("template_gender") <1>
.setScriptType(ScriptService.ScriptType.STORED) <2>
.setScriptType(ScriptType.STORED) <2>
.setScriptParams(template_params) <3>
.setRequest(new SearchRequest()) <4>
.get() <5>
@ -241,7 +241,7 @@ sr = new SearchTemplateRequestBuilder(client)
" }\n" +
" }\n" +
"}")
.setScriptType(ScriptService.ScriptType.INLINE) <2>
.setScriptType(ScriptType.INLINE) <2>
.setScriptParams(template_params) <3>
.setRequest(new SearchRequest()) <4>
.get() <5>