Percentiles.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Elastica\Aggregation;
  3. /**
  4. * Class Percentiles.
  5. *
  6. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html
  7. */
  8. class Percentiles extends AbstractSimpleAggregation
  9. {
  10. /**
  11. * @param string $name the name of this aggregation
  12. * @param string $field the field on which to perform this aggregation
  13. */
  14. public function __construct($name, $field = null)
  15. {
  16. parent::__construct($name);
  17. if (!is_null($field)) {
  18. $this->setField($field);
  19. }
  20. }
  21. /**
  22. * Set compression parameter.
  23. *
  24. * @param float $value
  25. *
  26. * @return Percentiles $this
  27. */
  28. public function setCompression(float $value): Percentiles
  29. {
  30. $compression = ['compression' => $value];
  31. return $this->setParam('tdigest', $compression);
  32. }
  33. /**
  34. * Set hdr parameter.
  35. *
  36. * @param string $key
  37. * @param float $value
  38. *
  39. * @return Percentiles $this
  40. */
  41. public function setHdr(string $key, float $value): Percentiles
  42. {
  43. $compression = [$key => $value];
  44. return $this->setParam('hdr', $compression);
  45. }
  46. /**
  47. * the keyed flag is set to true which associates a unique string
  48. * key with each bucket and returns the ranges as a hash
  49. * rather than an array.
  50. *
  51. * @param bool $keyed
  52. *
  53. * @return Percentiles $this
  54. */
  55. public function setKeyed(bool $keyed = true): Percentiles
  56. {
  57. return $this->setParam('keyed', $keyed);
  58. }
  59. /**
  60. * Set which percents must be returned.
  61. *
  62. * @param float[] $percents
  63. *
  64. * @return Percentiles $this
  65. */
  66. public function setPercents(array $percents): Percentiles
  67. {
  68. return $this->setParam('percents', $percents);
  69. }
  70. /**
  71. * Add yet another percent to result.
  72. *
  73. * @param float $percent
  74. *
  75. * @return Percentiles $this
  76. */
  77. public function addPercent(float $percent): Percentiles
  78. {
  79. return $this->addParam('percents', $percent);
  80. }
  81. /**
  82. * Defines how documents that are missing a value should
  83. * be treated.
  84. *
  85. * @param float $missing
  86. *
  87. * @return Percentiles
  88. */
  89. public function setMissing(float $missing): Percentiles
  90. {
  91. return $this->setParam('missing', $missing);
  92. }
  93. }