scripted-metric-aggregation.asciidoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. [[java-aggs-metrics-scripted-metric]]
  2. ==== Scripted Metric Aggregation
  3. Here is how you can use
  4. {ref}/search-aggregations-metrics-scripted-metric-aggregation.html[Scripted Metric Aggregation]
  5. with Java API.
  6. ===== Prepare aggregation request
  7. Here is an example on how to create the aggregation request:
  8. [source,java]
  9. --------------------------------------------------
  10. ScriptedMetricAggregationBuilder aggregation = AggregationBuilders
  11. .scriptedMetric("agg")
  12. .initScript(new Script("params._agg.heights = []"))
  13. .mapScript(new Script("params._agg.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"));
  14. --------------------------------------------------
  15. You can also specify a `combine` script which will be executed on each shard:
  16. [source,java]
  17. --------------------------------------------------
  18. ScriptedMetricAggregationBuilder aggregation = AggregationBuilders
  19. .scriptedMetric("agg")
  20. .initScript(new Script("params._agg.heights = []"))
  21. .mapScript(new Script("params._agg.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"))
  22. .combineScript(new Script("double heights_sum = 0.0; for (t in params._agg.heights) { heights_sum += t } return heights_sum"));
  23. --------------------------------------------------
  24. You can also specify a `reduce` script which will be executed on the node which gets the request:
  25. [source,java]
  26. --------------------------------------------------
  27. ScriptedMetricAggregationBuilder aggregation = AggregationBuilders
  28. .scriptedMetric("agg")
  29. .initScript(new Script("params._agg.heights = []"))
  30. .mapScript(new Script("params._agg.heights.add(doc.gender.value == 'male' ? doc.height.value : -1.0 * doc.height.value)"))
  31. .combineScript(new Script("double heights_sum = 0.0; for (t in params._agg.heights) { heights_sum += t } return heights_sum"))
  32. .reduceScript(new Script("double heights_sum = 0.0; for (a in params._aggs) { heights_sum += a } return heights_sum"));
  33. --------------------------------------------------
  34. ===== Use aggregation response
  35. Import Aggregation definition classes:
  36. [source,java]
  37. --------------------------------------------------
  38. import org.elasticsearch.search.aggregations.bucket.terms.Terms;
  39. import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
  40. --------------------------------------------------
  41. [source,java]
  42. --------------------------------------------------
  43. // sr is here your SearchResponse object
  44. ScriptedMetric agg = sr.getAggregations().get("agg");
  45. Object scriptedResult = agg.aggregation();
  46. logger.info("scriptedResult [{}]", scriptedResult);
  47. --------------------------------------------------
  48. Note that the result depends on the script you built.
  49. For the first example, this will basically produce:
  50. [source,text]
  51. --------------------------------------------------
  52. scriptedResult object [ArrayList]
  53. scriptedResult [ {
  54. "heights" : [ 1.122218480146643, -1.8148918111233887, -1.7626731575142909, ... ]
  55. }, {
  56. "heights" : [ -0.8046067304119863, -2.0785486707864553, -1.9183567430207953, ... ]
  57. }, {
  58. "heights" : [ 2.092635728868694, 1.5697545960886536, 1.8826954461968808, ... ]
  59. }, {
  60. "heights" : [ -2.1863201099468403, 1.6328549117346856, -1.7078288405893842, ... ]
  61. }, {
  62. "heights" : [ 1.6043904836424177, -2.0736538674414025, 0.9898266674373053, ... ]
  63. } ]
  64. --------------------------------------------------
  65. The second example will produce:
  66. [source,text]
  67. --------------------------------------------------
  68. scriptedResult object [ArrayList]
  69. scriptedResult [-41.279615707402876,
  70. -60.88007362339038,
  71. 38.823270659734256,
  72. 14.840192739445632,
  73. 11.300902755741326]
  74. --------------------------------------------------
  75. The last example will produce:
  76. [source,text]
  77. --------------------------------------------------
  78. scriptedResult object [Double]
  79. scriptedResult [2.171917696507009]
  80. --------------------------------------------------