Document.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace Elastica;
  3. use Elastica\Bulk\Action;
  4. use Elastica\Exception\InvalidException;
  5. /**
  6. * Single document stored in elastic search.
  7. *
  8. * @author Nicolas Ruflin <spam@ruflin.com>
  9. */
  10. class Document extends AbstractUpdateAction
  11. {
  12. const OP_TYPE_CREATE = Action::OP_TYPE_CREATE;
  13. /**
  14. * Document data.
  15. *
  16. * @var array Document data
  17. */
  18. protected $_data = [];
  19. /**
  20. * Whether to use this document to upsert if the document does not exist.
  21. *
  22. * @var bool
  23. */
  24. protected $_docAsUpsert = false;
  25. /**
  26. * @var bool
  27. */
  28. protected $_autoPopulate = false;
  29. /**
  30. * Creates a new document.
  31. *
  32. * @param int|string $id OPTIONAL $id Id is create if empty
  33. * @param array|string $data OPTIONAL Data array
  34. * @param Type|string $type OPTIONAL Type name
  35. * @param Index|string $index OPTIONAL Index name
  36. */
  37. public function __construct($id = '', $data = [], $type = '', $index = '')
  38. {
  39. $this->setId($id);
  40. $this->setData($data);
  41. $this->setType($type);
  42. $this->setIndex($index);
  43. }
  44. /**
  45. * @param string $key
  46. *
  47. * @return mixed
  48. */
  49. public function __get($key)
  50. {
  51. return $this->get($key);
  52. }
  53. /**
  54. * @param string $key
  55. * @param mixed $value
  56. */
  57. public function __set($key, $value)
  58. {
  59. $this->set($key, $value);
  60. }
  61. /**
  62. * @param string $key
  63. *
  64. * @return bool
  65. */
  66. public function __isset($key)
  67. {
  68. return $this->has($key) && null !== $this->get($key);
  69. }
  70. /**
  71. * @param string $key
  72. */
  73. public function __unset($key)
  74. {
  75. $this->remove($key);
  76. }
  77. /**
  78. * @param string $key
  79. *
  80. * @throws \Elastica\Exception\InvalidException
  81. *
  82. * @return mixed
  83. */
  84. public function get($key)
  85. {
  86. if (!$this->has($key)) {
  87. throw new InvalidException("Field {$key} does not exist");
  88. }
  89. return $this->_data[$key];
  90. }
  91. /**
  92. * @param string $key
  93. * @param mixed $value
  94. *
  95. * @throws \Elastica\Exception\InvalidException
  96. *
  97. * @return $this
  98. */
  99. public function set($key, $value)
  100. {
  101. if (!is_array($this->_data)) {
  102. throw new InvalidException('Document data is serialized data. Data creation is forbidden.');
  103. }
  104. $this->_data[$key] = $value;
  105. return $this;
  106. }
  107. /**
  108. * @param string $key
  109. *
  110. * @return bool
  111. */
  112. public function has($key)
  113. {
  114. return is_array($this->_data) && array_key_exists($key, $this->_data);
  115. }
  116. /**
  117. * @param string $key
  118. *
  119. * @throws \Elastica\Exception\InvalidException
  120. *
  121. * @return $this
  122. */
  123. public function remove($key)
  124. {
  125. if (!$this->has($key)) {
  126. throw new InvalidException("Field {$key} does not exist");
  127. }
  128. unset($this->_data[$key]);
  129. return $this;
  130. }
  131. /**
  132. * Adds a file to the index.
  133. *
  134. * To use this feature you have to call the following command in the
  135. * elasticsearch directory:
  136. * <code>
  137. * ./bin/plugin -install elasticsearch/elasticsearch-mapper-attachments/1.6.0
  138. * </code>
  139. * This installs the tika file analysis plugin. More infos about supported formats
  140. * can be found here: {@link http://tika.apache.org/0.7/formats.html}
  141. *
  142. * @param string $key Key to add the file to
  143. * @param string $filepath Path to add the file
  144. * @param string $mimeType OPTIONAL Header mime type
  145. *
  146. * @return $this
  147. */
  148. public function addFile($key, $filepath, $mimeType = '')
  149. {
  150. $value = base64_encode(file_get_contents($filepath));
  151. if (!empty($mimeType)) {
  152. $value = ['_content_type' => $mimeType, '_name' => $filepath, '_content' => $value];
  153. }
  154. $this->set($key, $value);
  155. return $this;
  156. }
  157. /**
  158. * Add file content.
  159. *
  160. * @param string $key Document key
  161. * @param string $content Raw file content
  162. *
  163. * @return $this
  164. */
  165. public function addFileContent($key, $content)
  166. {
  167. return $this->set($key, base64_encode($content));
  168. }
  169. /**
  170. * Adds a geopoint to the document.
  171. *
  172. * Geohashes are not yet supported
  173. *
  174. * @param string $key Field key
  175. * @param float $latitude Latitude value
  176. * @param float $longitude Longitude value
  177. *
  178. * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-geo-point-type.html
  179. *
  180. * @return $this
  181. */
  182. public function addGeoPoint($key, $latitude, $longitude)
  183. {
  184. $value = ['lat' => $latitude, 'lon' => $longitude];
  185. $this->set($key, $value);
  186. return $this;
  187. }
  188. /**
  189. * Overwrites the current document data with the given data.
  190. *
  191. * @param array|string $data Data array
  192. *
  193. * @return $this
  194. */
  195. public function setData($data)
  196. {
  197. $this->_data = $data;
  198. return $this;
  199. }
  200. /**
  201. * Returns the document data.
  202. *
  203. * @return array|string Document data
  204. */
  205. public function getData()
  206. {
  207. return $this->_data;
  208. }
  209. /**
  210. * @param bool $value
  211. *
  212. * @return $this
  213. */
  214. public function setDocAsUpsert($value)
  215. {
  216. $this->_docAsUpsert = (bool) $value;
  217. return $this;
  218. }
  219. /**
  220. * @return bool
  221. */
  222. public function getDocAsUpsert()
  223. {
  224. return $this->_docAsUpsert;
  225. }
  226. /**
  227. * @param bool $autoPopulate
  228. *
  229. * @return $this
  230. */
  231. public function setAutoPopulate($autoPopulate = true)
  232. {
  233. $this->_autoPopulate = (bool) $autoPopulate;
  234. return $this;
  235. }
  236. /**
  237. * @return bool
  238. */
  239. public function isAutoPopulate()
  240. {
  241. return $this->_autoPopulate;
  242. }
  243. /**
  244. * Sets pipeline.
  245. *
  246. * @param string $pipeline
  247. *
  248. * @return $this
  249. */
  250. public function setPipeline($pipeline)
  251. {
  252. return $this->setParam('_pipeline', $pipeline);
  253. }
  254. /**
  255. * @return string
  256. */
  257. public function getPipeline()
  258. {
  259. return $this->getParam('_pipeline');
  260. }
  261. /**
  262. * @return bool
  263. */
  264. public function hasPipeline()
  265. {
  266. return $this->hasParam('_pipeline');
  267. }
  268. /**
  269. * Returns the document as an array.
  270. *
  271. * @return array
  272. */
  273. public function toArray()
  274. {
  275. $doc = $this->getParams();
  276. $doc['_source'] = $this->getData();
  277. return $doc;
  278. }
  279. /**
  280. * @param array|\Elastica\Document $data
  281. *
  282. * @throws \Elastica\Exception\InvalidException
  283. *
  284. * @return self
  285. */
  286. public static function create($data)
  287. {
  288. if ($data instanceof self) {
  289. return $data;
  290. }
  291. if (is_array($data)) {
  292. return new self('', $data);
  293. }
  294. throw new InvalidException('Failed to create document. Invalid data passed.');
  295. }
  296. }