/lib/Elastica/Type.php

https://github.com/darkyoung/Elastica · PHP · 320 lines · 132 code · 45 blank · 143 comment · 8 complexity · 9ad7dbd380901538bbb42269b077bba0 MD5 · raw file

  1. <?php
  2. /**
  3. * Elastica type object
  4. *
  5. * elasticsearch has for every types as a substructure. This object
  6. * represents a type inside a context
  7. * The hirarchie is as following: client -> index -> type -> document
  8. *
  9. * Search over different indices and types is not supported yet {@link http://www.elasticsearch.com/docs/elasticsearch/rest_api/search/indices_types/}
  10. *
  11. * @category Xodoa
  12. * @package Elastica
  13. * @author Nicolas Ruflin <spam@ruflin.com>
  14. */
  15. class Elastica_Type implements Elastica_Searchable
  16. {
  17. /**
  18. * Index
  19. *
  20. * @var Elastica_Index Index object
  21. */
  22. protected $_index = null;
  23. /**
  24. * Type name
  25. *
  26. * @var string Type name
  27. */
  28. protected $_name = '';
  29. /**
  30. * Creates a new type object inside the given index
  31. *
  32. * @param Elastica_Index $index Index Object
  33. * @param string $name Type name
  34. */
  35. public function __construct(Elastica_Index $index, $name)
  36. {
  37. $this->_index = $index;
  38. $this->_name = $name;
  39. }
  40. /**
  41. * Adds the given document to the search index
  42. *
  43. * @param Elastica_Document $doc Document with data
  44. * @return Elastica_Response
  45. */
  46. public function addDocument(Elastica_Document $doc)
  47. {
  48. $path = $doc->getId();
  49. $query = array();
  50. if ($doc->getVersion() > 0) {
  51. $query['version'] = $doc->getVersion();
  52. }
  53. if (!is_null($doc->getParent())) {
  54. $query['parent'] = $doc->getParent();
  55. }
  56. if ($doc->getOpType()) {
  57. $query['op_type'] = $doc->getOpType();
  58. }
  59. if ($doc->getPercolate()) {
  60. $query['percolate'] = $doc->getPercolate();
  61. }
  62. $type = Elastica_Request::PUT;
  63. // If id is empty, POST has to be used to automatically create id
  64. if (empty($path)) {
  65. $type = Elastica_Request::POST;
  66. }
  67. return $this->request($path, $type, $doc->getData(), $query);
  68. }
  69. /**
  70. * Update document, using update script. Requires elasticsearch >= 0.19.0
  71. *
  72. * @param string $id Document id
  73. * @param Elastica_Script $script script to use for update
  74. * @param array $options options for query
  75. * @return Elastica_Response
  76. * @see Elastica_Client::updateDocument()
  77. * @link http://www.elasticsearch.org/guide/reference/api/update.html
  78. */
  79. public function updateDocument($id, Elastica_Script $script, array $options = array())
  80. {
  81. return $this->getIndex()->updateDocument($id, $script, $this->getName(), $options);
  82. }
  83. /**
  84. * Uses _bulk to send documents to the server
  85. *
  86. * @param array $docs Array of Elastica_Document
  87. * @link http://www.elasticsearch.com/docs/elasticsearch/rest_api/bulk/
  88. */
  89. public function addDocuments(array $docs)
  90. {
  91. foreach ($docs as $doc) {
  92. $doc->setType($this->getName());
  93. }
  94. return $this->getIndex()->addDocuments($docs);
  95. }
  96. /**
  97. * Get the document from search index
  98. *
  99. * @param string $id Document id
  100. * @return Elastica_Document
  101. */
  102. public function getDocument($id)
  103. {
  104. $path = $id;
  105. try {
  106. $result = $this->request($path, Elastica_Request::GET)->getData();
  107. } catch (Elastica_Exception_Response $e) {
  108. throw new Elastica_Exception_NotFound('doc id ' . $id . ' not found');
  109. }
  110. if (empty($result['exists'])) {
  111. throw new Elastica_Exception_NotFound('doc id ' . $id . ' not found');
  112. }
  113. $data = isset($result['_source']) ? $result['_source'] : array();
  114. $document = new Elastica_Document($id, $data, $this->getName(), $this->getIndex());
  115. $document->setVersion($result['_version']);
  116. return $document;
  117. }
  118. /**
  119. * Returns the type name
  120. *
  121. * @return string Type
  122. * @deprecated Use getName instead
  123. */
  124. public function getType()
  125. {
  126. return $this->getName();
  127. }
  128. /**
  129. * Returns the type name
  130. *
  131. * @return string Type name
  132. */
  133. public function getName()
  134. {
  135. return $this->_name;
  136. }
  137. /**
  138. * Sets value type mapping for this type
  139. *
  140. * @param Elastica_Type_Mapping|array $mapping Elastica_Type_Mapping object or property array with all mappings
  141. */
  142. public function setMapping($mapping)
  143. {
  144. $mapping = Elastica_Type_Mapping::create($mapping);
  145. $mapping->setType($this);
  146. return $mapping->send();
  147. }
  148. /**
  149. * Returns current mapping for the given type
  150. *
  151. * @return array Current mapping
  152. */
  153. public function getMapping()
  154. {
  155. $path = '_mapping';
  156. $response = $this->request($path, Elastica_Request::GET);
  157. return $response->getData();
  158. }
  159. /**
  160. * Do a search on this type
  161. *
  162. * @param string|array|Elastica_Query $query Array with all query data inside or a Elastica_Query object
  163. * @param int|array $options OPTIONAL Limit or associative array of options (option=>value)
  164. * @return Elastica_ResultSet ResultSet with all results inside
  165. * @see Elastica_Searchable::search
  166. */
  167. public function search($query, $options = null)
  168. {
  169. $search = new Elastica_Search($this->getIndex()->getClient());
  170. $search->addIndex($this->getIndex());
  171. $search->addType($this);
  172. return $search->search($query, $options);
  173. }
  174. /**
  175. * Count docs by query
  176. *
  177. * @param string|array|Elastica_Query $query Array with all query data inside or a Elastica_Query object
  178. * @return int number of documents matching the query
  179. * @see Elastica_Searchable::count
  180. */
  181. public function count($query = '')
  182. {
  183. $query = Elastica_Query::create($query);
  184. $path = '_search';
  185. $response = $this->request($path, Elastica_Request::GET, $query->toArray(), array('search_type' => 'count'));
  186. $resultSet = new Elastica_ResultSet($response);
  187. return $resultSet->getTotalHits();
  188. }
  189. /**
  190. * Returns index client
  191. *
  192. * @return Elastica_Index Index object
  193. */
  194. public function getIndex()
  195. {
  196. return $this->_index;
  197. }
  198. /**
  199. * Deletes an entry by its unique identifier
  200. *
  201. * @param int|string $id Document id
  202. * @return Elastica_Response Response object
  203. * @link http://www.elasticsearch.org/guide/reference/api/delete.html
  204. */
  205. public function deleteById($id)
  206. {
  207. if (empty($id) || !trim($id)) {
  208. throw new InvalidArgumentException();
  209. }
  210. return $this->request($id, Elastica_Request::DELETE);
  211. }
  212. /**
  213. * Deletes the given list of ids from this type
  214. *
  215. * @param array $ids
  216. * @return Elastica_Response Response object
  217. */
  218. public function deleteIds(array $ids)
  219. {
  220. return $this->getIndex()->getClient()->deleteIds($ids, $this->getIndex(), $this);
  221. }
  222. /**
  223. * Deletes entries in the db based on a query
  224. *
  225. * @param Elastica_Query|string $query Query object
  226. * @link http://www.elasticsearch.org/guide/reference/api/delete-by-query.html
  227. */
  228. public function deleteByQuery($query)
  229. {
  230. $query = Elastica_Query::create($query);
  231. return $this->request('_query', Elastica_Request::DELETE, $query->getQuery());
  232. }
  233. /**
  234. * Deletes the index type.
  235. *
  236. * @return Elastica_Response
  237. */
  238. public function delete()
  239. {
  240. $response = $this->request('', Elastica_Request::DELETE);
  241. return $response;
  242. }
  243. /**
  244. * More like this query based on the given object
  245. *
  246. * The id in the given object has to be set
  247. *
  248. * @param Elastica_Document $doc Document to query for similar objects
  249. * @param array $params OPTIONAL Additional arguments for the query
  250. * @param Elastica_Query $query OPTIONAL Query to filter the moreLikeThis results
  251. * @return Elastica_ResultSet ResultSet with all results inside
  252. * @link http://www.elasticsearch.org/guide/reference/api/more-like-this.html
  253. */
  254. public function moreLikeThis(Elastica_Document $doc, $params = array(), $query = array())
  255. {
  256. $path = $doc->getId() . '/_mlt';
  257. $query = Elastica_Query::create($query);
  258. $response = $this->request($path, Elastica_Request::GET, $query->toArray(), $params);
  259. return new Elastica_ResultSet($response);
  260. }
  261. /**
  262. * Makes calls to the elasticsearch server based on this type
  263. *
  264. * @param string $path Path to call
  265. * @param string $method Rest method to use (GET, POST, DELETE, PUT)
  266. * @param array $data OPTIONAL Arguments as array
  267. * @param array $query OPTIONAL Query params
  268. * @return Elastica_Response Response object
  269. */
  270. public function request($path, $method, $data = array(), array $query = array())
  271. {
  272. $path = $this->getName() . '/' . $path;
  273. return $this->getIndex()->request($path, $method, $data, $query);
  274. }
  275. }