Result.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace Elastica;
  3. /**
  4. * Elastica result item.
  5. *
  6. * Stores all information from a result
  7. *
  8. * @author Nicolas Ruflin <spam@ruflin.com>
  9. */
  10. class Result
  11. {
  12. /**
  13. * Hit array.
  14. *
  15. * @var array Hit array
  16. */
  17. protected $_hit = [];
  18. /**
  19. * Constructs a single results object.
  20. *
  21. * @param array $hit Hit data
  22. */
  23. public function __construct(array $hit)
  24. {
  25. $this->_hit = $hit;
  26. }
  27. /**
  28. * Returns a param from the result hit array.
  29. *
  30. * This function can be used to retrieve all data for which a specific
  31. * function doesn't exist.
  32. * If the param does not exist, an empty array is returned
  33. *
  34. * @param string $name Param name
  35. *
  36. * @return mixed Result data
  37. */
  38. public function getParam($name)
  39. {
  40. if (isset($this->_hit[$name])) {
  41. return $this->_hit[$name];
  42. }
  43. return [];
  44. }
  45. /**
  46. * Test if a param from the result hit is set.
  47. *
  48. * @param string $name Param name to test
  49. *
  50. * @return bool True if the param is set, false otherwise
  51. */
  52. public function hasParam($name)
  53. {
  54. return isset($this->_hit[$name]);
  55. }
  56. /**
  57. * Returns the hit id.
  58. *
  59. * @return string Hit id
  60. */
  61. public function getId()
  62. {
  63. return $this->getParam('_id');
  64. }
  65. /**
  66. * Returns the type of the result.
  67. *
  68. * @return string Result type
  69. */
  70. public function getType()
  71. {
  72. return $this->getParam('_type');
  73. }
  74. /**
  75. * Returns list of fields.
  76. *
  77. * @return array Fields list
  78. */
  79. public function getFields()
  80. {
  81. return $this->getParam('fields');
  82. }
  83. /**
  84. * Returns whether result has fields.
  85. *
  86. * @return bool
  87. */
  88. public function hasFields()
  89. {
  90. return $this->hasParam('fields');
  91. }
  92. /**
  93. * Returns the index name of the result.
  94. *
  95. * @return string Index name
  96. */
  97. public function getIndex()
  98. {
  99. return $this->getParam('_index');
  100. }
  101. /**
  102. * Returns the score of the result.
  103. *
  104. * @return float Result score
  105. */
  106. public function getScore()
  107. {
  108. return $this->getParam('_score');
  109. }
  110. /**
  111. * Returns the raw hit array.
  112. *
  113. * @return array Hit array
  114. */
  115. public function getHit()
  116. {
  117. return $this->_hit;
  118. }
  119. /**
  120. * Returns the version information from the hit.
  121. *
  122. * @return string|int Document version
  123. */
  124. public function getVersion()
  125. {
  126. return $this->getParam('_version');
  127. }
  128. /**
  129. * Returns inner hits.
  130. *
  131. * @return array Fields list
  132. */
  133. public function getInnerHits()
  134. {
  135. return $this->getParam('inner_hits');
  136. }
  137. /**
  138. * Returns whether result has inner hits.
  139. *
  140. * @return bool
  141. */
  142. public function hasInnerHits()
  143. {
  144. return $this->hasParam('inner_hits');
  145. }
  146. /**
  147. * Returns result data.
  148. *
  149. * Checks for partial result data with getFields, falls back to getSource or both
  150. *
  151. * @return array Result data array
  152. */
  153. public function getData()
  154. {
  155. if (isset($this->_hit['fields'])) {
  156. return isset($this->_hit['_source'])
  157. ? array_merge($this->getFields(), $this->getSource())
  158. : $this->getFields();
  159. }
  160. return $this->getSource();
  161. }
  162. /**
  163. * Returns the result source.
  164. *
  165. * @return array Source data array
  166. */
  167. public function getSource()
  168. {
  169. return $this->getParam('_source');
  170. }
  171. /**
  172. * Returns result data.
  173. *
  174. * @return array Result data array
  175. */
  176. public function getHighlights()
  177. {
  178. return $this->getParam('highlight');
  179. }
  180. /**
  181. * Returns explanation on how its score was computed.
  182. *
  183. * @return array explanations
  184. */
  185. public function getExplanation()
  186. {
  187. return $this->getParam('_explanation');
  188. }
  189. /**
  190. * Returns Document.
  191. *
  192. * @return Document
  193. */
  194. public function getDocument()
  195. {
  196. $doc = new Document();
  197. $doc->setData($this->getSource());
  198. $hit = $this->getHit();
  199. unset($hit['_source']);
  200. unset($hit['_explanation']);
  201. unset($hit['highlight']);
  202. unset($hit['_score']);
  203. $doc->setParams($hit);
  204. return $doc;
  205. }
  206. /**
  207. * Sets a parameter on the hit.
  208. *
  209. * @param string $param
  210. * @param mixed $value
  211. */
  212. public function setParam($param, $value)
  213. {
  214. $this->_hit[$param] = $value;
  215. }
  216. /**
  217. * Magic function to directly access keys inside the result.
  218. *
  219. * Returns null if key does not exist
  220. *
  221. * @param string $key Key name
  222. *
  223. * @return mixed Key value
  224. */
  225. public function __get($key)
  226. {
  227. $source = $this->getData();
  228. return array_key_exists($key, $source) ? $source[$key] : null;
  229. }
  230. /**
  231. * Magic function to support isset() calls.
  232. *
  233. * @param string $key Key name
  234. *
  235. * @return bool
  236. */
  237. public function __isset($key)
  238. {
  239. $source = $this->getData();
  240. return array_key_exists($key, $source) && null !== $source[$key];
  241. }
  242. }