1
0

scripted-metric-aggregation.asciidoc 4.3 KB

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