InnerHits.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Elastica\Query;
  3. use Elastica\Script\AbstractScript;
  4. use Elastica\Script\ScriptFields;
  5. /**
  6. * Nested query.
  7. *
  8. * @author Guillaume Affringue <wamania@yahoo.fr>
  9. *
  10. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html
  11. */
  12. class InnerHits extends AbstractQuery
  13. {
  14. /**
  15. * @return array
  16. */
  17. public function toArray()
  18. {
  19. $array = parent::toArray();
  20. // if there are no params, it's ok, but ES will throw exception if json
  21. // will be like {"top_hits":[]} instead of {"top_hits":{}}
  22. if (empty($array['inner_hits'])) {
  23. $array['inner_hits'] = new \stdClass();
  24. }
  25. return $array['inner_hits'];
  26. }
  27. /**
  28. * The name to be used for the particular inner hit definition in the response.
  29. * Useful when multiple inner hits have been defined in a single search request.
  30. *
  31. * @param string $name
  32. *
  33. * @return $this
  34. */
  35. public function setName($name)
  36. {
  37. return $this->setParam('name', $name);
  38. }
  39. /**
  40. * The maximum number of inner matching hits to return per bucket. By default the top three matching hits are returned.
  41. *
  42. * @param int $size
  43. *
  44. * @return $this
  45. */
  46. public function setSize($size)
  47. {
  48. return $this->setParam('size', (int) $size);
  49. }
  50. /**
  51. * The offset from the first result you want to fetch.
  52. *
  53. * @param int $from
  54. *
  55. * @return $this
  56. */
  57. public function setFrom($from)
  58. {
  59. return $this->setParam('from', (int) $from);
  60. }
  61. /**
  62. * How the inner matching hits should be sorted. By default the hits are sorted by the score of the main query.
  63. *
  64. * @param array $sortArgs
  65. *
  66. * @return $this
  67. */
  68. public function setSort(array $sortArgs)
  69. {
  70. return $this->setParam('sort', $sortArgs);
  71. }
  72. /**
  73. * Allows to control how the _source field is returned with every hit.
  74. *
  75. * @param array|bool $params Fields to be returned or false to disable source
  76. *
  77. * @return $this
  78. */
  79. public function setSource($params)
  80. {
  81. return $this->setParam('_source', $params);
  82. }
  83. /**
  84. * Returns a version for each search hit.
  85. *
  86. * @param bool $version
  87. *
  88. * @return $this
  89. */
  90. public function setVersion($version)
  91. {
  92. return $this->setParam('version', (bool) $version);
  93. }
  94. /**
  95. * Enables explanation for each hit on how its score was computed.
  96. *
  97. * @param bool $explain
  98. *
  99. * @return $this
  100. */
  101. public function setExplain($explain)
  102. {
  103. return $this->setParam('explain', (bool) $explain);
  104. }
  105. /**
  106. * Set script fields.
  107. *
  108. * @param \Elastica\Script\ScriptFields $scriptFields
  109. *
  110. * @return $this
  111. */
  112. public function setScriptFields(ScriptFields $scriptFields)
  113. {
  114. return $this->setParam('script_fields', $scriptFields);
  115. }
  116. /**
  117. * Adds a Script to the aggregation.
  118. *
  119. * @param string $name
  120. * @param \Elastica\Script\AbstractScript $script
  121. *
  122. * @return $this
  123. */
  124. public function addScriptField($name, AbstractScript $script)
  125. {
  126. if (!isset($this->_params['script_fields'])) {
  127. $this->_params['script_fields'] = new ScriptFields();
  128. }
  129. $this->_params['script_fields']->addScript($name, $script);
  130. return $this;
  131. }
  132. /**
  133. * Sets highlight arguments for the results.
  134. *
  135. * @param array $highlightArgs
  136. *
  137. * @return $this
  138. */
  139. public function setHighlight(array $highlightArgs)
  140. {
  141. return $this->setParam('highlight', $highlightArgs);
  142. }
  143. /**
  144. * Allows to return the field data representation of a field for each hit.
  145. *
  146. * @param array $fields
  147. *
  148. * @return $this
  149. */
  150. public function setFieldDataFields(array $fields)
  151. {
  152. return $this->setParam('docvalue_fields', $fields);
  153. }
  154. }