ScriptedMetric.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Elastica\Aggregation;
  3. /**
  4. * Class ScriptedMetric.
  5. *
  6. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
  7. */
  8. class ScriptedMetric extends AbstractAggregation
  9. {
  10. /**
  11. * @param string $name the name if this aggregation
  12. * @param string|null $initScript Executed prior to any collection of documents
  13. * @param string|null $mapScript Executed once per document collected
  14. * @param string|null $combineScript Executed once on each shard after document collection is complete
  15. * @param string|null $reduceScript Executed once on the coordinating node after all shards have returned their results
  16. */
  17. public function __construct($name, $initScript = null, $mapScript = null, $combineScript = null, $reduceScript = null)
  18. {
  19. parent::__construct($name);
  20. if ($initScript) {
  21. $this->setInitScript($initScript);
  22. }
  23. if ($mapScript) {
  24. $this->setMapScript($mapScript);
  25. }
  26. if ($combineScript) {
  27. $this->setCombineScript($combineScript);
  28. }
  29. if ($reduceScript) {
  30. $this->setReduceScript($reduceScript);
  31. }
  32. }
  33. /**
  34. * Executed once on each shard after document collection is complete.
  35. *
  36. * Allows the aggregation to consolidate the state returned from each shard.
  37. * If a combine_script is not provided the combine phase will return the aggregation variable.
  38. *
  39. * @param string $script
  40. *
  41. * @return $this
  42. */
  43. public function setCombineScript($script)
  44. {
  45. return $this->setParam('combine_script', $script);
  46. }
  47. /**
  48. * Executed prior to any collection of documents.
  49. *
  50. * Allows the aggregation to set up any initial state.
  51. *
  52. * @param string $script
  53. *
  54. * @return $this
  55. */
  56. public function setInitScript($script)
  57. {
  58. return $this->setParam('init_script', $script);
  59. }
  60. /**
  61. * Executed once per document collected.
  62. *
  63. * This is the only required script. If no combine_script is specified, the resulting state needs to be stored in
  64. * an object named _agg.
  65. *
  66. * @param string $script
  67. *
  68. * @return $this
  69. */
  70. public function setMapScript($script)
  71. {
  72. return $this->setParam('map_script', $script);
  73. }
  74. /**
  75. * Executed once on the coordinating node after all shards have returned their results.
  76. *
  77. * The script is provided with access to a variable _aggs which is an array of the result of the combine_script on
  78. * each shard. If a reduce_script is not provided the reduce phase will return the _aggs variable.
  79. *
  80. * @param string $script
  81. *
  82. * @return $this
  83. */
  84. public function setReduceScript($script)
  85. {
  86. return $this->setParam('reduce_script', $script);
  87. }
  88. }