Settings.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace Elastica\Index;
  3. use Elastica\Exception\NotFoundException;
  4. use Elastica\Exception\ResponseException;
  5. use Elastica\Index as BaseIndex;
  6. use Elastica\Request;
  7. /**
  8. * Elastica index settings object.
  9. *
  10. * All settings listed in the update settings API (https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html)
  11. * can be changed on a running indices. To make changes like the merge policy (https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-merge.html)
  12. * the index has to be closed first and reopened after the call
  13. *
  14. * @author Nicolas Ruflin <spam@ruflin.com>
  15. *
  16. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
  17. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-merge.html
  18. */
  19. class Settings
  20. {
  21. const DEFAULT_REFRESH_INTERVAL = '1s';
  22. const DEFAULT_NUMBER_OF_REPLICAS = 1;
  23. const DEFAULT_NUMBER_OF_SHARDS = 5;
  24. /**
  25. * Response.
  26. *
  27. * @var \Elastica\Response Response object
  28. */
  29. protected $_response;
  30. /**
  31. * Stats info.
  32. *
  33. * @var array Stats info
  34. */
  35. protected $_data = [];
  36. /**
  37. * Index.
  38. *
  39. * @var \Elastica\Index Index object
  40. */
  41. protected $_index;
  42. /**
  43. * Construct.
  44. *
  45. * @param \Elastica\Index $index Index object
  46. */
  47. public function __construct(BaseIndex $index)
  48. {
  49. $this->_index = $index;
  50. }
  51. /**
  52. * Returns the current settings of the index.
  53. *
  54. * If param is set, only specified setting is return.
  55. * 'index.' is added in front of $setting.
  56. *
  57. * @param string $setting OPTIONAL Setting name to return
  58. *
  59. * @return array|string|null Settings data
  60. *
  61. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
  62. */
  63. public function get($setting = '')
  64. {
  65. $requestData = $this->request()->getData();
  66. $data = reset($requestData);
  67. if (empty($data['settings']) || empty($data['settings']['index'])) {
  68. // should not append, the request should throw a ResponseException
  69. throw new NotFoundException('Index '.$this->getIndex()->getName().' not found');
  70. }
  71. $settings = $data['settings']['index'];
  72. if (!$setting) {
  73. // return all array
  74. return $settings;
  75. }
  76. if (isset($settings[$setting])) {
  77. return $settings[$setting];
  78. }
  79. if (false !== strpos($setting, '.')) {
  80. // translate old dot-notation settings to nested arrays
  81. $keys = explode('.', $setting);
  82. foreach ($keys as $key) {
  83. if (isset($settings[$key])) {
  84. $settings = $settings[$key];
  85. } else {
  86. return;
  87. }
  88. }
  89. return $settings;
  90. }
  91. return;
  92. }
  93. /**
  94. * Returns a setting interpreted as a bool.
  95. *
  96. * One can use a real bool, int(0), int(1) to set bool settings.
  97. * But Elasticsearch stores and returns all settings as strings and does
  98. * not normalize bool values. This method ensures a bool is returned for
  99. * whichever string representation is used like 'true', '1', 'on', 'yes'.
  100. *
  101. * @param string $setting Setting name to return
  102. *
  103. * @return bool
  104. */
  105. public function getBool($setting)
  106. {
  107. $data = $this->get($setting);
  108. return 'true' === $data || '1' === $data || 'on' === $data || 'yes' === $data;
  109. }
  110. /**
  111. * Sets the number of replicas.
  112. *
  113. * @param int $replicas Number of replicas
  114. *
  115. * @return \Elastica\Response Response object
  116. */
  117. public function setNumberOfReplicas($replicas)
  118. {
  119. return $this->set(['number_of_replicas' => (int) $replicas]);
  120. }
  121. /**
  122. * Returns the number of replicas.
  123. *
  124. * If no number of replicas is set, the default number is returned
  125. *
  126. * @return int The number of replicas
  127. */
  128. public function getNumberOfReplicas()
  129. {
  130. $replicas = $this->get('number_of_replicas');
  131. if (null === $replicas) {
  132. $replicas = self::DEFAULT_NUMBER_OF_REPLICAS;
  133. }
  134. return $replicas;
  135. }
  136. /**
  137. * Returns the number of shards.
  138. *
  139. * If no number of shards is set, the default number is returned
  140. *
  141. * @return int The number of shards
  142. */
  143. public function getNumberOfShards()
  144. {
  145. $shards = $this->get('number_of_shards');
  146. if (null === $shards) {
  147. $shards = self::DEFAULT_NUMBER_OF_SHARDS;
  148. }
  149. return $shards;
  150. }
  151. /**
  152. * Sets the index to read only.
  153. *
  154. * @param bool $readOnly (default = true)
  155. *
  156. * @return \Elastica\Response
  157. */
  158. public function setReadOnly($readOnly = true)
  159. {
  160. return $this->set(['blocks.read_only' => $readOnly]);
  161. }
  162. /**
  163. * @return bool
  164. */
  165. public function getReadOnly()
  166. {
  167. return $this->getBool('blocks.read_only');
  168. }
  169. /**
  170. * @return bool
  171. */
  172. public function getBlocksRead()
  173. {
  174. return $this->getBool('blocks.read');
  175. }
  176. /**
  177. * @param bool $state OPTIONAL (default = true)
  178. *
  179. * @return \Elastica\Response
  180. */
  181. public function setBlocksRead($state = true)
  182. {
  183. return $this->set(['blocks.read' => $state]);
  184. }
  185. /**
  186. * @return bool
  187. */
  188. public function getBlocksWrite()
  189. {
  190. return $this->getBool('blocks.write');
  191. }
  192. /**
  193. * @param bool $state OPTIONAL (default = true)
  194. *
  195. * @return \Elastica\Response
  196. */
  197. public function setBlocksWrite($state = true)
  198. {
  199. return $this->set(['blocks.write' => $state]);
  200. }
  201. /**
  202. * @return bool
  203. */
  204. public function getBlocksMetadata()
  205. {
  206. // When blocks.metadata is enabled, reading the settings is not possible anymore.
  207. // So when a cluster_block_exception happened it must be enabled.
  208. try {
  209. return $this->getBool('blocks.metadata');
  210. } catch (ResponseException $e) {
  211. if ('cluster_block_exception' === $e->getResponse()->getFullError()['type']) {
  212. return true;
  213. }
  214. throw $e;
  215. }
  216. }
  217. /**
  218. * Set to true to disable index metadata reads and writes.
  219. *
  220. * @param bool $state OPTIONAL (default = true)
  221. *
  222. * @return \Elastica\Response
  223. */
  224. public function setBlocksMetadata($state = true)
  225. {
  226. return $this->set(['blocks.metadata' => $state]);
  227. }
  228. /**
  229. * Sets the index refresh interval.
  230. *
  231. * Value can be for example 3s for 3 seconds or
  232. * 5m for 5 minutes. -1 to disabled refresh.
  233. *
  234. * @param string $interval Duration of the refresh interval
  235. *
  236. * @return \Elastica\Response Response object
  237. */
  238. public function setRefreshInterval($interval)
  239. {
  240. return $this->set(['refresh_interval' => $interval]);
  241. }
  242. /**
  243. * Returns the refresh interval.
  244. *
  245. * If no interval is set, the default interval is returned
  246. *
  247. * @return string Refresh interval
  248. */
  249. public function getRefreshInterval()
  250. {
  251. $interval = $this->get('refresh_interval');
  252. if (empty($interval)) {
  253. $interval = self::DEFAULT_REFRESH_INTERVAL;
  254. }
  255. return $interval;
  256. }
  257. /**
  258. * Sets the specific merge policies.
  259. *
  260. * To have this changes made the index has to be closed and reopened
  261. *
  262. * @param string $key Merge policy key (for ex. expunge_deletes_allowed)
  263. * @param string $value
  264. *
  265. * @return \Elastica\Response
  266. *
  267. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-merge.html
  268. */
  269. public function setMergePolicy($key, $value)
  270. {
  271. $this->getIndex()->close();
  272. $response = $this->set(['merge.policy.'.$key => $value]);
  273. $this->getIndex()->open();
  274. return $response;
  275. }
  276. /**
  277. * Returns the specific merge policy value.
  278. *
  279. * @param string $key Merge policy key (for ex. expunge_deletes_allowed)
  280. *
  281. * @return string Refresh interval
  282. *
  283. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules-merge.html
  284. */
  285. public function getMergePolicy($key)
  286. {
  287. $settings = $this->get();
  288. if (isset($settings['merge']['policy'][$key])) {
  289. return $settings['merge']['policy'][$key];
  290. }
  291. return;
  292. }
  293. /**
  294. * Can be used to set/update settings.
  295. *
  296. * @param array $data Arguments
  297. *
  298. * @return \Elastica\Response Response object
  299. */
  300. public function set(array $data)
  301. {
  302. return $this->request($data, Request::PUT);
  303. }
  304. /**
  305. * Returns the index object.
  306. *
  307. * @return \Elastica\Index Index object
  308. */
  309. public function getIndex()
  310. {
  311. return $this->_index;
  312. }
  313. /**
  314. * Updates the given settings for the index.
  315. *
  316. * With elasticsearch 0.16 the following settings are supported
  317. * - index.term_index_interval
  318. * - index.term_index_divisor
  319. * - index.translog.flush_threshold_ops
  320. * - index.translog.flush_threshold_size
  321. * - index.translog.flush_threshold_period
  322. * - index.refresh_interval
  323. * - index.merge.policy
  324. * - index.auto_expand_replicas
  325. *
  326. * @param array $data OPTIONAL Data array
  327. * @param string $method OPTIONAL Transfer method (default = \Elastica\Request::GET)
  328. *
  329. * @return \Elastica\Response Response object
  330. */
  331. public function request(array $data = [], $method = Request::GET)
  332. {
  333. $path = '_settings';
  334. if (!empty($data)) {
  335. $data = ['index' => $data];
  336. }
  337. return $this->getIndex()->request($path, $method, $data);
  338. }
  339. }