AbstractUpdateAction.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. namespace Elastica;
  3. /**
  4. * Base class for things that can be sent to the update api (Document and
  5. * Script).
  6. *
  7. * @author Nik Everett <nik9000@gmail.com>
  8. */
  9. class AbstractUpdateAction extends Param
  10. {
  11. /**
  12. * @var \Elastica\Document
  13. */
  14. protected $_upsert;
  15. /**
  16. * Sets the id of the document.
  17. *
  18. * @param string $id
  19. *
  20. * @return $this
  21. */
  22. public function setId($id)
  23. {
  24. return $this->setParam('_id', $id);
  25. }
  26. /**
  27. * Returns document id.
  28. *
  29. * @return string|int Document id
  30. */
  31. public function getId()
  32. {
  33. return ($this->hasParam('_id')) ? $this->getParam('_id') : null;
  34. }
  35. /**
  36. * @return bool
  37. */
  38. public function hasId()
  39. {
  40. return '' !== (string) $this->getId();
  41. }
  42. /**
  43. * Sets the document type name.
  44. *
  45. * @param Type|string $type Type name
  46. *
  47. * @return $this
  48. */
  49. public function setType($type)
  50. {
  51. if ($type instanceof Type) {
  52. $this->setIndex($type->getIndex());
  53. $type = $type->getName();
  54. }
  55. return $this->setParam('_type', $type);
  56. }
  57. /**
  58. * Return document type name.
  59. *
  60. * @throws \Elastica\Exception\InvalidException
  61. *
  62. * @return string Document type name
  63. */
  64. public function getType()
  65. {
  66. return $this->getParam('_type');
  67. }
  68. /**
  69. * Sets the document index name.
  70. *
  71. * @param Index|string $index Index name
  72. *
  73. * @return $this
  74. */
  75. public function setIndex($index)
  76. {
  77. if ($index instanceof Index) {
  78. $index = $index->getName();
  79. }
  80. return $this->setParam('_index', $index);
  81. }
  82. /**
  83. * Get the document index name.
  84. *
  85. * @throws \Elastica\Exception\InvalidException
  86. *
  87. * @return string Index name
  88. */
  89. public function getIndex()
  90. {
  91. return $this->getParam('_index');
  92. }
  93. /**
  94. * Sets the version of a document for use with optimistic concurrency control.
  95. *
  96. * @param int $version Document version
  97. *
  98. * @return $this
  99. *
  100. * @see https://www.elastic.co/blog/versioning
  101. */
  102. public function setVersion($version)
  103. {
  104. return $this->setParam('_version', (int) $version);
  105. }
  106. /**
  107. * Returns document version.
  108. *
  109. * @return string|int Document version
  110. */
  111. public function getVersion()
  112. {
  113. return $this->getParam('_version');
  114. }
  115. /**
  116. * @return bool
  117. */
  118. public function hasVersion()
  119. {
  120. return $this->hasParam('_version');
  121. }
  122. /**
  123. * Sets the version_type of a document
  124. * Default in ES is internal, but you can set to external to use custom versioning.
  125. *
  126. * @param string $versionType Document version type
  127. *
  128. * @return $this
  129. */
  130. public function setVersionType($versionType)
  131. {
  132. return $this->setParam('_version_type', $versionType);
  133. }
  134. /**
  135. * Returns document version type.
  136. *
  137. * @return string|int Document version type
  138. */
  139. public function getVersionType()
  140. {
  141. return $this->getParam('_version_type');
  142. }
  143. /**
  144. * @return bool
  145. */
  146. public function hasVersionType()
  147. {
  148. return $this->hasParam('_version_type');
  149. }
  150. /**
  151. * Sets parent document id.
  152. *
  153. * @param string|int $parent Parent document id
  154. *
  155. * @return $this
  156. *
  157. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-parent-field.html
  158. */
  159. public function setParent($parent)
  160. {
  161. return $this->setParam('_parent', $parent);
  162. }
  163. /**
  164. * Returns the parent document id.
  165. *
  166. * @return string|int Parent document id
  167. */
  168. public function getParent()
  169. {
  170. return $this->getParam('_parent');
  171. }
  172. /**
  173. * @return bool
  174. */
  175. public function hasParent()
  176. {
  177. return $this->hasParam('_parent');
  178. }
  179. /**
  180. * Set operation type.
  181. *
  182. * @param string $opType Only accept create
  183. *
  184. * @return $this
  185. */
  186. public function setOpType($opType)
  187. {
  188. return $this->setParam('_op_type', $opType);
  189. }
  190. /**
  191. * Get operation type.
  192. *
  193. * @return string
  194. */
  195. public function getOpType()
  196. {
  197. return $this->getParam('_op_type');
  198. }
  199. /**
  200. * @return bool
  201. */
  202. public function hasOpType()
  203. {
  204. return $this->hasParam('_op_type');
  205. }
  206. /**
  207. * Set routing query param.
  208. *
  209. * @param string $value routing
  210. *
  211. * @return $this
  212. */
  213. public function setRouting($value)
  214. {
  215. return $this->setParam('_routing', $value);
  216. }
  217. /**
  218. * Get routing parameter.
  219. *
  220. * @return string
  221. */
  222. public function getRouting()
  223. {
  224. return $this->getParam('_routing');
  225. }
  226. /**
  227. * @return bool
  228. */
  229. public function hasRouting()
  230. {
  231. return $this->hasParam('_routing');
  232. }
  233. /**
  234. * @param array|string $fields
  235. *
  236. * @return $this
  237. */
  238. public function setFields($fields)
  239. {
  240. if (is_array($fields)) {
  241. $fields = implode(',', $fields);
  242. }
  243. return $this->setParam('_fields', (string) $fields);
  244. }
  245. /**
  246. * @return $this
  247. */
  248. public function setFieldsSource()
  249. {
  250. return $this->setFields('_source');
  251. }
  252. /**
  253. * @return string
  254. */
  255. public function getFields()
  256. {
  257. return $this->getParam('_fields');
  258. }
  259. /**
  260. * @return bool
  261. */
  262. public function hasFields()
  263. {
  264. return $this->hasParam('_fields');
  265. }
  266. /**
  267. * @param int $num
  268. *
  269. * @return $this
  270. */
  271. public function setRetryOnConflict($num)
  272. {
  273. return $this->setParam('_retry_on_conflict', (int) $num);
  274. }
  275. /**
  276. * @return int
  277. */
  278. public function getRetryOnConflict()
  279. {
  280. return $this->getParam('_retry_on_conflict');
  281. }
  282. /**
  283. * @return bool
  284. */
  285. public function hasRetryOnConflict()
  286. {
  287. return $this->hasParam('_retry_on_conflict');
  288. }
  289. /**
  290. * @param bool $refresh
  291. *
  292. * @return $this
  293. */
  294. public function setRefresh($refresh = true)
  295. {
  296. return $this->setParam('_refresh', (bool) $refresh ? 'true' : 'false');
  297. }
  298. /**
  299. * @return bool
  300. */
  301. public function getRefresh()
  302. {
  303. return 'true' === $this->getParam('_refresh');
  304. }
  305. /**
  306. * @return bool
  307. */
  308. public function hasRefresh()
  309. {
  310. return $this->hasParam('_refresh');
  311. }
  312. /**
  313. * @param string $timeout
  314. *
  315. * @return $this
  316. */
  317. public function setTimeout($timeout)
  318. {
  319. return $this->setParam('_timeout', $timeout);
  320. }
  321. /**
  322. * @return bool
  323. */
  324. public function getTimeout()
  325. {
  326. return $this->getParam('_timeout');
  327. }
  328. /**
  329. * @return bool
  330. */
  331. public function hasTimeout()
  332. {
  333. return $this->hasParam('_timeout');
  334. }
  335. /**
  336. * @param string $timeout
  337. *
  338. * @return $this
  339. */
  340. public function setConsistency($timeout)
  341. {
  342. return $this->setParam('_consistency', $timeout);
  343. }
  344. /**
  345. * @return string
  346. */
  347. public function getConsistency()
  348. {
  349. return $this->getParam('_consistency');
  350. }
  351. /**
  352. * @return bool
  353. */
  354. public function hasConsistency()
  355. {
  356. return $this->hasParam('_consistency');
  357. }
  358. /**
  359. * @param string $timeout
  360. *
  361. * @return $this
  362. */
  363. public function setReplication($timeout)
  364. {
  365. return $this->setParam('_replication', $timeout);
  366. }
  367. /**
  368. * @return string
  369. */
  370. public function getReplication()
  371. {
  372. return $this->getParam('_replication');
  373. }
  374. /**
  375. * @return bool
  376. */
  377. public function hasReplication()
  378. {
  379. return $this->hasParam('_replication');
  380. }
  381. /**
  382. * @param \Elastica\Document|array $data
  383. *
  384. * @return $this
  385. */
  386. public function setUpsert($data)
  387. {
  388. $document = Document::create($data);
  389. $this->_upsert = $document;
  390. return $this;
  391. }
  392. /**
  393. * @return \Elastica\Document
  394. */
  395. public function getUpsert()
  396. {
  397. return $this->_upsert;
  398. }
  399. /**
  400. * @return bool
  401. */
  402. public function hasUpsert()
  403. {
  404. return null !== $this->_upsert;
  405. }
  406. /**
  407. * @param array $fields if empty array all options will be returned, field names can be either with underscored either without, i.e. _percolate, routing
  408. * @param bool $withUnderscore should option keys contain underscore prefix
  409. *
  410. * @return array
  411. */
  412. public function getOptions(array $fields = [], $withUnderscore = false)
  413. {
  414. if (!empty($fields)) {
  415. $data = [];
  416. foreach ($fields as $field) {
  417. $key = '_'.ltrim($field, '_');
  418. if ($this->hasParam($key) && '' !== (string) $this->getParam($key)) {
  419. $data[$key] = $this->getParam($key);
  420. }
  421. }
  422. } else {
  423. $data = $this->getParams();
  424. }
  425. if (!$withUnderscore) {
  426. foreach ($data as $key => $value) {
  427. $data[ltrim($key, '_')] = $value;
  428. unset($data[$key]);
  429. }
  430. }
  431. return $data;
  432. }
  433. }