put-enrich-policy.asciidoc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[put-enrich-policy-api]]
  4. === Put enrich policy API
  5. ++++
  6. <titleabbrev>Put enrich policy</titleabbrev>
  7. ++++
  8. Creates an enrich policy.
  9. ////
  10. [source,console]
  11. ----
  12. PUT /users
  13. ----
  14. ////
  15. [source,console]
  16. ----
  17. PUT /_enrich/policy/my-policy
  18. {
  19. "match": {
  20. "indices": "users",
  21. "match_field": "email",
  22. "enrich_fields": ["first_name", "last_name", "city", "zip", "state"]
  23. }
  24. }
  25. ----
  26. // TEST[continued]
  27. ////
  28. [source,console]
  29. --------------------------------------------------
  30. DELETE /_enrich/policy/my-policy
  31. --------------------------------------------------
  32. // TEST[continued]
  33. ////
  34. [[put-enrich-policy-api-request]]
  35. ==== {api-request-title}
  36. `PUT /_enrich/policy/<enrich-policy>`
  37. [[put-enrich-policy-api-prereqs]]
  38. ==== {api-prereq-title}
  39. // tag::enrich-policy-api-prereqs[]
  40. If you use {es} {security-features}, you must have:
  41. * `read` index privileges for any indices used
  42. * The `enrich_user` {stack-ov}/built-in-roles.html[built-in role]
  43. // end::enrich-policy-api-prereqs[]
  44. [[put-enrich-policy-api-desc]]
  45. ==== {api-description-title}
  46. Use the put enrich policy API
  47. to create a new enrich policy.
  48. // tag::enrich-policy-def[]
  49. An *enrich policy* is a set of rules the enrich processor uses
  50. to append the appropriate data to incoming documents.
  51. An enrich policy contains:
  52. * The *policy type*,
  53. which determines how the processor enriches incoming documents
  54. * A list of source indices
  55. * The *match field* used to match incoming documents
  56. * *Enrich fields* appended to incoming documents
  57. from matching documents
  58. // end::enrich-policy-def[]
  59. ===== Update an enrich policy
  60. // tag::update-enrich-policy[]
  61. You cannot update an existing enrich policy.
  62. Instead, you can:
  63. . Create and execute a new enrich policy.
  64. . Replace the previous enrich policy
  65. with the new enrich policy
  66. in any in-use enrich processors.
  67. . Use the <<delete-enrich-policy-api, delete enrich policy>> API
  68. to delete the previous enrich policy.
  69. // end::update-enrich-policy[]
  70. [[put-enrich-policy-api-path-params]]
  71. ==== {api-path-parms-title}
  72. `<enrich-policy>`::
  73. (Required, string)
  74. include::{docdir}/rest-api/common-parms.asciidoc[tag=enrich-policy]
  75. [[put-enrich-policy-api-request-body]]
  76. ==== {api-request-body-title}
  77. `<policy-type>`::
  78. +
  79. --
  80. (Required, enrich policy object)
  81. The parameter key is the enrich policy type.
  82. The enrich policy type indicates
  83. how the enrich processor matches incoming documents
  84. to documents in the enrich index.
  85. Valid key values are:
  86. `match`::
  87. Match documents in the enrich index
  88. using a <<query-dsl-term-query,term query>> for the `match_field`.
  89. See <<enrich-setup>> for an example.
  90. `geo_match`::
  91. Match documents in the enrich index
  92. using a <<query-dsl-geo-shape-query,`geo_shape` query>> for the `match_field`.
  93. See <<put-enrich-policy-geo-match-ex>> for an example.
  94. The parameter value is the enrich policy.
  95. The enrich policy is a set of rules
  96. used to create an <<execute-enrich-policy,enrich index>>.
  97. The enrich processor also uses these rules
  98. to append field data to incoming documents.
  99. Parameters include:
  100. `indices`::
  101. (Required, array of strings)
  102. Source indices used to create the enrich index.
  103. `query`::
  104. (Optional, string)
  105. Query type used to find and select documents in the enrich index.
  106. Valid value is <<query-dsl-match-all-query,`match_all`>> (default).
  107. `match_field`::
  108. (Required, string)
  109. Field used to match incoming documents
  110. to documents in the enrich index.
  111. `enrich_fields`::
  112. (Required, Array of string)
  113. Fields appended to incoming documents
  114. from matching documents in the enrich index.
  115. --
  116. [[put-enrich-policy-api-example]]
  117. ==== {api-examples-title}
  118. [[put-enrich-policy-geo-match-ex]]
  119. ===== `geo_match` policy type
  120. You can use the `geo_match` enrich policy type
  121. to enrich incoming documents
  122. based on matching geo_shapes.
  123. For example,
  124. you can add postal codes
  125. to incoming documents
  126. based on a set of coordinates.
  127. To see how the `geo_match` policy type works,
  128. try the following example.
  129. Use the <<indices-create-index, create index API>>
  130. to create a source index.
  131. The field mappings for the source index
  132. must contain:
  133. * A <<geo-shape,`geo_shape`>> field
  134. which the enrich processor can use to match incoming documents
  135. * One or more enrich fields
  136. you'd like to append to incoming documents
  137. [source,console]
  138. ----
  139. PUT /postal_codes
  140. {
  141. "mappings": {
  142. "properties": {
  143. "location": {
  144. "type": "geo_shape"
  145. },
  146. "postal_code": {
  147. "type": "keyword"
  148. }
  149. }
  150. }
  151. }
  152. ----
  153. Use the <<docs-index_,index API>>
  154. to index data to this source index.
  155. [source,console]
  156. ----
  157. PUT /postal_codes/_doc/1?refresh=wait_for
  158. {
  159. "location": {
  160. "type": "envelope",
  161. "coordinates": [[13.0, 53.0], [14.0, 52.0]]
  162. },
  163. "postal_code": "96598"
  164. }
  165. ----
  166. // TEST[continued]
  167. Use the put enrich policy API
  168. to create an enrich policy
  169. with the `geo_match` policy type.
  170. This policy must include:
  171. * One or more source indices
  172. * A `match_field`,
  173. the `geo_shape` field from the source indices
  174. used to match incoming documents
  175. * Enrich fields from the source indices
  176. you'd like to append to incoming documents
  177. [source,console]
  178. ----
  179. PUT /_enrich/policy/postal_policy
  180. {
  181. "geo_match": {
  182. "indices": "postal_codes",
  183. "match_field": "location",
  184. "enrich_fields": ["location","postal_code"]
  185. }
  186. }
  187. ----
  188. // TEST[continued]
  189. Use the <<execute-enrich-policy-api,execute enrich policy API>>
  190. to create an enrich index for the policy.
  191. include::execute-enrich-policy.asciidoc[tag=execute-enrich-policy-def]
  192. [source,console]
  193. ----
  194. POST /_enrich/policy/postal_policy/_execute
  195. ----
  196. // TEST[continued]
  197. Use the <<put-pipeline-api,put pipeline API>>
  198. to create an ingest pipeline.
  199. In the pipeline,
  200. add an <<enrich-processor,enrich processor>>
  201. that includes:
  202. * Your enrich policy
  203. * The `field` of incoming documents used
  204. to match the geo_shape of documents from the enrich index.
  205. * The `target_field` used
  206. to store appended enrich data for incoming documents.
  207. * The `shape_relation`,
  208. which indicates how the processor matches geo_shapes in incoming documents
  209. to geo_shapes in documents from the enrich index.
  210. See <<_spatial_relations>> for valid options and more information.
  211. [source,console]
  212. ----
  213. PUT /_ingest/pipeline/postal_lookup
  214. {
  215. "description": "Enrich postal codes",
  216. "processors": [
  217. {
  218. "enrich": {
  219. "policy_name": "postal_policy",
  220. "field": "geo_location",
  221. "target_field": "geo_data",
  222. "shape_relation": "INTERSECTS"
  223. }
  224. }
  225. ]
  226. }
  227. ----
  228. // TEST[continued]
  229. Use the ingest pipeline
  230. to index a document.
  231. The incoming document
  232. should include the `field`
  233. specified in your enrich processor.
  234. [source,console]
  235. ----
  236. PUT /users/_doc/0?pipeline=postal_lookup
  237. {
  238. "first_name": "Mardy",
  239. "last_name": "Brown",
  240. "geo_location": "POINT (13.5 52.5)"
  241. }
  242. ----
  243. // TEST[continued]
  244. To verify the enrich processor matched
  245. and appended the appropriate field data,
  246. use the <<docs-get,get API>>
  247. to view the indexed document.
  248. [source,console]
  249. ----
  250. GET /users/_doc/0
  251. ----
  252. // TEST[continued]
  253. The API returns the following response:
  254. [source,console-result]
  255. ----
  256. {
  257. "found": true,
  258. "_index": "users",
  259. "_id": "0",
  260. "_version": 1,
  261. "_seq_no": 55,
  262. "_primary_term": 1,
  263. "_source": {
  264. "geo_data": [
  265. {
  266. "location": {
  267. "type": "envelope",
  268. "coordinates": [[13.0, 53.0], [14.0, 52.0]]
  269. },
  270. "postal_code": "96598"
  271. }
  272. ],
  273. "first_name": "Mardy",
  274. "last_name": "Brown",
  275. "geo_location": "POINT (13.5 52.5)"
  276. }
  277. }
  278. ----
  279. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term":1/"_primary_term" : $body._primary_term/]
  280. ////
  281. [source,console]
  282. --------------------------------------------------
  283. DELETE /_ingest/pipeline/postal_lookup
  284. DELETE /_enrich/policy/postal_policy
  285. --------------------------------------------------
  286. // TEST[continued]
  287. ////