match-enrich-policy-type-ex.asciidoc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. [role="xpack"]
  2. [[match-enrich-policy-type]]
  3. === Example: Enrich your data based on exact values
  4. `match` <<enrich-policy,enrich policies>> match enrich data to incoming
  5. documents based on an exact value, such as a email address or ID, using a
  6. <<query-dsl-term-query,`term` query>>.
  7. The following example creates a `match` enrich policy that adds user name and
  8. contact information to incoming documents based on an email address. It then
  9. adds the `match` enrich policy to a processor in an ingest pipeline.
  10. Use the <<indices-create-index, create index API>> or <<docs-index_,index
  11. API>> to create a source index.
  12. The following index API request creates a source index and indexes a
  13. new document to that index.
  14. [source,console]
  15. ----
  16. PUT /users/_doc/1?refresh=wait_for
  17. {
  18. "email": "mardy.brown@asciidocsmith.com",
  19. "first_name": "Mardy",
  20. "last_name": "Brown",
  21. "city": "New Orleans",
  22. "county": "Orleans",
  23. "state": "LA",
  24. "zip": 70116,
  25. "web": "mardy.asciidocsmith.com"
  26. }
  27. ----
  28. Use the create enrich policy API to create an enrich policy with the
  29. `match` policy type. This policy must include:
  30. * One or more source indices
  31. * A `match_field`,
  32. the field from the source indices used to match incoming documents
  33. * Enrich fields from the source indices you'd like to append to incoming
  34. documents
  35. [source,console]
  36. ----
  37. PUT /_enrich/policy/users-policy
  38. {
  39. "match": {
  40. "indices": "users",
  41. "match_field": "email",
  42. "enrich_fields": ["first_name", "last_name", "city", "zip", "state"]
  43. }
  44. }
  45. ----
  46. // TEST[continued]
  47. Use the <<execute-enrich-policy-api,execute enrich policy API>> to create an
  48. enrich index for the policy.
  49. [source,console]
  50. ----
  51. POST /_enrich/policy/users-policy/_execute?wait_for_completion=false
  52. ----
  53. // TEST[s/\?wait_for_completion=false//]
  54. // TEST[continued]
  55. Use the <<put-pipeline-api,create or update pipeline API>> to create an ingest
  56. pipeline. In the pipeline, add an <<enrich-processor,enrich processor>> that
  57. includes:
  58. * Your enrich policy.
  59. * The `field` of incoming documents used to match documents
  60. from the enrich index.
  61. * The `target_field` used to store appended enrich data for incoming documents.
  62. This field contains the `match_field` and `enrich_fields` specified in your
  63. enrich policy.
  64. [source,console]
  65. ----
  66. PUT /_ingest/pipeline/user_lookup
  67. {
  68. "processors" : [
  69. {
  70. "enrich" : {
  71. "description": "Add 'user' data based on 'email'",
  72. "policy_name": "users-policy",
  73. "field" : "email",
  74. "target_field": "user",
  75. "max_matches": "1"
  76. }
  77. }
  78. ]
  79. }
  80. ----
  81. // TEST[continued]
  82. Use the ingest pipeline to index a document. The incoming document should
  83. include the `field` specified in your enrich processor.
  84. [source,console]
  85. ----
  86. PUT /my-index-000001/_doc/my_id?pipeline=user_lookup
  87. {
  88. "email": "mardy.brown@asciidocsmith.com"
  89. }
  90. ----
  91. // TEST[continued]
  92. To verify the enrich processor matched and appended the appropriate field data,
  93. use the <<docs-get,get API>> to view the indexed document.
  94. [source,console]
  95. ----
  96. GET /my-index-000001/_doc/my_id
  97. ----
  98. // TEST[continued]
  99. The API returns the following response:
  100. [source,console-result]
  101. ----
  102. {
  103. "found": true,
  104. "_index": "my-index-000001",
  105. "_id": "my_id",
  106. "_version": 1,
  107. "_seq_no": 55,
  108. "_primary_term": 1,
  109. "_source": {
  110. "user": {
  111. "email": "mardy.brown@asciidocsmith.com",
  112. "first_name": "Mardy",
  113. "last_name": "Brown",
  114. "zip": 70116,
  115. "city": "New Orleans",
  116. "state": "LA"
  117. },
  118. "email": "mardy.brown@asciidocsmith.com"
  119. }
  120. }
  121. ----
  122. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term":1/"_primary_term" : $body._primary_term/]
  123. ////
  124. [source,console]
  125. --------------------------------------------------
  126. DELETE /_ingest/pipeline/user_lookup
  127. DELETE /_enrich/policy/users-policy
  128. DELETE /my-index-000001
  129. DELETE /users
  130. --------------------------------------------------
  131. // TEST[continued]
  132. ////