crud.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. [[indexing_documents]]
  2. === Indexing documents
  3. When you add documents to {es}, you index JSON documents. This maps naturally to
  4. PHP associative arrays, since they can easily be encoded in JSON. Therefore, in
  5. Elasticsearch-PHP you create and pass associative arrays to the client for
  6. indexing. There are several methods of ingesting data into {es} which we cover
  7. here.
  8. [discrete]
  9. ==== Single document indexing
  10. When indexing a document, you can either provide an ID or let {es} generate one
  11. for you.
  12. {zwsp} +
  13. .Providing an ID value
  14. [source,php]
  15. ----
  16. $params = [
  17. 'index' => 'my_index',
  18. 'id' => 'my_id',
  19. 'body' => [ 'testField' => 'abc']
  20. ];
  21. // Document will be indexed to my_index/_doc/my_id
  22. $response = $client->index($params);
  23. ----
  24. {zwsp} +
  25. .Omitting an ID value
  26. [source,php]
  27. ----
  28. $params = [
  29. 'index' => 'my_index',
  30. 'body' => [ 'testField' => 'abc']
  31. ];
  32. // Document will be indexed to my_index/_doc/<autogenerated ID>
  33. $response = $client->index($params);
  34. ----
  35. {zwsp} +
  36. If you need to set other parameters, such as a `routing` value, you specify
  37. those in the array alongside the `index`, and others. For example, let's set the
  38. routing and timestamp of this new document:
  39. .Additional parameters
  40. [source,php]
  41. ----
  42. $params = [
  43. 'index' => 'my_index',
  44. 'id' => 'my_id',
  45. 'routing' => 'company_xyz',
  46. 'timestamp' => strtotime("-1d"),
  47. 'body' => [ 'testField' => 'abc']
  48. ];
  49. $response = $client->index($params);
  50. ----
  51. {zwsp} +
  52. [discrete]
  53. ==== Bulk Indexing
  54. {es} also supports bulk indexing of documents. The bulk API expects JSON
  55. action/metadata pairs, separated by newlines. When constructing your documents
  56. in PHP, the process is similar. You first create an action array object (for
  57. example, an `index` object), then you create a document body object. This
  58. process repeats for all your documents.
  59. A simple example might look like this:
  60. .Bulk indexing with PHP arrays
  61. [source,php]
  62. ----
  63. for($i = 0; $i < 100; $i++) {
  64. $params['body'][] = [
  65. 'index' => [
  66. '_index' => 'my_index',
  67. ]
  68. ];
  69. $params['body'][] = [
  70. 'my_field' => 'my_value',
  71. 'second_field' => 'some more values'
  72. ];
  73. }
  74. $responses = $client->bulk($params);
  75. ----
  76. In practice, you'll likely have more documents than you want to send in a single
  77. bulk request. In that case, you need to batch up the requests and periodically
  78. send them:
  79. .Bulk indexing with batches
  80. [source,php]
  81. ----
  82. $params = ['body' => []];
  83. for ($i = 1; $i <= 1234567; $i++) {
  84. $params['body'][] = [
  85. 'index' => [
  86. '_index' => 'my_index',
  87. '_id' => $i
  88. ]
  89. ];
  90. $params['body'][] = [
  91. 'my_field' => 'my_value',
  92. 'second_field' => 'some more values'
  93. ];
  94. // Every 1000 documents stop and send the bulk request
  95. if ($i % 1000 == 0) {
  96. $responses = $client->bulk($params);
  97. // erase the old bulk request
  98. $params = ['body' => []];
  99. // unset the bulk response when you are done to save memory
  100. unset($responses);
  101. }
  102. }
  103. // Send the last batch if it exists
  104. if (!empty($params['body'])) {
  105. $responses = $client->bulk($params);
  106. }
  107. ----
  108. [[getting_documents]]
  109. === Getting documents
  110. {es} provides realtime GETs of documents. This means that as soon as the
  111. document is indexed and your client receives an acknowledgement, you can
  112. immediately retrieve the document from any shard. Get operations are performed
  113. by requesting a document by its full `index/type/id` path:
  114. [source,php]
  115. ----
  116. $params = [
  117. 'index' => 'my_index',
  118. 'id' => 'my_id'
  119. ];
  120. // Get doc at /my_index/_doc/my_id
  121. $response = $client->get($params);
  122. ----
  123. {zwsp} +
  124. [[updating_documents]]
  125. === Updating documents
  126. Updating a document allows you to either completely replace the contents of the
  127. existing document, or perform a partial update to just some fields (either
  128. changing an existing field or adding new fields).
  129. [discrete]
  130. ==== Partial document update
  131. If you want to partially update a document (for example, change an existing
  132. field or add a new one) you can do so by specifying the `doc` in the `body`
  133. parameter. This merges the fields in `doc` with the existing document.
  134. [source,php]
  135. ----
  136. $params = [
  137. 'index' => 'my_index',
  138. 'id' => 'my_id',
  139. 'body' => [
  140. 'doc' => [
  141. 'new_field' => 'abc'
  142. ]
  143. ]
  144. ];
  145. // Update doc at /my_index/_doc/my_id
  146. $response = $client->update($params);
  147. ----
  148. {zwsp} +
  149. [discrete]
  150. ==== Scripted document update
  151. Sometimes you need to perform a scripted update, such as incrementing a counter
  152. or appending a new value to an array. To perform a scripted update, you need to
  153. provide a script and usually a set of parameters:
  154. [source,php]
  155. ----
  156. $params = [
  157. 'index' => 'my_index',
  158. 'id' => 'my_id',
  159. 'body' => [
  160. 'script' => 'ctx._source.counter += count',
  161. 'params' => [
  162. 'count' => 4
  163. ]
  164. ]
  165. ];
  166. $response = $client->update($params);
  167. ----
  168. {zwsp} +
  169. [discrete]
  170. ==== Upserts
  171. Upserts are "Update or Insert" operations. This means an upsert attempts to run
  172. your update script, but if the document does not exist (or the field you are
  173. trying to update doesn't exist), default values are inserted instead.
  174. [source,php]
  175. ----
  176. $params = [
  177. 'index' => 'my_index',
  178. 'id' => 'my_id',
  179. 'body' => [
  180. 'script' => [
  181. 'source' => 'ctx._source.counter += params.count',
  182. 'params' => [
  183. 'count' => 4
  184. ],
  185. ],
  186. 'upsert' => [
  187. 'counter' => 1
  188. ],
  189. ]
  190. ];
  191. $response = $client->update($params);
  192. ----
  193. {zwsp} +
  194. [[deleting_documents]]
  195. === Deleting documents
  196. Finally, you can delete documents by specifying their full `/index/_doc_/id`
  197. path:
  198. [source,php]
  199. ----
  200. $params = [
  201. 'index' => 'my_index',
  202. 'id' => 'my_id'
  203. ];
  204. // Delete doc at /my_index/_doc_/my_id
  205. $response = $client->delete($params);
  206. ----
  207. {zwsp} +