scripted-metric-aggregation.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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("_agg['heights'] = []"))
  13. .mapScript(new Script("if (doc['gender'].value == \"male\") " +
  14. "{ _agg.heights.add(doc['height'].value) } " +
  15. "else " +
  16. "{ _agg.heights.add(-1 * doc['height'].value) }"));
  17. --------------------------------------------------
  18. You can also specify a `combine` script which will be executed on each shard:
  19. [source,java]
  20. --------------------------------------------------
  21. ScriptedMetricAggregationBuilder aggregation =
  22. AggregationBuilders
  23. .scriptedMetric("agg")
  24. .initScript(new Script("_agg['heights'] = []"))
  25. .mapScript(new Script("if (doc['gender'].value == \"male\") " +
  26. "{ _agg.heights.add(doc['height'].value) } " +
  27. "else " +
  28. "{ _agg.heights.add(-1 * doc['height'].value) }"))
  29. .combineScript(new Script("heights_sum = 0; for (t in _agg.heights) { heights_sum += t }; return heights_sum"));
  30. --------------------------------------------------
  31. You can also specify a `reduce` script which will be executed on the node which gets the request:
  32. [source,java]
  33. --------------------------------------------------
  34. ScriptedMetricAggregationBuilder aggregation =
  35. AggregationBuilders
  36. .scriptedMetric("agg")
  37. .initScript(new Script("_agg['heights'] = []"))
  38. .mapScript(new Script("if (doc['gender'].value == \"male\") " +
  39. "{ _agg.heights.add(doc['height'].value) } " +
  40. "else " +
  41. "{ _agg.heights.add(-1 * doc['height'].value) }"))
  42. .combineScript(new Script("heights_sum = 0; for (t in _agg.heights) { heights_sum += t }; return heights_sum"))
  43. .reduceScript(new Script("heights_sum = 0; for (a in _aggs) { heights_sum += a }; return heights_sum"));
  44. --------------------------------------------------
  45. ===== Use aggregation response
  46. Import Aggregation definition classes:
  47. [source,java]
  48. --------------------------------------------------
  49. import org.elasticsearch.search.aggregations.bucket.terms.Terms;
  50. import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
  51. --------------------------------------------------
  52. [source,java]
  53. --------------------------------------------------
  54. // sr is here your SearchResponse object
  55. ScriptedMetric agg = sr.getAggregations().get("agg");
  56. Object scriptedResult = agg.aggregation();
  57. logger.info("scriptedResult [{}]", scriptedResult);
  58. --------------------------------------------------
  59. Note that the result depends on the script you built.
  60. For the first example, this will basically produce:
  61. [source,text]
  62. --------------------------------------------------
  63. scriptedResult object [ArrayList]
  64. scriptedResult [ {
  65. "heights" : [ 1.122218480146643, -1.8148918111233887, -1.7626731575142909, ... ]
  66. }, {
  67. "heights" : [ -0.8046067304119863, -2.0785486707864553, -1.9183567430207953, ... ]
  68. }, {
  69. "heights" : [ 2.092635728868694, 1.5697545960886536, 1.8826954461968808, ... ]
  70. }, {
  71. "heights" : [ -2.1863201099468403, 1.6328549117346856, -1.7078288405893842, ... ]
  72. }, {
  73. "heights" : [ 1.6043904836424177, -2.0736538674414025, 0.9898266674373053, ... ]
  74. } ]
  75. --------------------------------------------------
  76. The second example will produce:
  77. [source,text]
  78. --------------------------------------------------
  79. scriptedResult object [ArrayList]
  80. scriptedResult [-41.279615707402876,
  81. -60.88007362339038,
  82. 38.823270659734256,
  83. 14.840192739445632,
  84. 11.300902755741326]
  85. --------------------------------------------------
  86. The last example will produce:
  87. [source,text]
  88. --------------------------------------------------
  89. scriptedResult object [Double]
  90. scriptedResult [2.171917696507009]
  91. --------------------------------------------------