enrich.asciidoc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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,console]
  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. You also can set up {beats-ref}/getting-started.html[{beats}],
  67. such as a {filebeat-ref}/filebeat-getting-started.html[{filebeat}],
  68. to automatically send and index documents
  69. to your source indices.
  70. See {beats-ref}/getting-started.html[Getting started with {beats}].
  71. [float]
  72. [[create-enrich-policy]]
  73. ==== Create an enrich policy
  74. Use the <<put-enrich-policy-api, put enrich policy>> API
  75. to create an enrich policy.
  76. include::{docdir}/ingest/apis/enrich/put-enrich-policy.asciidoc[tag=enrich-policy-def]
  77. [source,console]
  78. ----
  79. PUT /_enrich/policy/users-policy
  80. {
  81. "match": {
  82. "indices": "users",
  83. "match_field": "email",
  84. "enrich_fields": ["first_name", "last_name", "city", "zip", "state"]
  85. }
  86. }
  87. ----
  88. // TEST[continued]
  89. [float]
  90. [[execute-enrich-policy]]
  91. ==== Execute an enrich policy
  92. Use the <<execute-enrich-policy-api, execute enrich policy>> API
  93. to create an enrich index for the policy.
  94. include::apis/enrich/execute-enrich-policy.asciidoc[tag=execute-enrich-policy-def]
  95. The following request executes the `users-policy` enrich policy.
  96. Because this API request performs several operations,
  97. it may take a while to return a response.
  98. [source,console]
  99. ----
  100. POST /_enrich/policy/users-policy/_execute
  101. ----
  102. // TEST[continued]
  103. [float]
  104. [[add-enrich-processor]]
  105. ==== Add the enrich processor to an ingest pipeline
  106. Use the <<put-pipeline-api,put pipeline>> API
  107. to create an ingest pipeline.
  108. Include an <<enrich-processor,enrich processor>>
  109. that uses your enrich policy.
  110. When defining an enrich processor,
  111. you must include the following:
  112. * The *field* used to match incoming documents
  113. to documents in the enrich index.
  114. +
  115. This field should be included in incoming documents.
  116. To match, this field must contain the exact
  117. value of the match field of a document in the enrich index.
  118. * The *target field* added to incoming documents.
  119. This field contains all appended enrich data.
  120. The following request adds a new pipeline, `user_lookup`.
  121. This pipeline includes an enrich processor
  122. that uses the `users-policy` enrich policy.
  123. [source,console]
  124. ----
  125. PUT /_ingest/pipeline/user_lookup
  126. {
  127. "description" : "Enriching user details to messages",
  128. "processors" : [
  129. {
  130. "enrich" : {
  131. "policy_name": "users-policy",
  132. "field" : "email",
  133. "target_field": "user"
  134. }
  135. }
  136. ]
  137. }
  138. ----
  139. // TEST[continued]
  140. You also can add other <<ingest-processors,processors>>
  141. to your ingest pipeline.
  142. You can use these processors to change or drop incoming documents
  143. based on your criteria.
  144. See <<ingest-processors>> for a list of built-in processors.
  145. [float]
  146. [[ingest-enrich-docs]]
  147. ==== Ingest and enrich documents
  148. Index incoming documents using your ingest pipeline.
  149. Because the enrich policy type is `match`,
  150. the enrich processor matches incoming documents
  151. to documents in the enrich index
  152. based on match field values.
  153. The processor then appends the enrich field data
  154. from any matching document in the enrich index
  155. to target field of the incoming document.
  156. The enrich processor appends all data to the target field as an array.
  157. If the incoming document matches more than one document in the enrich index,
  158. the processor appends data from those documents to the array.
  159. If the incoming document matches no documents in the enrich index,
  160. the processor appends no data.
  161. The following <<docs-index_,Index API>> request uses the ingest pipeline
  162. to index a document
  163. containing the `email` field,
  164. the `match_field` specified in the `users-policy` enrich policy.
  165. [source,console]
  166. ----
  167. PUT /my_index/_doc/my_id?pipeline=user_lookup
  168. {
  169. "email": "mardy.brown@asciidocsmith.com"
  170. }
  171. ----
  172. // TEST[continued]
  173. To verify the enrich processor matched
  174. and appended the appropriate field data,
  175. use the <<docs-get,get>> API to view the indexed document.
  176. [source,console]
  177. ----
  178. GET /my_index/_doc/my_id
  179. ----
  180. // TEST[continued]
  181. The API returns the following response:
  182. [source,console-result]
  183. ----
  184. {
  185. "found": true,
  186. "_index": "my_index",
  187. "_type": "_doc",
  188. "_id": "my_id",
  189. "_version": 1,
  190. "_seq_no": 55,
  191. "_primary_term": 1,
  192. "_source": {
  193. "user": [
  194. {
  195. "email": "mardy.brown@asciidocsmith.com",
  196. "first_name": "Mardy",
  197. "last_name": "Brown",
  198. "zip": 70116,
  199. "city": "New Orleans",
  200. "state": "LA"
  201. }
  202. ],
  203. "email": "mardy.brown@asciidocsmith.com"
  204. }
  205. }
  206. ----
  207. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term":1/"_primary_term" : $body._primary_term/]
  208. [float]
  209. [[update-enrich-data]]
  210. === Update your enrich index
  211. include::{docdir}/ingest/apis/enrich/execute-enrich-policy.asciidoc[tag=update-enrich-index]
  212. If wanted, you can <<docs-reindex,reindex>>
  213. or <<docs-update-by-query,update>> any already ingested documents
  214. using your ingest pipeline.
  215. [float]
  216. [[update-enrich-policies]]
  217. === Update an enrich policy
  218. include::apis/enrich/put-enrich-policy.asciidoc[tag=update-enrich-policy]
  219. ////
  220. [source,console]
  221. --------------------------------------------------
  222. DELETE /_ingest/pipeline/user_lookup
  223. DELETE /_enrich/policy/users-policy
  224. --------------------------------------------------
  225. // TEST[continued]
  226. ////