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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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
  65. ----
  66. // TEST[continued]
  67. Use the <<put-pipeline-api,create or update pipeline API>> to create an ingest
  68. pipeline. In the pipeline, add an <<enrich-processor,enrich processor>> that
  69. includes:
  70. * Your enrich policy.
  71. * The `field` of incoming documents used to match the geoshape of documents
  72. from the enrich index.
  73. * The `target_field` used to store appended enrich data for incoming documents.
  74. This field contains the `match_field` and `enrich_fields` specified in your
  75. enrich policy.
  76. * The `shape_relation`, which indicates how the processor matches geoshapes in
  77. incoming documents to geoshapes in documents from the enrich index. See
  78. <<_spatial_relations>> for valid options and more information.
  79. [source,console]
  80. ----
  81. PUT /_ingest/pipeline/postal_lookup
  82. {
  83. "processors": [
  84. {
  85. "enrich": {
  86. "description": "Add 'geo_data' based on 'geo_location'",
  87. "policy_name": "postal_policy",
  88. "field": "geo_location",
  89. "target_field": "geo_data",
  90. "shape_relation": "INTERSECTS"
  91. }
  92. }
  93. ]
  94. }
  95. ----
  96. // TEST[continued]
  97. Use the ingest pipeline to index a document. The incoming document should
  98. include the `field` specified in your enrich processor.
  99. [source,console]
  100. ----
  101. PUT /users/_doc/0?pipeline=postal_lookup
  102. {
  103. "first_name": "Mardy",
  104. "last_name": "Brown",
  105. "geo_location": "POINT (13.5 52.5)"
  106. }
  107. ----
  108. // TEST[continued]
  109. To verify the enrich processor matched and appended the appropriate field data,
  110. use the <<docs-get,get API>> to view the indexed document.
  111. [source,console]
  112. ----
  113. GET /users/_doc/0
  114. ----
  115. // TEST[continued]
  116. The API returns the following response:
  117. [source,console-result]
  118. ----
  119. {
  120. "found": true,
  121. "_index": "users",
  122. "_id": "0",
  123. "_version": 1,
  124. "_seq_no": 55,
  125. "_primary_term": 1,
  126. "_source": {
  127. "geo_data": {
  128. "location": {
  129. "type": "envelope",
  130. "coordinates": [[13.0, 53.0], [14.0, 52.0]]
  131. },
  132. "postal_code": "96598"
  133. },
  134. "first_name": "Mardy",
  135. "last_name": "Brown",
  136. "geo_location": "POINT (13.5 52.5)"
  137. }
  138. }
  139. ----
  140. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term":1/"_primary_term" : $body._primary_term/]
  141. ////
  142. [source,console]
  143. --------------------------------------------------
  144. DELETE /_ingest/pipeline/postal_lookup
  145. DELETE /_enrich/policy/postal_policy
  146. DELETE /postal_codes
  147. DELETE /users
  148. --------------------------------------------------
  149. // TEST[continued]
  150. ////