HasChild.php 1.9 KB

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