ResultSet.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace Elastica;
  3. use Elastica\Exception\InvalidException;
  4. /**
  5. * Elastica result set.
  6. *
  7. * List of all hits that are returned for a search on elasticsearch
  8. * Result set implements iterator
  9. *
  10. * @author Nicolas Ruflin <spam@ruflin.com>
  11. */
  12. class ResultSet implements \Iterator, \Countable, \ArrayAccess
  13. {
  14. /**
  15. * Current position.
  16. *
  17. * @var int Current position
  18. */
  19. private $_position = 0;
  20. /**
  21. * Query.
  22. *
  23. * @var Query Query object
  24. */
  25. private $_query;
  26. /**
  27. * Response.
  28. *
  29. * @var Response Response object
  30. */
  31. private $_response;
  32. /**
  33. * Results.
  34. *
  35. * @var Result[] Results
  36. */
  37. private $_results = [];
  38. /**
  39. * Constructs ResultSet object.
  40. *
  41. * @param Response $response Response object
  42. * @param Query $query Query object
  43. * @param Result[] $results
  44. */
  45. public function __construct(Response $response, Query $query, $results)
  46. {
  47. $this->_query = $query;
  48. $this->_response = $response;
  49. $this->_results = $results;
  50. }
  51. /**
  52. * Returns all results.
  53. *
  54. * @return Result[] Results
  55. */
  56. public function getResults()
  57. {
  58. return $this->_results;
  59. }
  60. /**
  61. * Returns all Documents.
  62. *
  63. * @return array Documents \Elastica\Document
  64. */
  65. public function getDocuments()
  66. {
  67. $documents = [];
  68. foreach ($this->_results as $doc) {
  69. $documents[] = $doc->getDocument();
  70. }
  71. return $documents;
  72. }
  73. /**
  74. * Returns true if the response contains suggestion results; false otherwise.
  75. *
  76. * @return bool
  77. */
  78. public function hasSuggests()
  79. {
  80. $data = $this->_response->getData();
  81. return isset($data['suggest']);
  82. }
  83. /**
  84. * Return all suggests.
  85. *
  86. * @return array suggest results
  87. */
  88. public function getSuggests()
  89. {
  90. $data = $this->_response->getData();
  91. return $data['suggest'] ?? [];
  92. }
  93. /**
  94. * Returns whether aggregations exist.
  95. *
  96. * @return bool Aggregation existence
  97. */
  98. public function hasAggregations()
  99. {
  100. $data = $this->_response->getData();
  101. return isset($data['aggregations']);
  102. }
  103. /**
  104. * Returns all aggregation results.
  105. *
  106. * @return array
  107. */
  108. public function getAggregations()
  109. {
  110. $data = $this->_response->getData();
  111. return $data['aggregations'] ?? [];
  112. }
  113. /**
  114. * Retrieve a specific aggregation from this result set.
  115. *
  116. * @param string $name the name of the desired aggregation
  117. *
  118. * @throws Exception\InvalidException if an aggregation by the given name cannot be found
  119. *
  120. * @return array
  121. */
  122. public function getAggregation($name)
  123. {
  124. $data = $this->_response->getData();
  125. if (isset($data['aggregations']) && isset($data['aggregations'][$name])) {
  126. return $data['aggregations'][$name];
  127. }
  128. throw new InvalidException("This result set does not contain an aggregation named {$name}.");
  129. }
  130. /**
  131. * Returns the total number of found hits.
  132. *
  133. * @return int Total hits
  134. */
  135. public function getTotalHits()
  136. {
  137. $data = $this->_response->getData();
  138. return (int) ($data['hits']['total'] ?? 0);
  139. }
  140. /**
  141. * Returns the max score of the results found.
  142. *
  143. * @return float Max Score
  144. */
  145. public function getMaxScore()
  146. {
  147. $data = $this->_response->getData();
  148. return (float) ($data['hits']['max_score'] ?? 0);
  149. }
  150. /**
  151. * Returns the total number of ms for this search to complete.
  152. *
  153. * @return int Total time
  154. */
  155. public function getTotalTime()
  156. {
  157. $data = $this->_response->getData();
  158. return $data['took'] ?? 0;
  159. }
  160. /**
  161. * Returns true if the query has timed out.
  162. *
  163. * @return bool Timed out
  164. */
  165. public function hasTimedOut()
  166. {
  167. $data = $this->_response->getData();
  168. return !empty($data['timed_out']);
  169. }
  170. /**
  171. * Returns response object.
  172. *
  173. * @return Response Response object
  174. */
  175. public function getResponse()
  176. {
  177. return $this->_response;
  178. }
  179. /**
  180. * @return Query
  181. */
  182. public function getQuery()
  183. {
  184. return $this->_query;
  185. }
  186. /**
  187. * Returns size of current set.
  188. *
  189. * @return int Size of set
  190. */
  191. public function count()
  192. {
  193. return count($this->_results);
  194. }
  195. /**
  196. * Returns size of current suggests.
  197. *
  198. * @return int Size of suggests
  199. */
  200. public function countSuggests()
  201. {
  202. return sizeof($this->getSuggests());
  203. }
  204. /**
  205. * Returns the current object of the set.
  206. *
  207. * @return \Elastica\Result Set object
  208. */
  209. public function current()
  210. {
  211. return $this->_results[$this->key()];
  212. }
  213. /**
  214. * Sets pointer (current) to the next item of the set.
  215. */
  216. public function next()
  217. {
  218. ++$this->_position;
  219. }
  220. /**
  221. * Returns the position of the current entry.
  222. *
  223. * @return int Current position
  224. */
  225. public function key()
  226. {
  227. return $this->_position;
  228. }
  229. /**
  230. * Check if an object exists at the current position.
  231. *
  232. * @return bool True if object exists
  233. */
  234. public function valid()
  235. {
  236. return isset($this->_results[$this->key()]);
  237. }
  238. /**
  239. * Resets position to 0, restarts iterator.
  240. */
  241. public function rewind()
  242. {
  243. $this->_position = 0;
  244. }
  245. /**
  246. * Whether a offset exists.
  247. *
  248. * @see http://php.net/manual/en/arrayaccess.offsetexists.php
  249. *
  250. * @param int $offset
  251. *
  252. * @return bool true on success or false on failure
  253. */
  254. public function offsetExists($offset)
  255. {
  256. return isset($this->_results[$offset]);
  257. }
  258. /**
  259. * Offset to retrieve.
  260. *
  261. * @see http://php.net/manual/en/arrayaccess.offsetget.php
  262. *
  263. * @param int $offset
  264. *
  265. * @throws Exception\InvalidException If offset doesn't exist
  266. *
  267. * @return Result
  268. */
  269. public function offsetGet($offset)
  270. {
  271. if ($this->offsetExists($offset)) {
  272. return $this->_results[$offset];
  273. }
  274. throw new InvalidException('Offset does not exist.');
  275. }
  276. /**
  277. * Offset to set.
  278. *
  279. * @see http://php.net/manual/en/arrayaccess.offsetset.php
  280. *
  281. * @param int $offset
  282. * @param Result $value
  283. *
  284. * @throws Exception\InvalidException
  285. */
  286. public function offsetSet($offset, $value)
  287. {
  288. if (!($value instanceof Result)) {
  289. throw new InvalidException('ResultSet is a collection of Result only.');
  290. }
  291. if (!isset($this->_results[$offset])) {
  292. throw new InvalidException('Offset does not exist.');
  293. }
  294. $this->_results[$offset] = $value;
  295. }
  296. /**
  297. * Offset to unset.
  298. *
  299. * @see http://php.net/manual/en/arrayaccess.offsetunset.php
  300. *
  301. * @param int $offset
  302. */
  303. public function offsetUnset($offset)
  304. {
  305. unset($this->_results[$offset]);
  306. }
  307. }