HasParent.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Elastica\Query;
  3. use Elastica\Query as BaseQuery;
  4. /**
  5. * Returns child documents having parent docs matching the query.
  6. *
  7. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html
  8. */
  9. class HasParent extends AbstractQuery
  10. {
  11. /**
  12. * Construct HasChild Query.
  13. *
  14. * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query
  15. * @param string $type Parent document type
  16. */
  17. public function __construct($query, $type)
  18. {
  19. $this->setQuery($query);
  20. $this->setType($type);
  21. }
  22. /**
  23. * Sets query object.
  24. *
  25. * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query
  26. *
  27. * @return $this
  28. */
  29. public function setQuery($query)
  30. {
  31. return $this->setParam('query', BaseQuery::create($query));
  32. }
  33. /**
  34. * Set type of the parent document.
  35. *
  36. * @param string $type Parent document type
  37. *
  38. * @return $this
  39. */
  40. public function setType($type)
  41. {
  42. return $this->setParam('parent_type', $type);
  43. }
  44. /**
  45. * Sets the scope.
  46. *
  47. * @param string $scope Scope
  48. *
  49. * @return $this
  50. */
  51. public function setScope($scope)
  52. {
  53. return $this->setParam('_scope', $scope);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function toArray()
  59. {
  60. $array = parent::toArray();
  61. $baseName = $this->_getBaseName();
  62. if (isset($array[$baseName]['query'])) {
  63. $array[$baseName]['query'] = $array[$baseName]['query']['query'];
  64. }
  65. return $array;
  66. }
  67. }