Command.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\elasticsearch;
  8. use yii\base\Component;
  9. use yii\base\InvalidCallException;
  10. use yii\helpers\ArrayHelper;
  11. use yii\helpers\Json;
  12. /**
  13. * The Command class implements the API for accessing the Elasticsearch REST API.
  14. *
  15. * Check the [Elasticsearch guide](https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)
  16. * for details on these commands.
  17. *
  18. * @author Carsten Brandt <mail@cebe.cc>
  19. * @since 2.0
  20. */
  21. class Command extends Component
  22. {
  23. /**
  24. * @var Connection
  25. */
  26. public $db;
  27. /**
  28. * @var string|array the indexes to execute the query on. Defaults to null meaning all indexes
  29. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html#search-multi-index-type
  30. */
  31. public $index;
  32. /**
  33. * @var string|array|null the types to execute the query on. Defaults to null meaning all types
  34. */
  35. public $type;
  36. /**
  37. * @var array list of arrays or json strings that become parts of a query
  38. */
  39. public $queryParts;
  40. /**
  41. * @var array options to be appended to the query URL, such as "search_type" for search or "timeout" for delete
  42. */
  43. public $options = [];
  44. /**
  45. * Sends a request to the _search API and returns the result
  46. * @param array $options URL options
  47. * @return mixed
  48. * @throws Exception
  49. * @throws \yii\base\InvalidConfigException
  50. */
  51. public function search($options = [])
  52. {
  53. $query = $this->queryParts;
  54. if (empty($query)) {
  55. $query = '{}';
  56. }
  57. if (is_array($query)) {
  58. $query = Json::encode($query);
  59. }
  60. $url = [$this->index !== null ? $this->index : '_all'];
  61. if ($this->db->dslVersion < 7 && $this->type !== null) {
  62. $url[] = $this->type;
  63. }
  64. $url[] = '_search';
  65. return $this->db->get($url, array_merge($this->options, $options), $query);
  66. }
  67. /**
  68. * Sends a request to the delete by query
  69. * @param array $options URL options
  70. * @return mixed
  71. * @throws Exception
  72. * @throws \yii\base\InvalidConfigException
  73. */
  74. public function deleteByQuery($options = [])
  75. {
  76. if (!isset($this->queryParts['query'])) {
  77. throw new InvalidCallException('Can not call deleteByQuery when no query is given.');
  78. }
  79. $query = [
  80. 'query' => $this->queryParts['query'],
  81. ];
  82. if (isset($this->queryParts['filter'])) {
  83. $query['filter'] = $this->queryParts['filter'];
  84. }
  85. $query = Json::encode($query);
  86. $url = [$this->index !== null ? $this->index : '_all'];
  87. if ($this->type !== null) {
  88. $url[] = $this->type;
  89. }
  90. $url[] = '_delete_by_query';
  91. return $this->db->post($url, array_merge($this->options, $options), $query);
  92. }
  93. /**
  94. * Sends a suggest request to the _search API and returns the result
  95. * @param string|array $suggester the suggester body
  96. * @param array $options URL options
  97. * @return mixed
  98. * @throws Exception
  99. * @throws \yii\base\InvalidConfigException
  100. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html
  101. */
  102. public function suggest($suggester, $options = [])
  103. {
  104. if (empty($suggester)) {
  105. $suggester = '{}';
  106. }
  107. if (is_array($suggester)) {
  108. $suggester = Json::encode($suggester);
  109. }
  110. $body = '{"suggest":'.$suggester.',"size":0}';
  111. $url = [
  112. $this->index !== null ? $this->index : '_all',
  113. '_search'
  114. ];
  115. $result = $this->db->post($url, array_merge($this->options, $options), $body);
  116. return $result['suggest'];
  117. }
  118. /**
  119. * Inserts a document into an index
  120. * @param string $index Index that the document belongs to.
  121. * @param string|null $type Type that the document belongs to.
  122. * @param string|array $data json string or array of data to store
  123. * @param string|null $id the documents id. If not specified Id will be automatically chosen
  124. * @param array $options URL options
  125. * @return mixed
  126. * @throws Exception
  127. * @throws \yii\base\InvalidConfigException
  128. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
  129. */
  130. public function insert($index, $type, $data, $id = null, $options = [])
  131. {
  132. if (empty($data)) {
  133. $body = '{}';
  134. } else {
  135. $body = is_array($data) ? Json::encode($data) : $data;
  136. }
  137. if ($id !== null) {
  138. if ($this->db->dslVersion >= 7) {
  139. return $this->db->put([$index, '_doc', $id], $options, $body);
  140. } else {
  141. return $this->db->put([$index, $type, $id], $options, $body);
  142. }
  143. } else {
  144. if ($this->db->dslVersion >= 7) {
  145. return $this->db->post([$index, '_doc'], $options, $body);
  146. } else {
  147. return $this->db->post([$index, $type], $options, $body);
  148. }
  149. }
  150. }
  151. /**
  152. * gets a document from the index
  153. * @param string $index Index that the document belongs to.
  154. * @param string|null $type Type that the document belongs to.
  155. * @param string $id the documents id.
  156. * @param array $options URL options
  157. * @return mixed
  158. * @throws Exception
  159. * @throws \yii\base\InvalidConfigException
  160. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html
  161. */
  162. public function get($index, $type, $id, $options = [])
  163. {
  164. if ($this->db->dslVersion >= 7) {
  165. return $this->db->get([$index, '_doc', $id], $options);
  166. } else {
  167. return $this->db->get([$index, $type, $id], $options);
  168. }
  169. }
  170. /**
  171. * gets multiple documents from the index
  172. *
  173. * TODO allow specifying type and index + fields
  174. * @param string $index Index that the document belongs to.
  175. * @param string|null $type Type that the document belongs to.
  176. * @param string[] $ids the documents ids as values in array.
  177. * @param array $options URL options
  178. * @return mixed
  179. * @throws Exception
  180. * @throws \yii\base\InvalidConfigException
  181. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html
  182. */
  183. public function mget($index, $type, $ids, $options = [])
  184. {
  185. $body = Json::encode(['ids' => array_values($ids)]);
  186. if ($this->db->dslVersion >= 7) {
  187. return $this->db->get([$index, '_doc', '_mget'], $options, $body);
  188. } else {
  189. return $this->db->get([$index, $type, '_mget'], $options, $body);
  190. }
  191. }
  192. /**
  193. * gets a documents _source from the index (>=v0.90.1)
  194. * @param string $index Index that the document belongs to.
  195. * @param string|null $type Type that the document belongs to.
  196. * @param string $id the documents id.
  197. * @return mixed
  198. * @throws Exception
  199. * @throws \yii\base\InvalidConfigException
  200. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#_source
  201. */
  202. public function getSource($index, $type, $id)
  203. {
  204. if ($this->db->dslVersion >= 7) {
  205. return $this->db->get([$index, '_doc', $id]);
  206. } else {
  207. return $this->db->get([$index, $type, $id]);
  208. }
  209. }
  210. /**
  211. * gets a document from the index
  212. * @param string $index Index that the document belongs to.
  213. * @param string|null $type Type that the document belongs to.
  214. * @param string $id the documents id.
  215. * @return mixed
  216. * @throws Exception
  217. * @throws \yii\base\InvalidConfigException
  218. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html
  219. */
  220. public function exists($index, $type, $id)
  221. {
  222. if ($this->db->dslVersion >= 7) {
  223. return $this->db->head([$index, '_doc', $id]);
  224. } else {
  225. return $this->db->head([$index, $type, $id]);
  226. }
  227. }
  228. /**
  229. * deletes a document from the index
  230. * @param string $index Index that the document belongs to.
  231. * @param string|null $type Type that the document belongs to.
  232. * @param string $id the documents id.
  233. * @param array $options URL options
  234. * @return mixed
  235. * @throws Exception
  236. * @throws \yii\base\InvalidConfigException
  237. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html
  238. */
  239. public function delete($index, $type, $id, $options = [])
  240. {
  241. if ($this->db->dslVersion >= 7) {
  242. return $this->db->delete([$index, '_doc', $id], $options);
  243. } else {
  244. return $this->db->delete([$index, $type, $id], $options);
  245. }
  246. }
  247. /**
  248. * updates a document
  249. * @param string $index Index that the document belongs to.
  250. * @param string|null $type Type that the document belongs to.
  251. * @param string $id the documents id.
  252. * @param mixed $data
  253. * @param array $options URL options
  254. * @return mixed
  255. * @throws Exception
  256. * @throws \yii\base\InvalidConfigException
  257. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
  258. */
  259. public function update($index, $type, $id, $data, $options = [])
  260. {
  261. $body = [
  262. 'doc' => empty($data) ? new \stdClass() : $data,
  263. ];
  264. if (isset($options["detect_noop"])) {
  265. $body["detect_noop"] = $options["detect_noop"];
  266. unset($options["detect_noop"]);
  267. }
  268. if ($this->db->dslVersion >= 7) {
  269. return $this->db->post([$index, '_doc', $id, '_update'], $options, Json::encode($body));
  270. } else {
  271. return $this->db->post([$index, $type, $id, '_update'], $options, Json::encode($body));
  272. }
  273. }
  274. // TODO bulk https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
  275. /**
  276. * creates an index
  277. * @param string $index Index that the document belongs to.
  278. * @param null|array $configuration
  279. * @return mixed
  280. * @throws Exception
  281. * @throws \yii\base\InvalidConfigException
  282. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
  283. */
  284. public function createIndex($index, $configuration = null)
  285. {
  286. $body = $configuration !== null ? Json::encode($configuration) : null;
  287. return $this->db->put([$index], [], $body);
  288. }
  289. /**
  290. * deletes an index
  291. * @param string $index Index that the document belongs to.
  292. * @return mixed
  293. * @throws Exception
  294. * @throws \yii\base\InvalidConfigException
  295. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html
  296. */
  297. public function deleteIndex($index)
  298. {
  299. return $this->db->delete([$index]);
  300. }
  301. /**
  302. * deletes all indexes
  303. * @return mixed
  304. * @throws Exception
  305. * @throws \yii\base\InvalidConfigException
  306. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html
  307. */
  308. public function deleteAllIndexes()
  309. {
  310. return $this->db->delete(['_all']);
  311. }
  312. /**
  313. * checks whether an index exists
  314. * @param string $index Index that the document belongs to.
  315. * @return mixed
  316. * @throws Exception
  317. * @throws \yii\base\InvalidConfigException
  318. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html
  319. */
  320. public function indexExists($index)
  321. {
  322. return $this->db->head([$index]);
  323. }
  324. /**
  325. * @param string $index Index that the document belongs to.
  326. * @param string|null $type Type that the document belongs to.
  327. * @return mixed
  328. * @throws Exception
  329. * @throws \yii\base\InvalidConfigException
  330. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-types-exists.html
  331. */
  332. public function typeExists($index, $type)
  333. {
  334. if ($this->db->dslVersion >= 7) {
  335. return $this->db->head([$index, '_doc']);
  336. } else {
  337. return $this->db->head([$index, $type]);
  338. }
  339. }
  340. /**
  341. * @param string $alias
  342. *
  343. * @return bool
  344. * @throws Exception
  345. * @throws \yii\base\InvalidConfigException
  346. */
  347. public function aliasExists($alias)
  348. {
  349. $indexes = $this->getIndexesByAlias($alias);
  350. return !empty($indexes);
  351. }
  352. /**
  353. * @return array
  354. * @throws Exception
  355. * @throws \yii\base\InvalidConfigException
  356. * @see https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#alias-retrieving
  357. */
  358. public function getAliasInfo()
  359. {
  360. $aliasInfo = $this->db->get(['_alias', '*']);
  361. return $aliasInfo ?: [];
  362. }
  363. /**
  364. * @param string $alias
  365. *
  366. * @return array
  367. * @throws Exception
  368. * @throws \yii\base\InvalidConfigException
  369. * @see https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#alias-retrieving
  370. */
  371. public function getIndexInfoByAlias($alias)
  372. {
  373. $responseData = $this->db->get(['_alias', $alias]);
  374. if (empty($responseData)) {
  375. return [];
  376. }
  377. return $responseData;
  378. }
  379. /**
  380. * @param string $alias
  381. *
  382. * @return array
  383. * @throws Exception
  384. * @throws \yii\base\InvalidConfigException
  385. */
  386. public function getIndexesByAlias($alias)
  387. {
  388. return array_keys($this->getIndexInfoByAlias($alias));
  389. }
  390. /**
  391. * @param string $index Index that the document belongs to.
  392. *
  393. * @return array
  394. * @throws Exception
  395. * @throws \yii\base\InvalidConfigException
  396. * @see https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#alias-retrieving
  397. */
  398. public function getIndexAliases($index)
  399. {
  400. $responseData = $this->db->get([$index, '_alias', '*']);
  401. if (empty($responseData)) {
  402. return [];
  403. }
  404. return $responseData[$index]['aliases'];
  405. }
  406. /**
  407. * @param string $index Index that the document belongs to.
  408. * @param string $alias
  409. * @param array $aliasParameters
  410. *
  411. * @return bool
  412. * @throws Exception
  413. * @throws \yii\base\InvalidConfigException
  414. * @see https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#alias-adding
  415. */
  416. public function addAlias($index, $alias, $aliasParameters = [])
  417. {
  418. return (bool)$this->db->put([$index, '_alias', $alias], [], json_encode((object)$aliasParameters));
  419. }
  420. /**
  421. * @param string $index Index that the document belongs to.
  422. * @param string $alias
  423. *
  424. * @return bool
  425. * @throws Exception
  426. * @throws \yii\base\InvalidConfigException
  427. * @see https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#deleting
  428. */
  429. public function removeAlias($index, $alias)
  430. {
  431. return (bool)$this->db->delete([$index, '_alias', $alias]);
  432. }
  433. /**
  434. * Runs alias manipulations.
  435. * If you want to add alias1 to index1
  436. * and remove alias2 from index2 you can use following commands:
  437. * ~~~
  438. * $actions = [
  439. * ['add' => ['index' => 'index1', 'alias' => 'alias1']],
  440. * ['remove' => ['index' => 'index2', 'alias' => 'alias2']],
  441. * ];
  442. * ~~~
  443. * @param array $actions
  444. *
  445. * @return bool
  446. * @throws Exception
  447. * @throws \yii\base\InvalidConfigException
  448. * @see https://www.elastic.co/guide/en/elasticsearch/reference/2.0/indices-aliases.html#indices-aliases
  449. */
  450. public function aliasActions(array $actions)
  451. {
  452. return (bool)$this->db->post(['_aliases'], [], json_encode(['actions' => $actions]));
  453. }
  454. /**
  455. * Change specific index level settings in real time.
  456. * Note that update analyzers required to [[close()]] the index first and [[open()]] it after the changes are made,
  457. * use [[updateAnalyzers()]] for it.
  458. *
  459. * @param string $index Index that the document belongs to.
  460. * @param string|array $setting
  461. * @param array $options URL options
  462. * @return mixed
  463. * @throws Exception
  464. * @throws \yii\base\InvalidConfigException
  465. * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html
  466. * @since 2.0.4
  467. */
  468. public function updateSettings($index, $setting, $options = [])
  469. {
  470. $body = $setting !== null ? (is_string($setting) ? $setting : Json::encode($setting)) : null;
  471. return $this->db->put([$index, '_settings'], $options, $body);
  472. }
  473. /**
  474. * Define new analyzers for the index.
  475. * For example if content analyzer hasn’t been defined on "myindex" yet
  476. * you can use the following commands to add it:
  477. *
  478. * ~~~
  479. * $setting = [
  480. * 'analysis' => [
  481. * 'analyzer' => [
  482. * 'ngram_analyzer_with_filter' => [
  483. * 'tokenizer' => 'ngram_tokenizer',
  484. * 'filter' => 'lowercase, snowball'
  485. * ],
  486. * ],
  487. * 'tokenizer' => [
  488. * 'ngram_tokenizer' => [
  489. * 'type' => 'nGram',
  490. * 'min_gram' => 3,
  491. * 'max_gram' => 10,
  492. * 'token_chars' => ['letter', 'digit', 'whitespace', 'punctuation', 'symbol']
  493. * ],
  494. * ],
  495. * ]
  496. * ];
  497. * $elasticQuery->createCommand()->updateAnalyzers('myindex', $setting);
  498. * ~~~
  499. *
  500. * @param string $index Index that the document belongs to.
  501. * @param string|array $setting
  502. * @param array $options URL options
  503. * @return mixed
  504. * @throws Exception
  505. * @throws \yii\base\InvalidConfigException
  506. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html#update-settings-analysis
  507. * @since 2.0.4
  508. */
  509. public function updateAnalyzers($index, $setting, $options = [])
  510. {
  511. $this->closeIndex($index);
  512. $result = $this->updateSettings($index, $setting, $options);
  513. $this->openIndex($index);
  514. return $result;
  515. }
  516. // TODO https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html
  517. // TODO https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-warmers.html
  518. /**
  519. * @param string $index Index that the document belongs to.
  520. * @return mixed
  521. * @throws Exception
  522. * @throws \yii\base\InvalidConfigException
  523. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html
  524. */
  525. public function openIndex($index)
  526. {
  527. return $this->db->post([$index, '_open']);
  528. }
  529. /**
  530. * @param string $index Index that the document belongs to.
  531. * @return mixed
  532. * @throws Exception
  533. * @throws \yii\base\InvalidConfigException
  534. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html
  535. */
  536. public function closeIndex($index)
  537. {
  538. return $this->db->post([$index, '_close']);
  539. }
  540. /**
  541. * @param array $options URL options
  542. * @return mixed
  543. * @throws Exception
  544. * @throws \yii\base\InvalidConfigException
  545. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
  546. * @since 2.0.4
  547. */
  548. public function scroll($options = [])
  549. {
  550. $body = array_filter([
  551. 'scroll' => ArrayHelper::remove($options, 'scroll', null),
  552. 'scroll_id' => ArrayHelper::remove($options, 'scroll_id', null),
  553. ]);
  554. if (empty($body)) {
  555. $body = (object) [];
  556. }
  557. return $this->db->post(['_search', 'scroll'], $options, Json::encode($body));
  558. }
  559. /**
  560. * @param array $options URL options
  561. * @return mixed
  562. * @throws Exception
  563. * @throws \yii\base\InvalidConfigException
  564. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
  565. * @since 2.0.4
  566. */
  567. public function clearScroll($options = [])
  568. {
  569. $body = array_filter([
  570. 'scroll_id' => ArrayHelper::remove($options, 'scroll_id', null),
  571. ]);
  572. if (empty($body)) {
  573. $body = (object) [];
  574. }
  575. return $this->db->delete(['_search', 'scroll'], $options, Json::encode($body));
  576. }
  577. /**
  578. * @param string $index Index that the document belongs to.
  579. * @return mixed
  580. * @throws Exception
  581. * @throws \yii\base\InvalidConfigException
  582. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html
  583. */
  584. public function getIndexStats($index = '_all')
  585. {
  586. return $this->db->get([$index, '_stats']);
  587. }
  588. /**
  589. * @param string $index Index that the document belongs to.
  590. * @return mixed
  591. * @throws Exception
  592. * @throws \yii\base\InvalidConfigException
  593. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-recovery.html
  594. */
  595. public function getIndexRecoveryStats($index = '_all')
  596. {
  597. return $this->db->get([$index, '_recovery']);
  598. }
  599. // https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-segments.html
  600. /**
  601. * @param string $index Index that the document belongs to.
  602. * @return mixed
  603. * @throws Exception
  604. * @throws \yii\base\InvalidConfigException
  605. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-clearcache.html
  606. */
  607. public function clearIndexCache($index)
  608. {
  609. return $this->db->post([$index, '_cache', 'clear']);
  610. }
  611. /**
  612. * @param string $index Index that the document belongs to.
  613. * @return mixed
  614. * @throws Exception
  615. * @throws \yii\base\InvalidConfigException
  616. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-flush.html
  617. */
  618. public function flushIndex($index = '_all')
  619. {
  620. return $this->db->post([$index, '_flush']);
  621. }
  622. /**
  623. * @param string $index Index that the document belongs to.
  624. * @return mixed
  625. * @throws Exception
  626. * @throws \yii\base\InvalidConfigException
  627. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html
  628. */
  629. public function refreshIndex($index)
  630. {
  631. return $this->db->post([$index, '_refresh']);
  632. }
  633. // TODO https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-optimize.html
  634. // TODO https://www.elastic.co/guide/en/elasticsearch/reference/0.90/indices-gateway-snapshot.html
  635. /**
  636. * @param string $index Index that the document belongs to.
  637. * @param string|null $type Type that the document belongs to.
  638. * @param string|array $mapping
  639. * @param array $options URL options
  640. * @return mixed
  641. * @throws Exception
  642. * @throws \yii\base\InvalidConfigException
  643. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html
  644. */
  645. public function setMapping($index, $type, $mapping, $options = [])
  646. {
  647. $body = $mapping !== null ? (is_string($mapping) ? $mapping : Json::encode($mapping)) : null;
  648. if ($this->db->dslVersion >= 7) {
  649. $endpoint = [$index, '_mapping'];
  650. } else {
  651. $endpoint = [$index, '_mapping', $type];
  652. }
  653. return $this->db->put($endpoint, $options, $body);
  654. }
  655. /**
  656. * @param string $index Index that the document belongs to.
  657. * @param string|null $type Type that the document belongs to.
  658. * @return mixed
  659. * @throws Exception
  660. * @throws \yii\base\InvalidConfigException
  661. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html
  662. */
  663. public function getMapping($index = '_all', $type = null)
  664. {
  665. $url = [$index, '_mapping'];
  666. if ($this->db->dslVersion < 7 && $type !== null) {
  667. $url[] = $type;
  668. }
  669. return $this->db->get($url);
  670. }
  671. /**
  672. * @param string $index Index that the document belongs to.
  673. * @param string $type
  674. * @return mixed
  675. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html
  676. */
  677. // public function getFieldMapping($index, $type = '_all')
  678. // {
  679. // // TODO implement
  680. // return $this->db->put([$index, $type, '_mapping']);
  681. // }
  682. /**
  683. * @param $options
  684. * @param string $index Index that the document belongs to.
  685. * @return mixed
  686. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html
  687. */
  688. // public function analyze($options, $index = null)
  689. // {
  690. // // TODO implement
  691. //// return $this->db->put([$index]);
  692. // }
  693. /**
  694. * @param $name
  695. * @param $pattern
  696. * @param $settings
  697. * @param $mappings
  698. * @param int $order
  699. * @return mixed
  700. * @throws Exception
  701. * @throws \yii\base\InvalidConfigException
  702. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
  703. */
  704. public function createTemplate($name, $pattern, $settings, $mappings, $order = 0)
  705. {
  706. $body = Json::encode([
  707. 'template' => $pattern,
  708. 'order' => $order,
  709. 'settings' => (object) $settings,
  710. 'mappings' => (object) $mappings,
  711. ]);
  712. return $this->db->put(['_template', $name], [], $body);
  713. }
  714. /**
  715. * @param $name
  716. * @return mixed
  717. * @throws Exception
  718. * @throws \yii\base\InvalidConfigException
  719. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
  720. */
  721. public function deleteTemplate($name)
  722. {
  723. return $this->db->delete(['_template', $name]);
  724. }
  725. /**
  726. * @param $name
  727. * @return mixed
  728. * @throws Exception
  729. * @throws \yii\base\InvalidConfigException
  730. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
  731. */
  732. public function getTemplate($name)
  733. {
  734. return $this->db->get(['_template', $name]);
  735. }
  736. }