TopHits.php 3.7 KB

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