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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[geo-match-enrich-policy-type]]
  4. === Example: Enrich your data based on geolocation
  5. `geo_match` <<enrich-policy,enrich policies>> match enrich data to incoming
  6. documents based on a geographic location, using a
  7. <<query-dsl-geo-shape-query,`geo_shape` query>>.
  8. The following example creates a `geo_match` enrich policy that adds postal
  9. codes to incoming documents based on a set of coordinates. It then adds the
  10. `geo_match` enrich policy to a processor in an ingest pipeline.
  11. Use the <<indices-create-index,create index API>> to create a source index
  12. containing at least one `geo_shape` field.
  13. [source,console]
  14. ----
  15. PUT /postal_codes
  16. {
  17. "mappings": {
  18. "properties": {
  19. "location": {
  20. "type": "geo_shape"
  21. },
  22. "postal_code": {
  23. "type": "keyword"
  24. }
  25. }
  26. }
  27. }
  28. ----
  29. Use the <<docs-index_,index API>> to index enrich data to this source index.
  30. [source,console]
  31. ----
  32. PUT /postal_codes/_doc/1?refresh=wait_for
  33. {
  34. "location": {
  35. "type": "envelope",
  36. "coordinates": [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]
  37. },
  38. "postal_code": "96598"
  39. }
  40. ----
  41. // TEST[continued]
  42. Use the <<put-enrich-policy-api,create enrich policy API>> to create
  43. an enrich policy with the `geo_match` policy type. This policy must include:
  44. * One or more source indices
  45. * A `match_field`,
  46. the `geo_shape` field from the source indices used to match incoming documents
  47. * Enrich fields from the source indices you'd like to append to incoming
  48. documents
  49. [source,console]
  50. ----
  51. PUT /_enrich/policy/postal_policy
  52. {
  53. "geo_match": {
  54. "indices": "postal_codes",
  55. "match_field": "location",
  56. "enrich_fields": [ "location", "postal_code" ]
  57. }
  58. }
  59. ----
  60. // TEST[continued]
  61. Use the <<execute-enrich-policy-api,execute enrich policy API>> to create an
  62. enrich index for the policy.
  63. [source,console]
  64. ----
  65. POST /_enrich/policy/postal_policy/_execute
  66. ----
  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. --------------------------------------------------
  148. // TEST[continued]
  149. ////