Client.php 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. <?php
  2. namespace Elasticsearch;
  3. use Elasticsearch\Common\Exceptions\BadMethodCallException;
  4. use Elasticsearch\Common\Exceptions\InvalidArgumentException;
  5. use Elasticsearch\Common\Exceptions\NoNodesAvailableException;
  6. use Elasticsearch\Common\Exceptions\BadRequest400Exception;
  7. use Elasticsearch\Common\Exceptions\Missing404Exception;
  8. use Elasticsearch\Common\Exceptions\TransportException;
  9. use Elasticsearch\Endpoints\AbstractEndpoint;
  10. use Elasticsearch\Namespaces\AbstractNamespace;
  11. use Elasticsearch\Namespaces\CatNamespace;
  12. use Elasticsearch\Namespaces\ClusterNamespace;
  13. use Elasticsearch\Namespaces\IndicesNamespace;
  14. use Elasticsearch\Namespaces\IngestNamespace;
  15. use Elasticsearch\Namespaces\NamespaceBuilderInterface;
  16. use Elasticsearch\Namespaces\NodesNamespace;
  17. use Elasticsearch\Namespaces\RemoteNamespace;
  18. use Elasticsearch\Namespaces\SnapshotNamespace;
  19. use Elasticsearch\Namespaces\BooleanRequestWrapper;
  20. use Elasticsearch\Namespaces\TasksNamespace;
  21. /**
  22. * Class Client
  23. *
  24. * @category Elasticsearch
  25. * @package Elasticsearch
  26. * @author Zachary Tong <zach@elastic.co>
  27. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
  28. * @link http://elastic.co
  29. */
  30. class Client
  31. {
  32. /**
  33. * @var Transport
  34. */
  35. public $transport;
  36. /**
  37. * @var array
  38. */
  39. protected $params;
  40. /**
  41. * @var IndicesNamespace
  42. */
  43. protected $indices;
  44. /**
  45. * @var ClusterNamespace
  46. */
  47. protected $cluster;
  48. /**
  49. * @var NodesNamespace
  50. */
  51. protected $nodes;
  52. /**
  53. * @var SnapshotNamespace
  54. */
  55. protected $snapshot;
  56. /**
  57. * @var CatNamespace
  58. */
  59. protected $cat;
  60. /**
  61. * @var IngestNamespace
  62. */
  63. protected $ingest;
  64. /**
  65. * @var TasksNamespace
  66. */
  67. protected $tasks;
  68. /**
  69. * @var RemoteNamespace
  70. */
  71. protected $remote;
  72. /** @var callback */
  73. protected $endpoints;
  74. /** @var NamespaceBuilderInterface[] */
  75. protected $registeredNamespaces = [];
  76. /**
  77. * Client constructor
  78. *
  79. * @param Transport $transport
  80. * @param callable $endpoint
  81. * @param AbstractNamespace[] $registeredNamespaces
  82. */
  83. public function __construct(Transport $transport, callable $endpoint, array $registeredNamespaces)
  84. {
  85. $this->transport = $transport;
  86. $this->endpoints = $endpoint;
  87. $this->indices = new IndicesNamespace($transport, $endpoint);
  88. $this->cluster = new ClusterNamespace($transport, $endpoint);
  89. $this->nodes = new NodesNamespace($transport, $endpoint);
  90. $this->snapshot = new SnapshotNamespace($transport, $endpoint);
  91. $this->cat = new CatNamespace($transport, $endpoint);
  92. $this->ingest = new IngestNamespace($transport, $endpoint);
  93. $this->tasks = new TasksNamespace($transport, $endpoint);
  94. $this->remote = new RemoteNamespace($transport, $endpoint);
  95. $this->registeredNamespaces = $registeredNamespaces;
  96. }
  97. /**
  98. * @param $params
  99. * @return array
  100. */
  101. public function info($params = [])
  102. {
  103. /** @var callback $endpointBuilder */
  104. $endpointBuilder = $this->endpoints;
  105. /** @var \Elasticsearch\Endpoints\Info $endpoint */
  106. $endpoint = $endpointBuilder('Info');
  107. $endpoint->setParams($params);
  108. return $this->performRequest($endpoint);
  109. }
  110. /**
  111. * @param $params array Associative array of parameters
  112. *
  113. * @return bool
  114. */
  115. public function ping($params = [])
  116. {
  117. /** @var callback $endpointBuilder */
  118. $endpointBuilder = $this->endpoints;
  119. /** @var \Elasticsearch\Endpoints\Ping $endpoint */
  120. $endpoint = $endpointBuilder('Ping');
  121. $endpoint->setParams($params);
  122. try {
  123. $this->performRequest($endpoint);
  124. } catch (Missing404Exception $exception) {
  125. return false;
  126. } catch (TransportException $exception) {
  127. return false;
  128. } catch (NoNodesAvailableException $exception) {
  129. return false;
  130. }
  131. return true;
  132. }
  133. /**
  134. * $params['id'] = (string) The document ID (Required)
  135. * ['index'] = (string) The name of the index (Required)
  136. * ['type'] = (string) The type of the document (use `_all` to fetch the first document matching the ID across all types) (Required)
  137. * ['ignore_missing'] = ??
  138. * ['fields'] = (list) A comma-separated list of fields to return in the response
  139. * ['parent'] = (string) The ID of the parent document
  140. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
  141. * ['realtime'] = (boolean) Specify whether to perform the operation in realtime or search mode
  142. * ['refresh'] = (boolean) Refresh the shard containing the document before performing the operation
  143. * ['routing'] = (string) Specific routing value
  144. * ['_source'] = (list) True or false to return the _source field or not, or a list of fields to return
  145. * ['_source_exclude'] = (list) A list of fields to exclude from the returned _source field
  146. * ['_source_include'] = (list) A list of fields to extract and return from the _source field
  147. *
  148. * @param $params array Associative array of parameters
  149. *
  150. * @return array
  151. */
  152. public function get($params)
  153. {
  154. $id = $this->extractArgument($params, 'id');
  155. $index = $this->extractArgument($params, 'index');
  156. $type = $this->extractArgument($params, 'type');
  157. /** @var callback $endpointBuilder */
  158. $endpointBuilder = $this->endpoints;
  159. /** @var \Elasticsearch\Endpoints\Get $endpoint */
  160. $endpoint = $endpointBuilder('Get');
  161. $endpoint->setID($id)
  162. ->setIndex($index)
  163. ->setType($type);
  164. $endpoint->setParams($params);
  165. return $this->performRequest($endpoint);
  166. }
  167. /**
  168. * $params['id'] = (string) The document ID (Required)
  169. * ['index'] = (string) The name of the index (Required)
  170. * ['type'] = (string) The type of the document (use `_all` to fetch the first document matching the ID across all types) (Required)
  171. * ['ignore_missing'] = ??
  172. * ['parent'] = (string) The ID of the parent document
  173. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
  174. * ['realtime'] = (boolean) Specify whether to perform the operation in realtime or search mode
  175. * ['refresh'] = (boolean) Refresh the shard containing the document before performing the operation
  176. * ['routing'] = (string) Specific routing value
  177. *
  178. * @param $params array Associative array of parameters
  179. *
  180. * @return array
  181. */
  182. public function getSource($params)
  183. {
  184. $id = $this->extractArgument($params, 'id');
  185. $index = $this->extractArgument($params, 'index');
  186. $type = $this->extractArgument($params, 'type');
  187. /** @var callback $endpointBuilder */
  188. $endpointBuilder = $this->endpoints;
  189. /** @var \Elasticsearch\Endpoints\Get $endpoint */
  190. $endpoint = $endpointBuilder('Get');
  191. $endpoint->setID($id)
  192. ->setIndex($index)
  193. ->setType($type)
  194. ->returnOnlySource();
  195. $endpoint->setParams($params);
  196. return $this->performRequest($endpoint);
  197. }
  198. /**
  199. * $params['id'] = (string) The document ID (Required)
  200. * ['index'] = (string) The name of the index (Required)
  201. * ['type'] = (string) The type of the document (Required)
  202. * ['consistency'] = (enum) Specific write consistency setting for the operation
  203. * ['parent'] = (string) ID of parent document
  204. * ['refresh'] = (boolean) Refresh the index after performing the operation
  205. * ['replication'] = (enum) Specific replication type
  206. * ['routing'] = (string) Specific routing value
  207. * ['timeout'] = (time) Explicit operation timeout
  208. * ['version_type'] = (enum) Specific version type
  209. *
  210. * @param $params array Associative array of parameters
  211. *
  212. * @return array
  213. */
  214. public function delete($params)
  215. {
  216. $id = $this->extractArgument($params, 'id');
  217. $index = $this->extractArgument($params, 'index');
  218. $type = $this->extractArgument($params, 'type');
  219. $this->verifyNotNullOrEmpty("id", $id);
  220. $this->verifyNotNullOrEmpty("type", $type);
  221. $this->verifyNotNullOrEmpty("index", $index);
  222. /** @var callback $endpointBuilder */
  223. $endpointBuilder = $this->endpoints;
  224. /** @var \Elasticsearch\Endpoints\Delete $endpoint */
  225. $endpoint = $endpointBuilder('Delete');
  226. $endpoint->setID($id)
  227. ->setIndex($index)
  228. ->setType($type);
  229. $endpoint->setParams($params);
  230. return $this->performRequest($endpoint);
  231. }
  232. /**
  233. *
  234. * $params['_source'] = (list) True or false to return the _source field or not, or a list of fields to return
  235. * ['_source_exclude'] = (array) A list of fields to exclude from the returned _source field
  236. * ['_source_include'] = (array) A list of fields to extract and return from the _source field
  237. * ['allow_no_indices'] = (bool) Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
  238. * ['analyze_wildcard'] = (bool) Specify whether wildcard and prefix queries should be analyzed (default: false)
  239. * ['analyzer'] = (string) The analyzer to use for the query string
  240. * ['conflicts'] = (enum) What to do when the delete-by-query hits version conflicts?
  241. * ['default_operator'] = (enum) The default operator for query string query (AND or OR)
  242. * ['df'] = (string) The field to use as default where no field prefix is given in the query string
  243. * ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are open, closed or both.
  244. * ['from'] = (number) Starting offset (default: 0)
  245. * ['ignore_unavailable'] = (bool) Whether specified concrete indices should be ignored when unavailable (missing or closed)
  246. * ['lenient'] = (bool) Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
  247. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
  248. * ['q'] = (string) Query in the Lucene query string syntax
  249. * ['refresh'] = (bool) Should the effected indexes be refreshed?
  250. * ['request_cache'] = (bool) Specify if request cache should be used for this request or not, defaults to index level setting
  251. * ['requests_per_second'] = (number) The throttle for this request in sub-requests per second. -1 means no throttle.
  252. * ['routing'] = (array) A comma-separated list of specific routing values
  253. * ['scroll'] = (number) Specify how long a consistent view of the index should be maintained for scrolled search
  254. * ['scroll_size'] = (number) Size on the scroll request powering the update_by_query
  255. * ['search_timeout'] = (number) Explicit timeout for each search request. Defaults to no timeout.
  256. * ['search_type'] = (enum) Search operation type
  257. * ['size'] = (number) Number of hits to return (default: 10)
  258. * ['slices'] = (integer) The number of slices this task should be divided into. Defaults to 1 meaning the task isn't sliced into subtasks.
  259. * ['sort'] = (array) A comma-separated list of <field>:<direction> pairs
  260. * ['stats'] = (array) Specific 'tag' of the request for logging and statistical purposes
  261. * ['terminate_after'] = (number) The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.
  262. * ['timeout'] = (number) Time each individual bulk request should wait for shards that are unavailable.
  263. * ['version'] = (bool) Specify whether to return document version as part of a hit
  264. * ['wait_for_active_shards'] = (string) Sets the number of shard copies that must be active before proceeding with the delete by query operation. Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1)
  265. * ['wait_for_completion'] = (bool) Should the request should block until the delete-by-query is complete.
  266. *
  267. * @param array $params
  268. *
  269. * @return array
  270. */
  271. public function deleteByQuery($params = array())
  272. {
  273. $index = $this->extractArgument($params, 'index');
  274. $type = $this->extractArgument($params, 'type');
  275. $body = $this->extractArgument($params, 'body');
  276. /** @var callback $endpointBuilder */
  277. $endpointBuilder = $this->endpoints;
  278. /** @var \Elasticsearch\Endpoints\DeleteByQuery $endpoint */
  279. $endpoint = $endpointBuilder('DeleteByQuery');
  280. $endpoint->setIndex($index)
  281. ->setType($type)
  282. ->setBody($body);
  283. $endpoint->setParams($params);
  284. return $this->performRequest($endpoint);
  285. }
  286. /**
  287. * $params['index'] = (list) A comma-separated list of indices to restrict the results
  288. * ['type'] = (list) A comma-separated list of types to restrict the results
  289. * ['min_score'] = (number) Include only documents with a specific `_score` value in the result
  290. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
  291. * ['routing'] = (string) Specific routing value
  292. * ['source'] = (string) The URL-encoded query definition (instead of using the request body)
  293. * ['body'] = (array) A query to restrict the results (optional)
  294. * ['ignore_unavailable'] = (bool) Whether specified concrete indices should be ignored when unavailable (missing or closed)
  295. * ['allow_no_indices'] = (bool) Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
  296. * ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are open, closed or both.
  297. *
  298. * @param $params array Associative array of parameters
  299. *
  300. * @return array
  301. */
  302. public function count($params = array())
  303. {
  304. $index = $this->extractArgument($params, 'index');
  305. $type = $this->extractArgument($params, 'type');
  306. $body = $this->extractArgument($params, 'body');
  307. /** @var callback $endpointBuilder */
  308. $endpointBuilder = $this->endpoints;
  309. /** @var \Elasticsearch\Endpoints\Count $endpoint */
  310. $endpoint = $endpointBuilder('Count');
  311. $endpoint->setIndex($index)
  312. ->setType($type)
  313. ->setBody($body);
  314. $endpoint->setParams($params);
  315. return $this->performRequest($endpoint);
  316. }
  317. /**
  318. * $params['index'] = (list) A comma-separated list of indices to restrict the results
  319. * ['type'] = (list) A comma-separated list of types to restrict the results
  320. * ['id'] = (string) ID of document
  321. * ['ignore_unavailable'] = (boolean) Whether specified concrete indices should be ignored when unavailable (missing or closed)
  322. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
  323. * ['routing'] = (string) Specific routing value
  324. * ['allow_no_indices'] = (boolean) Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
  325. * ['body'] = (array) A query to restrict the results (optional)
  326. * ['ignore_unavailable'] = (bool) Whether specified concrete indices should be ignored when unavailable (missing or closed)
  327. * ['percolate_index'] = (string) The index to count percolate the document into. Defaults to index.
  328. * ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are open, closed or both.
  329. * ['version'] = (number) Explicit version number for concurrency control
  330. * ['version_type'] = (enum) Specific version type
  331. *
  332. * @param $params array Associative array of parameters
  333. *
  334. * @return array
  335. *
  336. * @deprecated
  337. */
  338. public function countPercolate($params = array())
  339. {
  340. $index = $this->extractArgument($params, 'index');
  341. $type = $this->extractArgument($params, 'type');
  342. $id = $this->extractArgument($params, 'id');
  343. $body = $this->extractArgument($params, 'body');
  344. /** @var callback $endpointBuilder */
  345. $endpointBuilder = $this->endpoints;
  346. /** @var \Elasticsearch\Endpoints\CountPercolate $endpoint */
  347. $endpoint = $endpointBuilder('CountPercolate');
  348. $endpoint->setIndex($index)
  349. ->setType($type)
  350. ->setID($id)
  351. ->setBody($body);
  352. $endpoint->setParams($params);
  353. return $this->performRequest($endpoint);
  354. }
  355. /**
  356. * $params['index'] = (string) The name of the index with a registered percolator query (Required)
  357. * ['type'] = (string) The document type (Required)
  358. * ['prefer_local'] = (boolean) With `true`, specify that a local shard should be used if available, with `false`, use a random shard (default: true)
  359. * ['body'] = (array) The document (`doc`) to percolate against registered queries; optionally also a `query` to limit the percolation to specific registered queries
  360. *
  361. * @param $params array Associative array of parameters
  362. *
  363. * @return array
  364. *
  365. * @deprecated
  366. */
  367. public function percolate($params)
  368. {
  369. $index = $this->extractArgument($params, 'index');
  370. $type = $this->extractArgument($params, 'type');
  371. $id = $this->extractArgument($params, 'id');
  372. $body = $this->extractArgument($params, 'body');
  373. /** @var callback $endpointBuilder */
  374. $endpointBuilder = $this->endpoints;
  375. /** @var \Elasticsearch\Endpoints\Percolate $endpoint */
  376. $endpoint = $endpointBuilder('Percolate');
  377. $endpoint->setIndex($index)
  378. ->setType($type)
  379. ->setID($id)
  380. ->setBody($body);
  381. $endpoint->setParams($params);
  382. return $this->performRequest($endpoint);
  383. }
  384. /**
  385. * $params['index'] = (string) Default index for items which don't provide one
  386. * ['type'] = (string) Default document type for items which don't provide one
  387. * ['ignore_unavailable'] = (boolean) Whether specified concrete indices should be ignored when unavailable (missing or closed)
  388. * ['allow_no_indices'] = (boolean) Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
  389. * ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are open, closed or both.
  390. *
  391. * @param $params array Associative array of parameters
  392. *
  393. * @return array
  394. *
  395. * @deprecated
  396. */
  397. public function mpercolate($params = array())
  398. {
  399. $index = $this->extractArgument($params, 'index');
  400. $type = $this->extractArgument($params, 'type');
  401. $body = $this->extractArgument($params, 'body');
  402. /** @var callback $endpointBuilder */
  403. $endpointBuilder = $this->endpoints;
  404. /** @var \Elasticsearch\Endpoints\MPercolate $endpoint */
  405. $endpoint = $endpointBuilder('MPercolate');
  406. $endpoint->setIndex($index)
  407. ->setType($type)
  408. ->setBody($body);
  409. $endpoint->setParams($params);
  410. return $this->performRequest($endpoint);
  411. }
  412. /**
  413. * $params['index'] = (string) Default index for items which don't provide one
  414. * ['type'] = (string) Default document type for items which don't provide one
  415. * ['term_statistics'] = (boolean) Specifies if total term frequency and document frequency should be returned. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\"."
  416. * ['field_statistics'] = (boolean) Specifies if document count, sum of document frequencies and sum of total term frequencies should be returned. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\"."
  417. * ['fields'] = (list) A comma-separated list of fields to return. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\"."
  418. * ['offsets'] = (boolean) Specifies if term offsets should be returned. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\"."
  419. * ['positions'] = (boolean) Specifies if term positions should be returned. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\"."
  420. * ['payloads'] = (boolean) Specifies if term payloads should be returned. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\".
  421. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random) .Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\".
  422. * ['routing'] = (string) Specific routing value. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\".
  423. * ['parent'] = (string) Parent id of documents. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\".
  424. * ['realtime'] = (boolean) Specifies if request is real-time as opposed to near-real-time (default: true).
  425. *
  426. * @param $params array Associative array of parameters
  427. *
  428. * @return array
  429. */
  430. public function termvectors($params = array())
  431. {
  432. $index = $this->extractArgument($params, 'index');
  433. $type = $this->extractArgument($params, 'type');
  434. $id = $this->extractArgument($params, 'id');
  435. $body = $this->extractArgument($params, 'body');
  436. /** @var callback $endpointBuilder */
  437. $endpointBuilder = $this->endpoints;
  438. /** @var \Elasticsearch\Endpoints\TermVectors $endpoint */
  439. $endpoint = $endpointBuilder('TermVectors');
  440. $endpoint->setIndex($index)
  441. ->setType($type)
  442. ->setID($id)
  443. ->setBody($body);
  444. $endpoint->setParams($params);
  445. return $this->performRequest($endpoint);
  446. }
  447. /**
  448. * $params['index'] = (string) Default index for items which don't provide one
  449. * ['type'] = (string) Default document type for items which don't provide one
  450. * ['ids'] = (list) A comma-separated list of documents ids. You must define ids as parameter or set \"ids\" or \"docs\" in the request body
  451. * ['term_statistics'] = (boolean) Specifies if total term frequency and document frequency should be returned. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\"."
  452. * ['field_statistics'] = (boolean) Specifies if document count, sum of document frequencies and sum of total term frequencies should be returned. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\"."
  453. * ['fields'] = (list) A comma-separated list of fields to return. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\"."
  454. * ['offsets'] = (boolean) Specifies if term offsets should be returned. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\"."
  455. * ['positions'] = (boolean) Specifies if term positions should be returned. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\"."
  456. * ['payloads'] = (boolean) Specifies if term payloads should be returned. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\".
  457. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random) .Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\".
  458. * ['routing'] = (string) Specific routing value. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\".
  459. * ['parent'] = (string) Parent id of documents. Applies to all returned documents unless otherwise specified in body \"params\" or \"docs\".
  460. * ['realtime'] = (boolean) Specifies if request is real-time as opposed to near-real-time (default: true).
  461. *
  462. * @param $params array Associative array of parameters
  463. *
  464. * @return array
  465. */
  466. public function mtermvectors($params = array())
  467. {
  468. $index = $this->extractArgument($params, 'index');
  469. $type = $this->extractArgument($params, 'type');
  470. $body = $this->extractArgument($params, 'body');
  471. /** @var callback $endpointBuilder */
  472. $endpointBuilder = $this->endpoints;
  473. /** @var \Elasticsearch\Endpoints\MTermVectors $endpoint */
  474. $endpoint = $endpointBuilder('MTermVectors');
  475. $endpoint->setIndex($index)
  476. ->setType($type)
  477. ->setBody($body);
  478. $endpoint->setParams($params);
  479. return $this->performRequest($endpoint);
  480. }
  481. /**
  482. * $params['id'] = (string) The document ID (Required)
  483. * ['index'] = (string) The name of the index (Required)
  484. * ['type'] = (string) The type of the document (use `_all` to fetch the first document matching the ID across all types) (Required)
  485. * ['parent'] = (string) The ID of the parent document
  486. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
  487. * ['realtime'] = (boolean) Specify whether to perform the operation in realtime or search mode
  488. * ['refresh'] = (boolean) Refresh the shard containing the document before performing the operation
  489. * ['routing'] = (string) Specific routing value
  490. *
  491. * @param $params array Associative array of parameters
  492. *
  493. * @return array | boolean
  494. */
  495. public function exists($params)
  496. {
  497. $id = $this->extractArgument($params, 'id');
  498. $index = $this->extractArgument($params, 'index');
  499. $type = $this->extractArgument($params, 'type');
  500. //manually make this verbose so we can check status code
  501. $params['client']['verbose'] = true;
  502. /** @var callback $endpointBuilder */
  503. $endpointBuilder = $this->endpoints;
  504. /** @var \Elasticsearch\Endpoints\Exists $endpoint */
  505. $endpoint = $endpointBuilder('Exists');
  506. $endpoint->setID($id)
  507. ->setIndex($index)
  508. ->setType($type);
  509. $endpoint->setParams($params);
  510. return BooleanRequestWrapper::performRequest($endpoint, $this->transport);
  511. }
  512. /**
  513. * $params['index'] = (string) The name of the index
  514. * ['type'] = (string) The type of the document
  515. * ['fields'] = (list) A comma-separated list of fields to return in the response
  516. * ['parent'] = (string) The ID of the parent document
  517. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
  518. * ['realtime'] = (boolean) Specify whether to perform the operation in realtime or search mode
  519. * ['refresh'] = (boolean) Refresh the shard containing the document before performing the operation
  520. * ['routing'] = (string) Specific routing value
  521. * ['body'] = (array) Document identifiers; can be either `docs` (containing full document information) or `ids` (when index and type is provided in the URL.
  522. * ['_source'] = (list) True or false to return the _source field or not, or a list of fields to return
  523. * ['_source_exclude'] = (list) A list of fields to exclude from the returned _source field
  524. * ['_source_include'] = (list) A list of fields to extract and return from the _source field
  525. *
  526. * @param $params array Associative array of parameters
  527. *
  528. * @return array
  529. */
  530. public function mget($params = array())
  531. {
  532. $index = $this->extractArgument($params, 'index');
  533. $type = $this->extractArgument($params, 'type');
  534. $body = $this->extractArgument($params, 'body');
  535. /** @var callback $endpointBuilder */
  536. $endpointBuilder = $this->endpoints;
  537. /** @var \Elasticsearch\Endpoints\Mget $endpoint */
  538. $endpoint = $endpointBuilder('Mget');
  539. $endpoint->setIndex($index)
  540. ->setType($type)
  541. ->setBody($body);
  542. $endpoint->setParams($params);
  543. return $this->performRequest($endpoint);
  544. }
  545. /**
  546. * $params['index'] = (list) A comma-separated list of index names to use as default
  547. * ['type'] = (list) A comma-separated list of document types to use as default
  548. * ['search_type'] = (enum) Search operation type
  549. * ['body'] = (array|string) The request definitions (metadata-search request definition pairs), separated by newlines
  550. *
  551. * @param $params array Associative array of parameters
  552. *
  553. * @return array
  554. */
  555. public function msearch($params = array())
  556. {
  557. $index = $this->extractArgument($params, 'index');
  558. $type = $this->extractArgument($params, 'type');
  559. $body = $this->extractArgument($params, 'body');
  560. /** @var callback $endpointBuilder */
  561. $endpointBuilder = $this->endpoints;
  562. /** @var \Elasticsearch\Endpoints\Msearch $endpoint */
  563. $endpoint = $endpointBuilder('Msearch');
  564. $endpoint->setIndex($index)
  565. ->setType($type)
  566. ->setBody($body);
  567. $endpoint->setParams($params);
  568. return $this->performRequest($endpoint);
  569. }
  570. /**
  571. * $params['index'] = (list) A comma-separated list of index names to use as default
  572. * ['type'] = (list) A comma-separated list of document types to use as default
  573. * ['search_type'] = (enum) Search operation type
  574. * ['body'] = (array|string) The request definitions (metadata-search request definition pairs), separated by newlines
  575. * ['max_concurrent_searches'] = (number) Controls the maximum number of concurrent searches the multi search api will execute
  576. *
  577. * @param $params array Associative array of parameters
  578. *
  579. * @return array
  580. */
  581. public function msearchTemplate($params = array())
  582. {
  583. $index = $this->extractArgument($params, 'index');
  584. $type = $this->extractArgument($params, 'type');
  585. $body = $this->extractArgument($params, 'body');
  586. /** @var callback $endpointBuilder */
  587. $endpointBuilder = $this->endpoints;
  588. /** @var \Elasticsearch\Endpoints\MsearchTemplate $endpoint */
  589. $endpoint = $endpointBuilder('MsearchTemplate');
  590. $endpoint->setIndex($index)
  591. ->setType($type)
  592. ->setBody($body);
  593. $endpoint->setParams($params);
  594. return $this->performRequest($endpoint);
  595. }
  596. /**
  597. * $params['index'] = (string) The name of the index (Required)
  598. * ['type'] = (string) The type of the document (Required)
  599. * ['id'] = (string) Specific document ID (when the POST method is used)
  600. * ['consistency'] = (enum) Explicit write consistency setting for the operation
  601. * ['parent'] = (string) ID of the parent document
  602. * ['refresh'] = (boolean) Refresh the index after performing the operation
  603. * ['replication'] = (enum) Specific replication type
  604. * ['routing'] = (string) Specific routing value
  605. * ['timeout'] = (time) Explicit operation timeout
  606. * ['timestamp'] = (time) Explicit timestamp for the document
  607. * ['ttl'] = (duration) Expiration time for the document
  608. * ['version'] = (number) Explicit version number for concurrency control
  609. * ['version_type'] = (enum) Specific version type
  610. * ['body'] = (array) The document
  611. *
  612. * @param $params array Associative array of parameters
  613. *
  614. * @return array
  615. */
  616. public function create($params)
  617. {
  618. $id = $this->extractArgument($params, 'id');
  619. $index = $this->extractArgument($params, 'index');
  620. $type = $this->extractArgument($params, 'type');
  621. $body = $this->extractArgument($params, 'body');
  622. /** @var callback $endpointBuilder */
  623. $endpointBuilder = $this->endpoints;
  624. /** @var \Elasticsearch\Endpoints\Create $endpoint */
  625. $endpoint = $endpointBuilder('Create');
  626. $endpoint->setID($id)
  627. ->setIndex($index)
  628. ->setType($type)
  629. ->setBody($body);
  630. $endpoint->setParams($params);
  631. return $this->performRequest($endpoint);
  632. }
  633. /**
  634. * $params['index'] = (string) Default index for items which don't provide one
  635. * ['type'] = (string) Default document type for items which don't provide one
  636. * ['consistency'] = (enum) Explicit write consistency setting for the operation
  637. * ['refresh'] = (boolean) Refresh the index after performing the operation
  638. * ['replication'] = (enum) Explicitly set the replication type
  639. * ['fields'] = (list) Default comma-separated list of fields to return in the response for updates
  640. * ['body'] = (array) The document
  641. *
  642. * @param $params array Associative array of parameters
  643. *
  644. * @return array
  645. */
  646. public function bulk($params = array())
  647. {
  648. $index = $this->extractArgument($params, 'index');
  649. $type = $this->extractArgument($params, 'type');
  650. $body = $this->extractArgument($params, 'body');
  651. /** @var callback $endpointBuilder */
  652. $endpointBuilder = $this->endpoints;
  653. /** @var \Elasticsearch\Endpoints\Bulk $endpoint */
  654. $endpoint = $endpointBuilder('Bulk');
  655. $endpoint->setIndex($index)
  656. ->setType($type)
  657. ->setBody($body);
  658. $endpoint->setParams($params);
  659. return $this->performRequest($endpoint);
  660. }
  661. /**
  662. * $params['index'] = (string) The name of the index (Required)
  663. * ['type'] = (string) The type of the document (Required)
  664. * ['id'] = (string) Specific document ID (when the POST method is used)
  665. * ['consistency'] = (enum) Explicit write consistency setting for the operation
  666. * ['op_type'] = (enum) Explicit operation type
  667. * ['parent'] = (string) ID of the parent document
  668. * ['refresh'] = (boolean) Refresh the index after performing the operation
  669. * ['replication'] = (enum) Specific replication type
  670. * ['routing'] = (string) Specific routing value
  671. * ['timeout'] = (time) Explicit operation timeout
  672. * ['timestamp'] = (time) Explicit timestamp for the document
  673. * ['ttl'] = (duration) Expiration time for the document
  674. * ['version'] = (number) Explicit version number for concurrency control
  675. * ['version_type'] = (enum) Specific version type
  676. * ['body'] = (array) The document
  677. *
  678. * @param $params array Associative array of parameters
  679. *
  680. * @return array
  681. */
  682. public function index($params)
  683. {
  684. $id = $this->extractArgument($params, 'id');
  685. $index = $this->extractArgument($params, 'index');
  686. $type = $this->extractArgument($params, 'type');
  687. $body = $this->extractArgument($params, 'body');
  688. /** @var callback $endpointBuilder */
  689. $endpointBuilder = $this->endpoints;
  690. /** @var \Elasticsearch\Endpoints\Index $endpoint */
  691. $endpoint = $endpointBuilder('Index');
  692. $endpoint->setID($id)
  693. ->setIndex($index)
  694. ->setType($type)
  695. ->setBody($body);
  696. $endpoint->setParams($params);
  697. return $this->performRequest($endpoint);
  698. }
  699. /**
  700. * $params['refresh'] = (boolean) Should the effected indexes be refreshed?
  701. * ['timeout'] = (time) Time each individual bulk request should wait for shards that are unavailable
  702. * ['consistency'] = (enum) Explicit write consistency setting for the operation
  703. * ['wait_for_completion'] = (boolean) Should the request should block until the reindex is complete
  704. * ['requests_per_second'] = (float) The throttle for this request in sub-requests per second. 0 means set no throttle
  705. * ['body'] = (array) The search definition using the Query DSL and the prototype for the index request (Required)
  706. *
  707. * @param $params array Associative array of parameters
  708. *
  709. * @return array
  710. */
  711. public function reindex($params)
  712. {
  713. $body = $this->extractArgument($params, 'body');
  714. /** @var callback $endpointBuilder */
  715. $endpointBuilder = $this->endpoints;
  716. /** @var \Elasticsearch\Endpoints\Reindex $endpoint */
  717. $endpoint = $endpointBuilder('Reindex');
  718. $endpoint->setBody($body);
  719. $endpoint->setParams($params);
  720. return $this->performRequest($endpoint);
  721. }
  722. /**
  723. * $params['index'] = (list) A comma-separated list of index names to restrict the operation; use `_all` or empty string to perform the operation on all indices
  724. * ['ignore_indices'] = (enum) When performed on multiple indices, allows to ignore `missing` ones
  725. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
  726. * ['routing'] = (string) Specific routing value
  727. * ['source'] = (string) The URL-encoded request definition (instead of using request body)
  728. * ['body'] = (array) The request definition
  729. *
  730. * @param $params array Associative array of parameters
  731. *
  732. * @return array
  733. */
  734. public function suggest($params = array())
  735. {
  736. $index = $this->extractArgument($params, 'index');
  737. $body = $this->extractArgument($params, 'body');
  738. /** @var callback $endpointBuilder */
  739. $endpointBuilder = $this->endpoints;
  740. /** @var \Elasticsearch\Endpoints\Suggest $endpoint */
  741. $endpoint = $endpointBuilder('Suggest');
  742. $endpoint->setIndex($index)
  743. ->setBody($body);
  744. $endpoint->setParams($params);
  745. return $this->performRequest($endpoint);
  746. }
  747. /**
  748. * $params['id'] = (string) The document ID (Required)
  749. * ['index'] = (string) The name of the index (Required)
  750. * ['type'] = (string) The type of the document (Required)
  751. * ['analyze_wildcard'] = (boolean) Specify whether wildcards and prefix queries in the query string query should be analyzed (default: false)
  752. * ['analyzer'] = (string) The analyzer for the query string query
  753. * ['default_operator'] = (enum) The default operator for query string query (AND or OR)
  754. * ['df'] = (string) The default field for query string query (default: _all)
  755. * ['fields'] = (list) A comma-separated list of fields to return in the response
  756. * ['lenient'] = (boolean) Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
  757. * ['lowercase_expanded_terms'] = (boolean) Specify whether query terms should be lowercased
  758. * ['parent'] = (string) The ID of the parent document
  759. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
  760. * ['q'] = (string) Query in the Lucene query string syntax
  761. * ['routing'] = (string) Specific routing value
  762. * ['source'] = (string) The URL-encoded query definition (instead of using the request body)
  763. * ['_source'] = (list) True or false to return the _source field or not, or a list of fields to return
  764. * ['_source_exclude'] = (list) A list of fields to exclude from the returned _source field
  765. * ['_source_include'] = (list) A list of fields to extract and return from the _source field
  766. * ['body'] = (string) The URL-encoded query definition (instead of using the request body)
  767. *
  768. * @param $params array Associative array of parameters
  769. *
  770. * @return array
  771. */
  772. public function explain($params)
  773. {
  774. $id = $this->extractArgument($params, 'id');
  775. $index = $this->extractArgument($params, 'index');
  776. $type = $this->extractArgument($params, 'type');
  777. $body = $this->extractArgument($params, 'body');
  778. /** @var callback $endpointBuilder */
  779. $endpointBuilder = $this->endpoints;
  780. /** @var \Elasticsearch\Endpoints\Explain $endpoint */
  781. $endpoint = $endpointBuilder('Explain');
  782. $endpoint->setID($id)
  783. ->setIndex($index)
  784. ->setType($type)
  785. ->setBody($body);
  786. $endpoint->setParams($params);
  787. return $this->performRequest($endpoint);
  788. }
  789. /**
  790. * $params['index'] = (list) A comma-separated list of index names to search; use `_all` or empty string to perform the operation on all indices
  791. * ['type'] = (list) A comma-separated list of document types to search; leave empty to perform the operation on all types
  792. * ['analyzer'] = (string) The analyzer to use for the query string
  793. * ['analyze_wildcard'] = (boolean) Specify whether wildcard and prefix queries should be analyzed (default: false)
  794. * ['default_operator'] = (enum) The default operator for query string query (AND or OR)
  795. * ['df'] = (string) The field to use as default where no field prefix is given in the query string
  796. * ['explain'] = (boolean) Specify whether to return detailed information about score computation as part of a hit
  797. * ['fields'] = (list) A comma-separated list of fields to return as part of a hit
  798. * ['from'] = (number) Starting offset (default: 0)
  799. * ['ignore_indices'] = (enum) When performed on multiple indices, allows to ignore `missing` ones
  800. * ['indices_boost'] = (list) Comma-separated list of index boosts
  801. * ['lenient'] = (boolean) Specify whether format-based query failures (such as providing text to a numeric field) should be ignored
  802. * ['lowercase_expanded_terms'] = (boolean) Specify whether query terms should be lowercased
  803. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
  804. * ['q'] = (string) Query in the Lucene query string syntax
  805. * ['query_cache'] = (boolean) Enable query cache for this request
  806. * ['request_cache'] = (boolean) Enable request cache for this request
  807. * ['routing'] = (list) A comma-separated list of specific routing values
  808. * ['scroll'] = (duration) Specify how long a consistent view of the index should be maintained for scrolled search
  809. * ['search_type'] = (enum) Search operation type
  810. * ['size'] = (number) Number of hits to return (default: 10)
  811. * ['sort'] = (list) A comma-separated list of <field>:<direction> pairs
  812. * ['source'] = (string) The URL-encoded request definition using the Query DSL (instead of using request body)
  813. * ['_source'] = (list) True or false to return the _source field or not, or a list of fields to return
  814. * ['_source_exclude'] = (list) A list of fields to exclude from the returned _source field
  815. * ['_source_include'] = (list) A list of fields to extract and return from the _source field
  816. * ['stats'] = (list) Specific 'tag' of the request for logging and statistical purposes
  817. * ['suggest_field'] = (string) Specify which field to use for suggestions
  818. * ['suggest_mode'] = (enum) Specify suggest mode
  819. * ['suggest_size'] = (number) How many suggestions to return in response
  820. * ['suggest_text'] = (text) The source text for which the suggestions should be returned
  821. * ['timeout'] = (time) Explicit operation timeout
  822. * ['terminate_after'] = (number) The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early.
  823. * ['version'] = (boolean) Specify whether to return document version as part of a hit
  824. * ['body'] = (array|string) The search definition using the Query DSL
  825. *
  826. * @param $params array Associative array of parameters
  827. *
  828. * @return array
  829. */
  830. public function search($params = array())
  831. {
  832. $index = $this->extractArgument($params, 'index');
  833. $type = $this->extractArgument($params, 'type');
  834. $body = $this->extractArgument($params, 'body');
  835. /** @var callback $endpointBuilder */
  836. $endpointBuilder = $this->endpoints;
  837. /** @var \Elasticsearch\Endpoints\Search $endpoint */
  838. $endpoint = $endpointBuilder('Search');
  839. $endpoint->setIndex($index)
  840. ->setType($type)
  841. ->setBody($body);
  842. $endpoint->setParams($params);
  843. return $this->performRequest($endpoint);
  844. }
  845. /**
  846. * $params['index'] = (list) A comma-separated list of index names to search; use `_all` or empty string to perform the operation on all indices
  847. * ['type'] = (list) A comma-separated list of document types to search; leave empty to perform the operation on all types
  848. * ['preference'] = (string) Specify the node or shard the operation should be performed on (default: random)
  849. * ['routing'] = (string) Specific routing value
  850. * ['local'] = (bool) Return local information, do not retrieve the state from master node (default: false)
  851. * ['ignore_unavailable'] = (bool) Whether specified concrete indices should be ignored when unavailable (missing or closed)
  852. * ['allow_no_indices'] = (bool) Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
  853. * ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are open, closed or both.
  854. *
  855. * @param $params array Associative array of parameters
  856. *
  857. * @return array
  858. */
  859. public function searchShards($params = array())
  860. {
  861. $index = $this->extractArgument($params, 'index');
  862. $type = $this->extractArgument($params, 'type');
  863. /** @var callback $endpointBuilder */
  864. $endpointBuilder = $this->endpoints;
  865. /** @var \Elasticsearch\Endpoints\SearchShards $endpoint */
  866. $endpoint = $endpointBuilder('SearchShards');
  867. $endpoint->setIndex($index)
  868. ->setType($type);
  869. $endpoint->setParams($params);
  870. return $this->performRequest($endpoint);
  871. }
  872. /**
  873. * $params['index'] = (list) A comma-separated list of index names to search; use `_all` or empty string to perform the operation on all indices
  874. * ['type'] = (list) A comma-separated list of document types to search; leave empty to perform the operation on all types
  875. *
  876. * @param $params array Associative array of parameters
  877. *
  878. * @return array
  879. */
  880. public function searchTemplate($params = array())
  881. {
  882. $index = $this->extractArgument($params, 'index');
  883. $type = $this->extractArgument($params, 'type');
  884. $body = $this->extractArgument($params, 'body');
  885. /** @var callback $endpointBuilder */
  886. $endpointBuilder = $this->endpoints;
  887. /** @var \Elasticsearch\Endpoints\Search $endpoint */
  888. $endpoint = $endpointBuilder('SearchTemplate');
  889. $endpoint->setIndex($index)
  890. ->setType($type)
  891. ->setBody($body);
  892. $endpoint->setParams($params);
  893. return $this->performRequest($endpoint);
  894. }
  895. /**
  896. * $params['scroll_id'] = (string) The scroll ID for scrolled search
  897. * ['scroll'] = (duration) Specify how long a consistent view of the index should be maintained for scrolled search
  898. * ['body'] = (string) The scroll ID for scrolled search
  899. *
  900. * @param $params array Associative array of parameters
  901. *
  902. * @return array
  903. */
  904. public function scroll($params = array())
  905. {
  906. $scrollID = $this->extractArgument($params, 'scroll_id');
  907. $body = $this->extractArgument($params, 'body');
  908. $scroll = $this->extractArgument($params, 'scroll');
  909. /** @var callback $endpointBuilder */
  910. $endpointBuilder = $this->endpoints;
  911. /** @var \Elasticsearch\Endpoints\Scroll $endpoint */
  912. $endpoint = $endpointBuilder('Scroll');
  913. $endpoint->setScrollID($scrollID)
  914. ->setScroll($scroll)
  915. ->setBody($body);
  916. $endpoint->setParams($params);
  917. return $this->performRequest($endpoint);
  918. }
  919. /**
  920. * $params['scroll_id'] = (string) The scroll ID for scrolled search
  921. * ['scroll'] = (duration) Specify how long a consistent view of the index should be maintained for scrolled search
  922. * ['body'] = (string) The scroll ID for scrolled search
  923. *
  924. * @param $params array Associative array of parameters
  925. *
  926. * @return array
  927. */
  928. public function clearScroll($params = array())
  929. {
  930. $scrollID = $this->extractArgument($params, 'scroll_id');
  931. $body = $this->extractArgument($params, 'body');
  932. /** @var callback $endpointBuilder */
  933. $endpointBuilder = $this->endpoints;
  934. /** @var \Elasticsearch\Endpoints\ClearScroll $endpoint */
  935. $endpoint = $endpointBuilder('ClearScroll');
  936. $endpoint->setScrollID($scrollID)
  937. ->setBody($body);
  938. $endpoint->setParams($params);
  939. return $this->performRequest($endpoint);
  940. }
  941. /**
  942. * $params['id'] = (string) Document ID (Required)
  943. * ['index'] = (string) The name of the index (Required)
  944. * ['type'] = (string) The type of the document (Required)
  945. * ['consistency'] = (enum) Explicit write consistency setting for the operation
  946. * ['fields'] = (list) A comma-separated list of fields to return in the response
  947. * ['lang'] = (string) The script language (default: mvel)
  948. * ['parent'] = (string) ID of the parent document
  949. * ['refresh'] = (boolean) Refresh the index after performing the operation
  950. * ['replication'] = (enum) Specific replication type
  951. * ['retry_on_conflict'] = (number) Specify how many times should the operation be retried when a conflict occurs (default: 0)
  952. * ['routing'] = (string) Specific routing value
  953. * ['script'] = () The URL-encoded script definition (instead of using request body)
  954. * ['timeout'] = (time) Explicit operation timeout
  955. * ['timestamp'] = (time) Explicit timestamp for the document
  956. * ['ttl'] = (duration) Expiration time for the document
  957. * ['version_type'] = (number) Explicit version number for concurrency control
  958. * ['body'] = (array) The request definition using either `script` or partial `doc`
  959. *
  960. * @param $params array Associative array of parameters
  961. *
  962. * @return array
  963. */
  964. public function update($params)
  965. {
  966. $id = $this->extractArgument($params, 'id');
  967. $index = $this->extractArgument($params, 'index');
  968. $type = $this->extractArgument($params, 'type');
  969. $body = $this->extractArgument($params, 'body');
  970. /** @var callback $endpointBuilder */
  971. $endpointBuilder = $this->endpoints;
  972. /** @var \Elasticsearch\Endpoints\Update $endpoint */
  973. $endpoint = $endpointBuilder('Update');
  974. $endpoint->setID($id)
  975. ->setIndex($index)
  976. ->setType($type)
  977. ->setBody($body);
  978. $endpoint->setParams($params);
  979. return $this->performRequest($endpoint);
  980. }
  981. /**
  982. * $params['index'] = (list) A comma-separated list of index names to search; use `_all` or
  983. * empty string to perform the operation on all indices (Required)
  984. * ['type'] = (list) A comma-separated list of document types to search; leave empty to
  985. * perform the operation on all types
  986. * ['analyzer'] = (string) The analyzer to use for the query string
  987. * ['analyze_wildcard'] = (boolean) Specify whether wildcard and prefix queries should be analyzed
  988. * (default: false)
  989. * ['default_operator'] = (enum) The default operator for query string query (AND or OR) (AND,OR)
  990. * (default: OR)
  991. * ['df'] = (string) The field to use as default where no field prefix is given in the
  992. * query string
  993. * ['explain'] = (boolean) Specify whether to return detailed information about score
  994. * computation as part of a hit
  995. * ['fields'] = (list) A comma-separated list of fields to return as part of a hit
  996. * ['fielddata_fields'] = (list) A comma-separated list of fields to return as the field data
  997. * representation of a field for each hit
  998. * ['from'] = (number) Starting offset (default: 0)
  999. * ['ignore_unavailable'] = (boolean) Whether specified concrete indices should be ignored when
  1000. * unavailable (missing or closed)
  1001. * ['allow_no_indices'] = (boolean) Whether to ignore if a wildcard indices expression resolves into
  1002. * no concrete indices. (This includes `_all` string or when no indices have been specified)
  1003. * ['conflicts'] = (enum) What to do when the reindex hits version conflicts? (abort,proceed)
  1004. * (default: abort)
  1005. * ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are
  1006. * open, closed or both. (open,closed,none,all) (default: open)
  1007. * ['lenient'] = (boolean) Specify whether format-based query failures (such as providing
  1008. * text to a numeric field) should be ignored
  1009. * ['lowercase_expanded_terms'] = (boolean) Specify whether query terms should be lowercased
  1010. * ['preference'] = (string) Specify the node or shard the operation should be performed on
  1011. * (default: random)
  1012. * ['q'] = (string) Query in the Lucene query string syntax
  1013. * ['routing'] = (list) A comma-separated list of specific routing values
  1014. * ['scroll'] = (duration) Specify how long a consistent view of the index should be
  1015. * maintained for scrolled search
  1016. * ['search_type'] = (enum) Search operation type (query_then_fetch,dfs_query_then_fetch)
  1017. * ['search_timeout'] = (time) Explicit timeout for each search request. Defaults to no timeout.
  1018. * ['size'] = (number) Number of hits to return (default: 10)
  1019. * ['sort'] = (list) A comma-separated list of <field>:<direction> pairs
  1020. * ['_source'] = (list) True or false to return the _source field or not, or a list of
  1021. * fields to return
  1022. * ['_source_exclude'] = (list) A list of fields to exclude from the returned _source field
  1023. * ['_source_include'] = (list) A list of fields to extract and return from the _source field
  1024. * ['terminate_after'] = (number) The maximum number of documents to collect for each shard, upon
  1025. * reaching which the query execution will terminate early.
  1026. * ['stats'] = (list) Specific 'tag' of the request for logging and statistical purposes
  1027. * ['suggest_field'] = (string) Specify which field to use for suggestions
  1028. * ['suggest_mode'] = (enum) Specify suggest mode (missing,popular,always) (default: missing)
  1029. * ['suggest_size'] = (number) How many suggestions to return in response
  1030. * ['suggest_text'] = (text) The source text for which the suggestions should be returned
  1031. * ['timeout'] = (time) Time each individual bulk request should wait for shards that are
  1032. * unavailable. (default: 1m)
  1033. * ['track_scores'] = (boolean) Whether to calculate and return scores even if they are not used
  1034. * for sorting
  1035. * ['version'] = (boolean) Specify whether to return document version as part of a hit
  1036. * ['version_type'] = (boolean) Should the document increment the version number (internal) on
  1037. * hit or not (reindex)
  1038. * ['request_cache'] = (boolean) Specify if request cache should be used for this request or not,
  1039. * defaults to index level setting
  1040. * ['refresh'] = (boolean) Should the effected indexes be refreshed?
  1041. * ['consistency'] = (enum) Explicit write consistency setting for the operation
  1042. * (one,quorum,all)
  1043. * ['scroll_size'] = (integer) Size on the scroll request powering the update_by_query
  1044. * ['wait_for_completion'] = (boolean) Should the request should block until the reindex is complete.
  1045. * (default: false)
  1046. * ['body'] = The search definition using the Query DSL
  1047. *
  1048. * @param array $params
  1049. *
  1050. * @return array
  1051. */
  1052. public function updateByQuery($params = array())
  1053. {
  1054. $index = $this->extractArgument($params, 'index');
  1055. $body = $this->extractArgument($params, 'body');
  1056. $type = $this->extractArgument($params, 'type');
  1057. /** @var callback $endpointBuilder */
  1058. $endpointBuilder = $this->endpoints;
  1059. /** @var \Elasticsearch\Endpoints\UpdateByQuery $endpoint */
  1060. $endpoint = $endpointBuilder('UpdateByQuery');
  1061. $endpoint->setIndex($index)
  1062. ->setType($type)
  1063. ->setBody($body);
  1064. $endpoint->setParams($params);
  1065. return $this->performRequest($endpoint);
  1066. }
  1067. /**
  1068. * $params['id'] = (string) The script ID (Required)
  1069. * ['lang'] = (string) The script language (Required)
  1070. *
  1071. * @param $params array Associative array of parameters
  1072. *
  1073. * @return array
  1074. */
  1075. public function getScript($params)
  1076. {
  1077. $id = $this->extractArgument($params, 'id');
  1078. $lang = $this->extractArgument($params, 'lang');
  1079. /** @var callback $endpointBuilder */
  1080. $endpointBuilder = $this->endpoints;
  1081. /** @var \Elasticsearch\Endpoints\Script\Get $endpoint */
  1082. $endpoint = $endpointBuilder('Script\Get');
  1083. $endpoint->setID($id)
  1084. ->setLang($lang);
  1085. $endpoint->setParams($params);
  1086. return $this->performRequest($endpoint);
  1087. }
  1088. /**
  1089. * $params['id'] = (string) The script ID (Required)
  1090. * ['lang'] = (string) The script language (Required)
  1091. *
  1092. * @param $params array Associative array of parameters
  1093. *
  1094. * @return array
  1095. */
  1096. public function deleteScript($params)
  1097. {
  1098. $id = $this->extractArgument($params, 'id');
  1099. $lang = $this->extractArgument($params, 'lang');
  1100. /** @var callback $endpointBuilder */
  1101. $endpointBuilder = $this->endpoints;
  1102. /** @var \Elasticsearch\Endpoints\Script\Delete $endpoint */
  1103. $endpoint = $endpointBuilder('Script\Delete');
  1104. $endpoint->setID($id)
  1105. ->setLang($lang);
  1106. $endpoint->setParams($params);
  1107. return $this->performRequest($endpoint);
  1108. }
  1109. /**
  1110. * $params['id'] = (string) The script ID (Required)
  1111. * ['lang'] = (string) The script language (Required)
  1112. *
  1113. * @param $params array Associative array of parameters
  1114. *
  1115. * @return array
  1116. */
  1117. public function putScript($params)
  1118. {
  1119. $id = $this->extractArgument($params, 'id');
  1120. $lang = $this->extractArgument($params, 'lang');
  1121. $body = $this->extractArgument($params, 'body');
  1122. /** @var callback $endpointBuilder */
  1123. $endpointBuilder = $this->endpoints;
  1124. /** @var \Elasticsearch\Endpoints\Script\Put $endpoint */
  1125. $endpoint = $endpointBuilder('Script\Put');
  1126. $endpoint->setID($id)
  1127. ->setBody($body);
  1128. $endpoint->setParams($params);
  1129. return $this->performRequest($endpoint);
  1130. }
  1131. /**
  1132. * $params['id'] = (string) The search template ID (Required)
  1133. *
  1134. * @param $params array Associative array of parameters
  1135. *
  1136. * @return array
  1137. */
  1138. public function getTemplate($params)
  1139. {
  1140. $id = $this->extractArgument($params, 'id');
  1141. /** @var callback $endpointBuilder */
  1142. $endpointBuilder = $this->endpoints;
  1143. /** @var \Elasticsearch\Endpoints\Template\Get $endpoint */
  1144. $endpoint = $endpointBuilder('Template\Get');
  1145. $endpoint->setID($id);
  1146. $endpoint->setParams($params);
  1147. return $this->performRequest($endpoint);
  1148. }
  1149. /**
  1150. * $params['id'] = (string) The search template ID (Required)
  1151. *
  1152. * @param $params array Associative array of parameters
  1153. *
  1154. * @return array
  1155. */
  1156. public function deleteTemplate($params)
  1157. {
  1158. $id = $this->extractArgument($params, 'id');
  1159. /** @var callback $endpointBuilder */
  1160. $endpointBuilder = $this->endpoints;
  1161. /** @var \Elasticsearch\Endpoints\Template\Delete $endpoint */
  1162. $endpoint = $endpointBuilder('Template\Delete');
  1163. $endpoint->setID($id);
  1164. $endpoint->setParams($params);
  1165. return $this->performRequest($endpoint);
  1166. }
  1167. /**
  1168. * $params['index'] = (list) A comma-separated list of indices to restrict the results
  1169. * ['fields'] = (list) A comma-separated list of fields for to get field statistics for (min value, max value, and more)
  1170. * ['level'] = (enum) Defines if field stats should be returned on a per index level or on a cluster wide level
  1171. * ['ignore_unavailable'] = (bool) Whether specified concrete indices should be ignored when unavailable (missing or closed)
  1172. * ['allow_no_indices'] = (bool) Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
  1173. * ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are open, closed or both.
  1174. *
  1175. * @param $params array Associative array of parameters
  1176. *
  1177. * @return array
  1178. */
  1179. public function fieldStats($params = array())
  1180. {
  1181. $index = $this->extractArgument($params, 'index');
  1182. $body = $this->extractArgument($params, 'body');
  1183. /** @var callback $endpointBuilder */
  1184. $endpointBuilder = $this->endpoints;
  1185. /** @var \Elasticsearch\Endpoints\FieldStats $endpoint */
  1186. $endpoint = $endpointBuilder('FieldStats');
  1187. $endpoint->setIndex($index)
  1188. ->setBody($body)
  1189. ->setParams($params);
  1190. return $this->performRequest($endpoint);
  1191. }
  1192. /**
  1193. * $params['index'] = (list) A comma-separated list of indices to restrict the results
  1194. * ['ignore_unavailable'] = (bool) Whether specified concrete indices should be ignored when unavailable (missing or closed)
  1195. * ['allow_no_indices'] = (bool) Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified)
  1196. * ['expand_wildcards'] = (enum) Whether to expand wildcard expression to concrete indices that are open, closed or both.
  1197. *
  1198. * @param $params array Associative array of parameters
  1199. *
  1200. * @return array
  1201. */
  1202. public function fieldCaps($params = array())
  1203. {
  1204. $index = $this->extractArgument($params, 'index');
  1205. $body = $this->extractArgument($params, 'body');
  1206. /** @var callback $endpointBuilder */
  1207. $endpointBuilder = $this->endpoints;
  1208. /** @var \Elasticsearch\Endpoints\FieldCaps $endpoint */
  1209. $endpoint = $endpointBuilder('FieldCaps');
  1210. $endpoint->setIndex($index)
  1211. ->setBody($body)
  1212. ->setParams($params);
  1213. return $this->performRequest($endpoint);
  1214. }
  1215. /**
  1216. * $params['id'] = (string) ID of the template to render
  1217. *
  1218. * @param $params array Associative array of parameters
  1219. *
  1220. * @return array
  1221. */
  1222. public function renderSearchTemplate($params = array())
  1223. {
  1224. $body = $this->extractArgument($params, 'body');
  1225. $id = $this->extractArgument($params, 'id');
  1226. /** @var callback $endpointBuilder */
  1227. $endpointBuilder = $this->endpoints;
  1228. /** @var \Elasticsearch\Endpoints\RenderSearchTemplate $endpoint */
  1229. $endpoint = $endpointBuilder('RenderSearchTemplate');
  1230. $endpoint->setBody($body)
  1231. ->setID($id);
  1232. $endpoint->setParams($params);
  1233. return $this->performRequest($endpoint);
  1234. }
  1235. /**
  1236. * Operate on the Indices Namespace of commands
  1237. *
  1238. * @return IndicesNamespace
  1239. */
  1240. public function indices()
  1241. {
  1242. return $this->indices;
  1243. }
  1244. /**
  1245. * Operate on the Cluster namespace of commands
  1246. *
  1247. * @return ClusterNamespace
  1248. */
  1249. public function cluster()
  1250. {
  1251. return $this->cluster;
  1252. }
  1253. /**
  1254. * Operate on the Nodes namespace of commands
  1255. *
  1256. * @return NodesNamespace
  1257. */
  1258. public function nodes()
  1259. {
  1260. return $this->nodes;
  1261. }
  1262. /**
  1263. * Operate on the Snapshot namespace of commands
  1264. *
  1265. * @return SnapshotNamespace
  1266. */
  1267. public function snapshot()
  1268. {
  1269. return $this->snapshot;
  1270. }
  1271. /**
  1272. * Operate on the Cat namespace of commands
  1273. *
  1274. * @return CatNamespace
  1275. */
  1276. public function cat()
  1277. {
  1278. return $this->cat;
  1279. }
  1280. /**
  1281. * Operate on the Ingest namespace of commands
  1282. *
  1283. * @return IngestNamespace
  1284. */
  1285. public function ingest()
  1286. {
  1287. return $this->ingest;
  1288. }
  1289. /**
  1290. * Operate on the Tasks namespace of commands
  1291. *
  1292. * @return TasksNamespace
  1293. */
  1294. public function tasks()
  1295. {
  1296. return $this->tasks;
  1297. }
  1298. /**
  1299. * Operate on the Remote namespace of commands
  1300. *
  1301. * @return RemoteNamespace
  1302. */
  1303. public function remote()
  1304. {
  1305. return $this->remote;
  1306. }
  1307. /**
  1308. * Catchall for registered namespaces
  1309. *
  1310. * @param $name
  1311. * @param $arguments
  1312. * @return Object
  1313. * @throws BadMethodCallException if the namespace cannot be found
  1314. */
  1315. public function __call($name, $arguments)
  1316. {
  1317. if (isset($this->registeredNamespaces[$name])) {
  1318. return $this->registeredNamespaces[$name];
  1319. }
  1320. throw new BadMethodCallException("Namespace [$name] not found");
  1321. }
  1322. /**
  1323. * @param array $params
  1324. * @param string $arg
  1325. *
  1326. * @return null|mixed
  1327. */
  1328. public function extractArgument(&$params, $arg)
  1329. {
  1330. if (is_object($params) === true) {
  1331. $params = (array) $params;
  1332. }
  1333. if (array_key_exists($arg, $params) === true) {
  1334. $val = $params[$arg];
  1335. unset($params[$arg]);
  1336. return $val;
  1337. } else {
  1338. return null;
  1339. }
  1340. }
  1341. private function verifyNotNullOrEmpty($name, $var)
  1342. {
  1343. if ($var === null) {
  1344. throw new InvalidArgumentException("$name cannot be null.");
  1345. }
  1346. if (is_string($var)) {
  1347. if (strlen($var) === 0) {
  1348. throw new InvalidArgumentException("$name cannot be an empty string");
  1349. }
  1350. }
  1351. if (is_array($var)) {
  1352. if (strlen(implode("", $var)) === 0) {
  1353. throw new InvalidArgumentException("$name cannot be an array of empty strings");
  1354. }
  1355. }
  1356. }
  1357. /**
  1358. * @param $endpoint AbstractEndpoint
  1359. *
  1360. * @throws \Exception
  1361. * @return array
  1362. */
  1363. private function performRequest(AbstractEndpoint $endpoint)
  1364. {
  1365. $promise = $this->transport->performRequest(
  1366. $endpoint->getMethod(),
  1367. $endpoint->getURI(),
  1368. $endpoint->getParams(),
  1369. $endpoint->getBody(),
  1370. $endpoint->getOptions()
  1371. );
  1372. return $this->transport->resultOrFuture($promise, $endpoint->getOptions());
  1373. }
  1374. }