enrich.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[ingest-enriching-data]]
  4. == Enrich your data
  5. You can use the <<enrich-processor,enrich processor>> to add data from your
  6. existing indices to incoming documents during ingest.
  7. For example, you can use the enrich processor to:
  8. * Identify web services or vendors based on known IP addresses
  9. * Add product information to retail orders based on product IDs
  10. * Supplement contact information based on an email address
  11. * Add postal codes based on user coordinates
  12. [discrete]
  13. [[how-enrich-works]]
  14. === How the enrich processor works
  15. Most processors are self-contained and only change _existing_ data in incoming
  16. documents.
  17. image::images/ingest/ingest-process.svg[align="center"]
  18. The enrich processor adds _new_ data to incoming documents and requires a few
  19. special components:
  20. image::images/ingest/enrich/enrich-process.svg[align="center"]
  21. [[enrich-policy]]
  22. enrich policy::
  23. +
  24. --
  25. A set of configuration options used to add the right enrich data to the right
  26. incoming documents.
  27. An enrich policy contains:
  28. // tag::enrich-policy-fields[]
  29. * A list of one or more _source indices_ which store enrich data as documents
  30. * The _policy type_ which determines how the processor matches the enrich data
  31. to incoming documents
  32. * A _match field_ from the source indices used to match incoming documents
  33. * _Enrich fields_ containing enrich data from the source indices you want to add
  34. to incoming documents
  35. // end::enrich-policy-fields[]
  36. Before it can be used with an enrich processor, an enrich policy must be
  37. <<execute-enrich-policy-api,executed>>. When executed, an enrich policy uses
  38. enrich data from the policy's source indices to create a streamlined system
  39. index called the _enrich index_. The processor uses this index to match and
  40. enrich incoming documents.
  41. --
  42. [[source-index]]
  43. source index::
  44. An index which stores enrich data you'd like to add to incoming documents. You
  45. can create and manage these indices just like a regular {es} index. You can use
  46. multiple source indices in an enrich policy. You also can use the same source
  47. index in multiple enrich policies.
  48. [[enrich-index]]
  49. enrich index::
  50. +
  51. --
  52. A special system index tied to a specific enrich policy.
  53. Directly matching incoming documents to documents in source indices could be
  54. slow and resource intensive. To speed things up, the enrich processor uses an
  55. enrich index.
  56. Enrich indices contain enrich data from source indices but have a few special
  57. properties to help streamline them:
  58. * They are system indices, meaning they're managed internally by {es} and only
  59. intended for use with enrich processors.
  60. * They always begin with `.enrich-*`.
  61. * They are read-only, meaning you can't directly change them.
  62. * They are <<indices-forcemerge,force merged>> for fast retrieval.
  63. --
  64. [role="xpack"]
  65. [testenv="basic"]
  66. [[enrich-setup]]
  67. === Set up an enrich processor
  68. To set up an enrich processor, follow these steps:
  69. . Check the <<enrich-prereqs, prerequisites>>.
  70. . <<create-enrich-source-index>>.
  71. . <<create-enrich-policy>>.
  72. . <<execute-enrich-policy>>.
  73. . <<add-enrich-processor>>.
  74. . <<ingest-enrich-docs>>.
  75. Once you have an enrich processor set up,
  76. you can <<update-enrich-data,update your enrich data>>
  77. and <<update-enrich-policies, update your enrich policies>>.
  78. [IMPORTANT]
  79. ====
  80. The enrich processor performs several operations and may impact the speed of
  81. your ingest pipeline.
  82. We strongly recommend testing and benchmarking your enrich processors
  83. before deploying them in production.
  84. We do not recommend using the enrich processor to append real-time data.
  85. The enrich processor works best with reference data
  86. that doesn't change frequently.
  87. ====
  88. [discrete]
  89. [[enrich-prereqs]]
  90. ==== Prerequisites
  91. include::{es-repo-dir}/ingest/apis/enrich/put-enrich-policy.asciidoc[tag=enrich-policy-api-prereqs]
  92. [[create-enrich-source-index]]
  93. ==== Add enrich data
  94. To begin, add documents to one or more source indices. These documents should
  95. contain the enrich data you eventually want to add to incoming documents.
  96. You can manage source indices just like regular {es} indices using the
  97. <<docs,document>> and <<indices,index>> APIs.
  98. You also can set up {beats-ref}/getting-started.html[{beats}], such as a
  99. {filebeat-ref}/filebeat-installation-configuration.html[{filebeat}], to
  100. automatically send and index documents to your source indices. See
  101. {beats-ref}/getting-started.html[Getting started with {beats}].
  102. [[create-enrich-policy]]
  103. ==== Create an enrich policy
  104. After adding enrich data to your source indices, use the
  105. <<put-enrich-policy-api,create enrich policy API>> to create an enrich policy.
  106. [WARNING]
  107. ====
  108. Once created, you can't update or change an enrich policy.
  109. See <<update-enrich-policies>>.
  110. ====
  111. [[execute-enrich-policy]]
  112. ==== Execute the enrich policy
  113. Once the enrich policy is created, you can execute it using the
  114. <<execute-enrich-policy-api,execute enrich policy API>> to create an
  115. <<enrich-index,enrich index>>.
  116. image::images/ingest/enrich/enrich-policy-index.svg[align="center"]
  117. include::apis/enrich/execute-enrich-policy.asciidoc[tag=execute-enrich-policy-def]
  118. [[add-enrich-processor]]
  119. ==== Add an enrich processor to an ingest pipeline
  120. Once you have source indices, an enrich policy, and the related enrich index in
  121. place, you can set up an ingest pipeline that includes an enrich processor for
  122. your policy.
  123. image::images/ingest/enrich/enrich-processor.svg[align="center"]
  124. Define an <<enrich-processor,enrich processor>> and add it to an ingest
  125. pipeline using the <<put-pipeline-api,create or update pipeline API>>.
  126. When defining the enrich processor, you must include at least the following:
  127. * The enrich policy to use.
  128. * The field used to match incoming documents to the documents in your enrich index.
  129. * The target field to add to incoming documents. This target field contains the
  130. match and enrich fields specified in your enrich policy.
  131. You also can use the `max_matches` option to set the number of enrich documents
  132. an incoming document can match. If set to the default of `1`, data is added to
  133. an incoming document's target field as a JSON object. Otherwise, the data is
  134. added as an array.
  135. See <<enrich-processor>> for a full list of configuration options.
  136. You also can add other <<processors,processors>> to your ingest pipeline.
  137. [[ingest-enrich-docs]]
  138. ==== Ingest and enrich documents
  139. You can now use your ingest pipeline to enrich and index documents.
  140. image::images/ingest/enrich/enrich-process.svg[align="center"]
  141. Before implementing the pipeline in production, we recommend indexing a few test
  142. documents first and verifying enrich data was added correctly using the
  143. <<docs-get,get API>>.
  144. [[update-enrich-data]]
  145. ==== Update an enrich index
  146. include::{es-repo-dir}/ingest/apis/enrich/execute-enrich-policy.asciidoc[tag=update-enrich-index]
  147. If wanted, you can <<docs-reindex,reindex>>
  148. or <<docs-update-by-query,update>> any already ingested documents
  149. using your ingest pipeline.
  150. [[update-enrich-policies]]
  151. ==== Update an enrich policy
  152. // tag::update-enrich-policy[]
  153. Once created, you can't update or change an enrich policy.
  154. Instead, you can:
  155. . Create and <<execute-enrich-policy-api,execute>> a new enrich policy.
  156. . Replace the previous enrich policy
  157. with the new enrich policy
  158. in any in-use enrich processors.
  159. . Use the <<delete-enrich-policy-api, delete enrich policy>> API
  160. to delete the previous enrich policy.
  161. // end::update-enrich-policy[]
  162. [role="xpack"]
  163. [testenv="basic"]
  164. [[geo-match-enrich-policy-type]]
  165. === Example: Enrich your data based on geolocation
  166. `geo_match` <<enrich-policy,enrich policies>> match enrich data to incoming
  167. documents based on a geographic location, using a
  168. <<query-dsl-geo-shape-query,`geo_shape` query>>.
  169. The following example creates a `geo_match` enrich policy that adds postal
  170. codes to incoming documents based on a set of coordinates. It then adds the
  171. `geo_match` enrich policy to a processor in an ingest pipeline.
  172. Use the <<indices-create-index,create index API>> to create a source index
  173. containing at least one `geo_shape` field.
  174. [source,console]
  175. ----
  176. PUT /postal_codes
  177. {
  178. "mappings": {
  179. "properties": {
  180. "location": {
  181. "type": "geo_shape"
  182. },
  183. "postal_code": {
  184. "type": "keyword"
  185. }
  186. }
  187. }
  188. }
  189. ----
  190. Use the <<docs-index_,index API>> to index enrich data to this source index.
  191. [source,console]
  192. ----
  193. PUT /postal_codes/_doc/1?refresh=wait_for
  194. {
  195. "location": {
  196. "type": "envelope",
  197. "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
  198. },
  199. "postal_code": "96598"
  200. }
  201. ----
  202. // TEST[continued]
  203. Use the <<put-enrich-policy-api,create or update enrich policy API>> to create
  204. an enrich policy with the `geo_match` policy type. This policy must include:
  205. * One or more source indices
  206. * A `match_field`,
  207. the `geo_shape` field from the source indices used to match incoming documents
  208. * Enrich fields from the source indices you'd like to append to incoming
  209. documents
  210. [source,console]
  211. ----
  212. PUT /_enrich/policy/postal_policy
  213. {
  214. "geo_match": {
  215. "indices": "postal_codes",
  216. "match_field": "location",
  217. "enrich_fields": [ "location", "postal_code" ]
  218. }
  219. }
  220. ----
  221. // TEST[continued]
  222. Use the <<execute-enrich-policy-api,execute enrich policy API>> to create an
  223. enrich index for the policy.
  224. [source,console]
  225. ----
  226. POST /_enrich/policy/postal_policy/_execute
  227. ----
  228. // TEST[continued]
  229. Use the <<put-pipeline-api,create or update pipeline API>> to create an ingest
  230. pipeline. In the pipeline, add an <<enrich-processor,enrich processor>> that
  231. includes:
  232. * Your enrich policy.
  233. * The `field` of incoming documents used to match the geo_shape of documents
  234. from the enrich index.
  235. * The `target_field` used to store appended enrich data for incoming documents.
  236. This field contains the `match_field` and `enrich_fields` specified in your
  237. enrich policy.
  238. * The `shape_relation`, which indicates how the processor matches geo_shapes in
  239. incoming documents to geo_shapes in documents from the enrich index. See
  240. <<_spatial_relations>> for valid options and more information.
  241. [source,console]
  242. ----
  243. PUT /_ingest/pipeline/postal_lookup
  244. {
  245. "processors": [
  246. {
  247. "enrich": {
  248. "description": "Add 'geo_data' based on 'geo_location'",
  249. "policy_name": "postal_policy",
  250. "field": "geo_location",
  251. "target_field": "geo_data",
  252. "shape_relation": "INTERSECTS"
  253. }
  254. }
  255. ]
  256. }
  257. ----
  258. // TEST[continued]
  259. Use the ingest pipeline to index a document. The incoming document should
  260. include the `field` specified in your enrich processor.
  261. [source,console]
  262. ----
  263. PUT /users/_doc/0?pipeline=postal_lookup
  264. {
  265. "first_name": "Mardy",
  266. "last_name": "Brown",
  267. "geo_location": "POINT (13.5 52.5)"
  268. }
  269. ----
  270. // TEST[continued]
  271. To verify the enrich processor matched and appended the appropriate field data,
  272. use the <<docs-get,get API>> to view the indexed document.
  273. [source,console]
  274. ----
  275. GET /users/_doc/0
  276. ----
  277. // TEST[continued]
  278. The API returns the following response:
  279. [source,console-result]
  280. ----
  281. {
  282. "found": true,
  283. "_index": "users",
  284. "_id": "0",
  285. "_version": 1,
  286. "_seq_no": 55,
  287. "_primary_term": 1,
  288. "_source": {
  289. "geo_data": {
  290. "location": {
  291. "type": "envelope",
  292. "coordinates": [[13.0, 53.0], [14.0, 52.0]]
  293. },
  294. "postal_code": "96598"
  295. },
  296. "first_name": "Mardy",
  297. "last_name": "Brown",
  298. "geo_location": "POINT (13.5 52.5)"
  299. }
  300. }
  301. ----
  302. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term":1/"_primary_term" : $body._primary_term/]
  303. ////
  304. [source,console]
  305. --------------------------------------------------
  306. DELETE /_ingest/pipeline/postal_lookup
  307. DELETE /_enrich/policy/postal_policy
  308. --------------------------------------------------
  309. // TEST[continued]
  310. ////
  311. [role="xpack"]
  312. [testenv="basic"]
  313. [[match-enrich-policy-type]]
  314. === Example: Enrich your data based on exact values
  315. `match` <<enrich-policy,enrich policies>> match enrich data to incoming
  316. documents based on an exact value, such as a email address or ID, using a
  317. <<query-dsl-term-query,`term` query>>.
  318. The following example creates a `match` enrich policy that adds user name and
  319. contact information to incoming documents based on an email address. It then
  320. adds the `match` enrich policy to a processor in an ingest pipeline.
  321. Use the <<indices-create-index, create index API>> or <<docs-index_,index
  322. API>> to create a source index.
  323. The following index API request creates a source index and indexes a
  324. new document to that index.
  325. [source,console]
  326. ----
  327. PUT /users/_doc/1?refresh=wait_for
  328. {
  329. "email": "mardy.brown@asciidocsmith.com",
  330. "first_name": "Mardy",
  331. "last_name": "Brown",
  332. "city": "New Orleans",
  333. "county": "Orleans",
  334. "state": "LA",
  335. "zip": 70116,
  336. "web": "mardy.asciidocsmith.com"
  337. }
  338. ----
  339. Use the create or update enrich policy API to create an enrich policy with the
  340. `match` policy type. This policy must include:
  341. * One or more source indices
  342. * A `match_field`,
  343. the field from the source indices used to match incoming documents
  344. * Enrich fields from the source indices you'd like to append to incoming
  345. documents
  346. [source,console]
  347. ----
  348. PUT /_enrich/policy/users-policy
  349. {
  350. "match": {
  351. "indices": "users",
  352. "match_field": "email",
  353. "enrich_fields": ["first_name", "last_name", "city", "zip", "state"]
  354. }
  355. }
  356. ----
  357. // TEST[continued]
  358. Use the <<execute-enrich-policy-api,execute enrich policy API>> to create an
  359. enrich index for the policy.
  360. [source,console]
  361. ----
  362. POST /_enrich/policy/users-policy/_execute
  363. ----
  364. // TEST[continued]
  365. Use the <<put-pipeline-api,create or update pipeline API>> to create an ingest
  366. pipeline. In the pipeline, add an <<enrich-processor,enrich processor>> that
  367. includes:
  368. * Your enrich policy.
  369. * The `field` of incoming documents used to match documents
  370. from the enrich index.
  371. * The `target_field` used to store appended enrich data for incoming documents.
  372. This field contains the `match_field` and `enrich_fields` specified in your
  373. enrich policy.
  374. [source,console]
  375. ----
  376. PUT /_ingest/pipeline/user_lookup
  377. {
  378. "processors" : [
  379. {
  380. "enrich" : {
  381. "description": "Add 'user' data based on 'email'",
  382. "policy_name": "users-policy",
  383. "field" : "email",
  384. "target_field": "user",
  385. "max_matches": "1"
  386. }
  387. }
  388. ]
  389. }
  390. ----
  391. // TEST[continued]
  392. Use the ingest pipeline to index a document. The incoming document should
  393. include the `field` specified in your enrich processor.
  394. [source,console]
  395. ----
  396. PUT /my-index-000001/_doc/my_id?pipeline=user_lookup
  397. {
  398. "email": "mardy.brown@asciidocsmith.com"
  399. }
  400. ----
  401. // TEST[continued]
  402. To verify the enrich processor matched and appended the appropriate field data,
  403. use the <<docs-get,get API>> to view the indexed document.
  404. [source,console]
  405. ----
  406. GET /my-index-000001/_doc/my_id
  407. ----
  408. // TEST[continued]
  409. The API returns the following response:
  410. [source,console-result]
  411. ----
  412. {
  413. "found": true,
  414. "_index": "my-index-000001",
  415. "_id": "my_id",
  416. "_version": 1,
  417. "_seq_no": 55,
  418. "_primary_term": 1,
  419. "_source": {
  420. "user": {
  421. "email": "mardy.brown@asciidocsmith.com",
  422. "first_name": "Mardy",
  423. "last_name": "Brown",
  424. "zip": 70116,
  425. "city": "New Orleans",
  426. "state": "LA"
  427. },
  428. "email": "mardy.brown@asciidocsmith.com"
  429. }
  430. }
  431. ----
  432. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term":1/"_primary_term" : $body._primary_term/]
  433. ////
  434. [source,console]
  435. --------------------------------------------------
  436. DELETE /_ingest/pipeline/user_lookup
  437. DELETE /_enrich/policy/users-policy
  438. --------------------------------------------------
  439. // TEST[continued]
  440. ////