range-enrich-policy-type-ex.asciidoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. [role="xpack"]
  2. [[range-enrich-policy-type]]
  3. === Example: Enrich your data by matching a value to a range
  4. A `range` <<enrich-policy,enrich policy>> uses a <<query-dsl-term-query,`term`
  5. query>> to match a number, date, or IP address in incoming documents to a range
  6. of the same type in the enrich index. Matching a range to a range is not
  7. supported.
  8. The following example creates a `range` enrich policy that adds a descriptive network name and
  9. responsible department to incoming documents based on an IP address. It then
  10. adds the enrich policy to a processor in an ingest pipeline.
  11. Use the <<indices-create-index, create index API>> with the appropriate mappings to create a source index.
  12. [source,console]
  13. ----
  14. PUT /networks
  15. {
  16. "mappings": {
  17. "properties": {
  18. "range": { "type": "ip_range" },
  19. "name": { "type": "keyword" },
  20. "department": { "type": "keyword" }
  21. }
  22. }
  23. }
  24. ----
  25. The following index API request indexes a new document to that index.
  26. [source,console]
  27. ----
  28. PUT /networks/_doc/1?refresh=wait_for
  29. {
  30. "range": "10.100.0.0/16",
  31. "name": "production",
  32. "department": "OPS"
  33. }
  34. ----
  35. // TEST[continued]
  36. Use the create enrich policy API to create an enrich policy with the
  37. `range` policy type. This policy must include:
  38. * One or more source indices
  39. * A `match_field`,
  40. the field from the source indices used to match incoming documents
  41. * Enrich fields from the source indices you'd like to append to incoming
  42. documents
  43. Since we plan to enrich documents based on an IP address, the policy's
  44. `match_field` must be an `ip_range` field.
  45. [source,console]
  46. ----
  47. PUT /_enrich/policy/networks-policy
  48. {
  49. "range": {
  50. "indices": "networks",
  51. "match_field": "range",
  52. "enrich_fields": ["name", "department"]
  53. }
  54. }
  55. ----
  56. // TEST[continued]
  57. Use the <<execute-enrich-policy-api,execute enrich policy API>> to create an
  58. enrich index for the policy.
  59. [source,console]
  60. ----
  61. POST /_enrich/policy/networks-policy/_execute
  62. ----
  63. // TEST[continued]
  64. Use the <<put-pipeline-api,create or update pipeline API>> to create an ingest
  65. pipeline. In the pipeline, add an <<enrich-processor,enrich processor>> that
  66. includes:
  67. * Your enrich policy.
  68. * The `field` of incoming documents used to match documents
  69. from the enrich index.
  70. * The `target_field` used to store appended enrich data for incoming documents.
  71. This field contains the `match_field` and `enrich_fields` specified in your
  72. enrich policy.
  73. [source,console]
  74. ----
  75. PUT /_ingest/pipeline/networks_lookup
  76. {
  77. "processors" : [
  78. {
  79. "enrich" : {
  80. "description": "Add 'network' data based on 'ip'",
  81. "policy_name": "networks-policy",
  82. "field" : "ip",
  83. "target_field": "network",
  84. "max_matches": "10"
  85. }
  86. }
  87. ]
  88. }
  89. ----
  90. // TEST[continued]
  91. Use the ingest pipeline to index a document. The incoming document should
  92. include the `field` specified in your enrich processor.
  93. [source,console]
  94. ----
  95. PUT /my-index-000001/_doc/my_id?pipeline=networks_lookup
  96. {
  97. "ip": "10.100.34.1"
  98. }
  99. ----
  100. // TEST[continued]
  101. To verify the enrich processor matched and appended the appropriate field data,
  102. use the <<docs-get,get API>> to view the indexed document.
  103. [source,console]
  104. ----
  105. GET /my-index-000001/_doc/my_id
  106. ----
  107. // TEST[continued]
  108. The API returns the following response:
  109. [source,console-result]
  110. ----
  111. {
  112. "_index" : "my-index-000001",
  113. "_id" : "my_id",
  114. "_version" : 1,
  115. "_seq_no" : 0,
  116. "_primary_term" : 1,
  117. "found" : true,
  118. "_source" : {
  119. "ip" : "10.100.34.1",
  120. "network" : [
  121. {
  122. "name" : "production",
  123. "range" : "10.100.0.0/16",
  124. "department" : "OPS"
  125. }
  126. ]
  127. }
  128. }
  129. ----
  130. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term":1/"_primary_term" : $body._primary_term/]
  131. ////
  132. [source,console]
  133. --------------------------------------------------
  134. DELETE /_ingest/pipeline/networks_lookup
  135. DELETE /_enrich/policy/networks-policy
  136. DELETE /networks
  137. DELETE /my-index-000001
  138. --------------------------------------------------
  139. // TEST[continued]
  140. ////