geo-match-enrich-policy-type-ex.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. [role="xpack"]
  2. [[geo-match-enrich-policy-type]]
  3. === Example: Enrich your data based on geolocation
  4. `geo_match` <<enrich-policy,enrich policies>> match enrich data to incoming
  5. documents based on a geographic location, using a
  6. <<query-dsl-geo-shape-query,`geo_shape` query>>.
  7. The following example creates a `geo_match` enrich policy that adds postal
  8. codes to incoming documents based on a set of coordinates. It then adds the
  9. `geo_match` enrich policy to a processor in an ingest pipeline.
  10. Use the <<indices-create-index,create index API>> to create a source index
  11. containing at least one `geo_shape` field.
  12. [source,console]
  13. ----
  14. PUT /postal_codes
  15. {
  16. "mappings": {
  17. "properties": {
  18. "location": {
  19. "type": "geo_shape"
  20. },
  21. "postal_code": {
  22. "type": "keyword"
  23. }
  24. }
  25. }
  26. }
  27. ----
  28. Use the <<docs-index_,index API>> to index enrich data to this source index.
  29. [source,console]
  30. ----
  31. PUT /postal_codes/_doc/1?refresh=wait_for
  32. {
  33. "location": {
  34. "type": "envelope",
  35. "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
  36. },
  37. "postal_code": "96598"
  38. }
  39. ----
  40. // TEST[continued]
  41. Use the <<put-enrich-policy-api,create enrich policy API>> to create
  42. an enrich policy with the `geo_match` policy type. This policy must include:
  43. * One or more source indices
  44. * A `match_field`,
  45. the `geo_shape` field from the source indices used to match incoming documents
  46. * Enrich fields from the source indices you'd like to append to incoming
  47. documents
  48. [source,console]
  49. ----
  50. PUT /_enrich/policy/postal_policy
  51. {
  52. "geo_match": {
  53. "indices": "postal_codes",
  54. "match_field": "location",
  55. "enrich_fields": [ "location", "postal_code" ]
  56. }
  57. }
  58. ----
  59. // TEST[continued]
  60. Use the <<execute-enrich-policy-api,execute enrich policy API>> to create an
  61. enrich index for the policy.
  62. [source,console]
  63. ----
  64. POST /_enrich/policy/postal_policy/_execute?wait_for_completion=false
  65. ----
  66. // TEST[s/\?wait_for_completion=false//]
  67. // TEST[continued]
  68. Use the <<put-pipeline-api,create or update pipeline API>> to create an ingest
  69. pipeline. In the pipeline, add an <<enrich-processor,enrich processor>> that
  70. includes:
  71. * Your enrich policy.
  72. * The `field` of incoming documents used to match the geoshape of documents
  73. from the enrich index.
  74. * The `target_field` used to store appended enrich data for incoming documents.
  75. This field contains the `match_field` and `enrich_fields` specified in your
  76. enrich policy.
  77. * The `shape_relation`, which indicates how the processor matches geoshapes in
  78. incoming documents to geoshapes in documents from the enrich index. See
  79. <<_spatial_relations>> for valid options and more information.
  80. [source,console]
  81. ----
  82. PUT /_ingest/pipeline/postal_lookup
  83. {
  84. "processors": [
  85. {
  86. "enrich": {
  87. "description": "Add 'geo_data' based on 'geo_location'",
  88. "policy_name": "postal_policy",
  89. "field": "geo_location",
  90. "target_field": "geo_data",
  91. "shape_relation": "INTERSECTS"
  92. }
  93. }
  94. ]
  95. }
  96. ----
  97. // TEST[continued]
  98. Use the ingest pipeline to index a document. The incoming document should
  99. include the `field` specified in your enrich processor.
  100. [source,console]
  101. ----
  102. PUT /users/_doc/0?pipeline=postal_lookup
  103. {
  104. "first_name": "Mardy",
  105. "last_name": "Brown",
  106. "geo_location": "POINT (13.5 52.5)"
  107. }
  108. ----
  109. // TEST[continued]
  110. To verify the enrich processor matched and appended the appropriate field data,
  111. use the <<docs-get,get API>> to view the indexed document.
  112. [source,console]
  113. ----
  114. GET /users/_doc/0
  115. ----
  116. // TEST[continued]
  117. The API returns the following response:
  118. [source,console-result]
  119. ----
  120. {
  121. "found": true,
  122. "_index": "users",
  123. "_id": "0",
  124. "_version": 1,
  125. "_seq_no": 55,
  126. "_primary_term": 1,
  127. "_source": {
  128. "geo_data": {
  129. "location": {
  130. "type": "envelope",
  131. "coordinates": [[13.0, 53.0], [14.0, 52.0]]
  132. },
  133. "postal_code": "96598"
  134. },
  135. "first_name": "Mardy",
  136. "last_name": "Brown",
  137. "geo_location": "POINT (13.5 52.5)"
  138. }
  139. }
  140. ----
  141. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term":1/"_primary_term" : $body._primary_term/]
  142. ////
  143. [source,console]
  144. --------------------------------------------------
  145. DELETE /_ingest/pipeline/postal_lookup
  146. DELETE /_enrich/policy/postal_policy
  147. DELETE /postal_codes
  148. DELETE /users
  149. --------------------------------------------------
  150. // TEST[continued]
  151. ////