ReverseNested.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Elastica\Aggregation;
  3. /**
  4. * Reversed Nested Aggregation.
  5. *
  6. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html
  7. */
  8. class ReverseNested extends AbstractAggregation
  9. {
  10. /**
  11. * @param string $name The name of this aggregation
  12. * @param string $path Optional path to the nested object for this aggregation. Defaults to the root of the main document.
  13. */
  14. public function __construct($name, $path = null)
  15. {
  16. parent::__construct($name);
  17. if (null !== $path) {
  18. $this->setPath($path);
  19. }
  20. }
  21. /**
  22. * Set the nested path for this aggregation.
  23. *
  24. * @param string $path
  25. *
  26. * @return $this
  27. */
  28. public function setPath($path)
  29. {
  30. return $this->setParam('path', $path);
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function toArray()
  36. {
  37. $array = parent::toArray();
  38. // ensure we have an object for the reverse_nested key.
  39. // if we don't have a path, then this would otherwise get encoded as an empty array, which is invalid.
  40. $array['reverse_nested'] = (object) $array['reverse_nested'];
  41. return $array;
  42. }
  43. }