enrich.asciidoc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[ingest-enriching-data]]
  4. == Enrich your data
  5. You can use the <<enrich-processor,enrich processor>>
  6. to append data from existing indices
  7. to incoming documents during ingest.
  8. For example, you can use the enrich processor to:
  9. * Identify web services or vendors based on known IP addresses
  10. * Add product information to retail orders based on product IDs
  11. * Supplement contact information based on an email address
  12. [float]
  13. [[enrich-setup]]
  14. === Set up an enrich processor
  15. To set up an enrich processor and learn how it works,
  16. follow these steps:
  17. . Check the <<enrich-prereqs, prerequisites>>.
  18. . <<create-enrich-source-index>>.
  19. . <<create-enrich-policy>>.
  20. . <<execute-enrich-policy>>.
  21. . <<add-enrich-processor>>.
  22. . <<ingest-enrich-docs>>.
  23. Once you have an enrich processor set up,
  24. you can <<update-enrich-data,update your enrich data>>
  25. and <<update-enrich-policies, update your enrich policies>>
  26. using the <<enrich-apis,enrich APIs>>.
  27. [IMPORTANT]
  28. ====
  29. The enrich processor performs several operations
  30. and may impact the speed of your <<pipeline,ingest pipeline>>.
  31. We strongly recommend testing and benchmarking your enrich processors
  32. before deploying them in production.
  33. We do not recommend using the enrich processor to append real-time data.
  34. The enrich processor works best with reference data
  35. that doesn't change frequently.
  36. ====
  37. [float]
  38. [[enrich-prereqs]]
  39. ==== Prerequisites
  40. include::{docdir}/ingest/apis/enrich/put-enrich-policy.asciidoc[tag=enrich-policy-api-prereqs]
  41. [float]
  42. [[create-enrich-source-index]]
  43. ==== Create a source index
  44. To begin,
  45. create one or more source indices.
  46. A *source index* contains data you want to append to incoming documents.
  47. You can index and manage documents in a source index
  48. like a regular index.
  49. The following <<docs-index_,index API>> request creates the `users` source index
  50. containing user data.
  51. This request also indexes a new document to the `users` source index.
  52. [source,js]
  53. ----
  54. PUT /users/_doc/1?refresh
  55. {
  56. "email": "mardy.brown@asciidocsmith.com",
  57. "first_name": "Mardy",
  58. "last_name": "Brown",
  59. "city": "New Orleans",
  60. "county": "Orleans",
  61. "state": "LA",
  62. "zip": 70116,
  63. "web": "mardy.asciidocsmith.com"
  64. }
  65. ----
  66. // CONSOLE
  67. You also can set up {beats-ref}/getting-started.html[{beats}],
  68. such as a {filebeat-ref}/filebeat-getting-started.html[{filebeat}],
  69. to automatically send and index documents
  70. to your source indices.
  71. See {beats-ref}/getting-started.html[Getting started with {beats}].
  72. [float]
  73. [[create-enrich-policy]]
  74. ==== Create an enrich policy
  75. Use the <<put-enrich-policy-api, put enrich policy>> API
  76. to create an enrich policy.
  77. include::{docdir}/ingest/apis/enrich/put-enrich-policy.asciidoc[tag=enrich-policy-def]
  78. [source,js]
  79. ----
  80. PUT /_enrich/policy/users-policy
  81. {
  82. "match": {
  83. "indices": "users",
  84. "match_field": "email",
  85. "enrich_fields": ["first_name", "last_name", "city", "zip", "state"]
  86. }
  87. }
  88. ----
  89. // CONSOLE
  90. // TEST[continued]
  91. [float]
  92. [[execute-enrich-policy]]
  93. ==== Execute an enrich policy
  94. Use the <<execute-enrich-policy-api, execute enrich policy>> API
  95. to create an enrich index for the policy.
  96. include::apis/enrich/execute-enrich-policy.asciidoc[tag=execute-enrich-policy-def]
  97. The following request executes the `users-policy` enrich policy.
  98. Because this API request performs several operations,
  99. it may take a while to return a response.
  100. [source,js]
  101. ----
  102. POST /_enrich/policy/users-policy/_execute
  103. ----
  104. // CONSOLE
  105. // TEST[continued]
  106. [float]
  107. [[add-enrich-processor]]
  108. ==== Add the enrich processor to an ingest pipeline
  109. Use the <<put-pipeline-api,put pipeline>> API
  110. to create an ingest pipeline.
  111. Include an <<enrich-processor,enrich processor>>
  112. that uses your enrich policy.
  113. When defining an enrich processor,
  114. you must include the following:
  115. * The *field* used to match incoming documents
  116. to documents in the enrich index.
  117. +
  118. This field should be included in incoming documents.
  119. To match, this field must contain the exact
  120. value of the match field of a document in the enrich index.
  121. * The *target field* added to incoming documents.
  122. This field contains all appended enrich data.
  123. The following request adds a new pipeline, `user_lookup`.
  124. This pipeline includes an enrich processor
  125. that uses the `users-policy` enrich policy.
  126. [source,js]
  127. ----
  128. PUT /_ingest/pipeline/user_lookup
  129. {
  130. "description" : "Enriching user details to messages",
  131. "processors" : [
  132. {
  133. "enrich" : {
  134. "policy_name": "users-policy",
  135. "field" : "email",
  136. "target_field": "user"
  137. }
  138. }
  139. ]
  140. }
  141. ----
  142. // CONSOLE
  143. // TEST[continued]
  144. You also can add other <<ingest-processors,processors>>
  145. to your ingest pipeline.
  146. You can use these processors to change or drop incoming documents
  147. based on your criteria.
  148. See <<ingest-processors>> for a list of built-in processors.
  149. [float]
  150. [[ingest-enrich-docs]]
  151. ==== Ingest and enrich documents
  152. Index incoming documents using your ingest pipeline.
  153. Because the enrich policy type is `match`,
  154. the enrich processor matches incoming documents
  155. to documents in the enrich index
  156. based on match field values.
  157. The processor then appends the enrich field data
  158. from any matching document in the enrich index
  159. to target field of the incoming document.
  160. The enrich processor appends all data to the target field as an array.
  161. If the incoming document matches more than one document in the enrich index,
  162. the processor appends data from those documents to the array.
  163. If the incoming document matches no documents in the enrich index,
  164. the processor appends no data.
  165. The following <<docs-index_,Index API>> request uses the ingest pipeline
  166. to index a document
  167. containing the `email` field,
  168. the `match_field` specified in the `users-policy` enrich policy.
  169. [source,js]
  170. ----
  171. PUT /my_index/_doc/my_id?pipeline=user_lookup
  172. {
  173. "email": "mardy.brown@asciidocsmith.com"
  174. }
  175. ----
  176. // CONSOLE
  177. // TEST[continued]
  178. To verify the enrich processor matched
  179. and appended the appropriate field data,
  180. use the <<docs-get,get>> API to view the indexed document.
  181. [source,js]
  182. ----
  183. GET /my_index/_doc/my_id
  184. ----
  185. // CONSOLE
  186. // TEST[continued]
  187. The API returns the following response:
  188. [source,js]
  189. ----
  190. {
  191. "found": true,
  192. "_index": "my_index",
  193. "_type": "_doc",
  194. "_id": "my_id",
  195. "_version": 1,
  196. "_seq_no": 55,
  197. "_primary_term": 1,
  198. "_source": {
  199. "user": [
  200. {
  201. "email": "mardy.brown@asciidocsmith.com",
  202. "first_name": "Mardy",
  203. "last_name": "Brown",
  204. "zip": 70116,
  205. "city": "New Orleans",
  206. "state": "LA"
  207. }
  208. ],
  209. "email": "mardy.brown@asciidocsmith.com"
  210. }
  211. }
  212. ----
  213. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term":1/"_primary_term" : $body._primary_term/]
  214. [float]
  215. [[update-enrich-data]]
  216. === Update your enrich index
  217. include::{docdir}/ingest/apis/enrich/execute-enrich-policy.asciidoc[tag=update-enrich-index]
  218. If wanted, you can <<docs-reindex,reindex>>
  219. or <<docs-update-by-query,update>> any already ingested documents
  220. using your ingest pipeline.
  221. [float]
  222. [[update-enrich-policies]]
  223. === Update an enrich policy
  224. include::apis/enrich/put-enrich-policy.asciidoc[tag=update-enrich-policy]
  225. ////
  226. [source,js]
  227. --------------------------------------------------
  228. DELETE /_ingest/pipeline/user_lookup
  229. DELETE /_enrich/policy/users-policy
  230. --------------------------------------------------
  231. // CONSOLE
  232. // TEST[continued]
  233. ////