esql-lookup-join.asciidoc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. === LOOKUP JOIN
  2. ++++
  3. <titleabbrev>Correlate data with LOOKUP JOIN</titleabbrev>
  4. ++++
  5. // hack because page didn't have explicit id originally we could link to using internal link syntax
  6. [[esql-lookup-join-landing-page]]
  7. [WARNING]
  8. ====
  9. This functionality is in technical preview and may be
  10. changed or removed in a future release. Elastic will work to fix any
  11. issues, but features in technical preview are not subject to the support
  12. SLA of official GA features.
  13. ====
  14. The {esql} <<esql-lookup-join,LOOKUP join>>
  15. processing command combines data from your {esql} query results
  16. table with matching records from a specified lookup index. It adds
  17. fields from the lookup index as new columns to your results table based
  18. on matching values in the join field.
  19. Teams often have data scattered across multiple indices – like logs,
  20. IPs, user IDs, hosts, employees etc. Without a direct way to enrich or
  21. correlate each event with reference data, root-cause analysis, security
  22. checks, and operational insights become time-consuming.
  23. For example, you can use `LOOKUP JOIN` to:
  24. * Retrieve environment or ownership details for each host to correlate
  25. your metrics data.
  26. * Quickly see if any source IPs match known malicious addresses.
  27. * Tag logs with the owning team or escalation info for faster triage and
  28. incident response.
  29. [discrete]
  30. [[esql-compare-with-enrich]]
  31. ==== Compare with ENRICH
  32. <<esql-lookup-join,LOOKUP join>> is similar to <<esql-enrich-data,ENRICH>>
  33. in the fact that they both help you join data together. You should use
  34. `LOOKUP JOIN` when:
  35. * Your enrichment data changes frequently
  36. * You want to avoid index-time processing
  37. * You want SQL-like behavior, so that multiple matches result in multiple rows
  38. * You need to match on any field in a lookup index
  39. * You use document or field level security
  40. * You want to restrict users to use only specific lookup indices
  41. * You do not need to match using ranges or spatial relations
  42. [discrete]
  43. [[esql-how-lookup-join-works]]
  44. ==== How the command works
  45. The `LOOKUP JOIN` command adds fields from the lookup index as new columns
  46. to your results table based on matching values in the join field.
  47. [source,esql]
  48. ----
  49. LOOKUP JOIN <lookup_index> ON <field_name>
  50. ----
  51. The command requires two parameters:
  52. [[esql-lookup-join-lookup-index]]
  53. lookup_index::
  54. The name of the lookup index. This must
  55. be a specific index name - wildcards, aliases, and remote cluster
  56. references are not supported. Indices used for lookups must be configured with the <<index-mode-setting,`lookup` mode>>.
  57. [[esql-lookup-join-field-name]]
  58. field_name::
  59. The field to join on. This field must exist
  60. in both your current query results and in the lookup index. If the field
  61. contains multi-valued entries, those entries will not match anything
  62. (the added fields will contain `null` for those rows).
  63. image::images/esql/esql-lookup-join.png[align="center"]
  64. If you're familiar with SQL, `LOOKUP JOIN` has left-join behavior. This means that
  65. if no rows match in the lookup index, the incoming row is retained and `null` values are added. If many rows in the lookup index match, `LOOKUP JOIN` adds one row per match.
  66. [discrete]
  67. [[esql-lookup-join-example]]
  68. ==== Example
  69. You can run this example for yourself to see how it works by setting up the indices and adding sample data. Otherwise, you just inspect the query and response.
  70. [discrete]
  71. [[esql-lookup-join-example-setup-sample-data]]
  72. ===== Sample data
  73. .*Expand for setup instructions*
  74. [%collapsible]
  75. ==============
  76. **Set up indices**
  77. First, let's create two indices with mappings: `threat_list` and `firewall_logs`.
  78. [source,console]
  79. ----
  80. PUT threat_list
  81. {
  82. "settings": {
  83. "index.mode": "lookup" <1>
  84. },
  85. "mappings": {
  86. "properties": {
  87. "source.ip": { "type": "ip" },
  88. "threat_level": { "type": "keyword" },
  89. "threat_type": { "type": "keyword" },
  90. "last_updated": { "type": "date" }
  91. }
  92. }
  93. }
  94. ----
  95. <1> The lookup index must be set up using this mode
  96. [source,console]
  97. ----
  98. PUT firewall_logs
  99. {
  100. "mappings": {
  101. "properties": {
  102. "timestamp": { "type": "date" },
  103. "source.ip": { "type": "ip" },
  104. "destination.ip": { "type": "ip" },
  105. "action": { "type": "keyword" },
  106. "bytes_transferred": { "type": "long" }
  107. }
  108. }
  109. }
  110. ----
  111. *Add sample data*
  112. Next, let's add some sample data to both indices. The `threat_list` index contains known malicious IPs, while the `firewall_logs` index contains logs of network traffic.
  113. [source,console]
  114. ----
  115. POST threat_list/_bulk
  116. {"index":{}}
  117. {"source.ip":"203.0.113.5","threat_level":"high","threat_type":"C2_SERVER","last_updated":"2025-04-22"}
  118. {"index":{}}
  119. {"source.ip":"198.51.100.2","threat_level":"medium","threat_type":"SCANNER","last_updated":"2025-04-23"}
  120. ----
  121. [source,console]
  122. ----
  123. POST firewall_logs/_bulk
  124. {"index":{}}
  125. {"timestamp":"2025-04-23T10:00:01Z","source.ip":"192.0.2.1","destination.ip":"10.0.0.100","action":"allow","bytes_transferred":1024}
  126. {"index":{}}
  127. {"timestamp":"2025-04-23T10:00:05Z","source.ip":"203.0.113.5","destination.ip":"10.0.0.55","action":"allow","bytes_transferred":2048}
  128. {"index":{}}
  129. {"timestamp":"2025-04-23T10:00:08Z","source.ip":"198.51.100.2","destination.ip":"10.0.0.200","action":"block","bytes_transferred":0}
  130. {"index":{}}
  131. {"timestamp":"2025-04-23T10:00:15Z","source.ip":"203.0.113.5","destination.ip":"10.0.0.44","action":"allow","bytes_transferred":4096}
  132. {"index":{}}
  133. {"timestamp":"2025-04-23T10:00:30Z","source.ip":"192.0.2.1","destination.ip":"10.0.0.100","action":"allow","bytes_transferred":512}
  134. ----
  135. ==============
  136. [discrete]
  137. [[esql-lookup-join-example-query]]
  138. ===== Query the Data
  139. [source,esql]
  140. ----
  141. FROM firewall_logs <1>
  142. | LOOKUP JOIN threat_list ON source.ip <2>
  143. | WHERE threat_level IS NOT NULL <3>
  144. | SORT timestamp <4>
  145. | KEEP source.ip, action, threat_level, threat_type <5>
  146. | LIMIT 10 <6>
  147. ----
  148. <1> The source index
  149. <2> The lookup index and join field
  150. <3> Filter for rows with non-null threat levels
  151. <4> LOOKUP JOIN does not guarantee output order, so you must explicitly sort
  152. <5> Keep only relevant fields
  153. <6> Limit the output to 10 rows
  154. [discrete]
  155. [[esql-lookup-join-example-response]]
  156. ===== Response
  157. A successful query will output a table like this:
  158. [cols="4*",options="header"]
  159. |===
  160. |source.ip |action |threat_type |threat_level
  161. |203.0.113.5 |allow |C2_SERVER |high
  162. |198.51.100.2 |block |SCANNER |medium
  163. |203.0.113.5 |allow |C2_SERVER |high
  164. |===
  165. In this example, you can see that the `source.ip` field from the `firewall_logs` index is matched with the `source.ip` field in the `threat_list` index, and the corresponding `threat_level` and `threat_type` fields are added to the output.
  166. [discrete]
  167. [[esql-lookup-join-additional-examples]]
  168. ===== Additional examples
  169. Refer to the examples section of the <<esql-lookup-join,LOOKUP JOIN>> command reference for more examples.
  170. [discrete]
  171. [[esql-lookup-join-prereqs]]
  172. ==== Prerequisites
  173. To use `LOOKUP JOIN`, the following requirements must be met:
  174. * Indices used for lookups must be configured with the <<index-mode-setting,`lookup` mode>>
  175. * *Compatible data types*: The join key and join field in the lookup
  176. index must have compatible data types. This means:
  177. ** The data types must either be identical or be internally represented
  178. as the same type in {esql}
  179. ** Numeric types follow these compatibility rules:
  180. *** `short` and `byte` are compatible with `integer` (all represented as
  181. `int`)
  182. *** `float`, `half_float`, and `scaled_float` are compatible
  183. with `double` (all represented as `double`)
  184. ** For text fields: You can only use text fields as the join key on the
  185. left-hand side of the join and only if they have a `.keyword` subfield
  186. To obtain a join key with a compatible type, use a
  187. <<esql-type-conversion-functions,conversion function>> if needed.
  188. For a complete list of supported data types and their internal
  189. representations, see the <<esql-supported-types,Supported Field Types documentation>>.
  190. [discrete]
  191. [[esql-lookup-join-usage-notes]]
  192. ==== Usage notes
  193. This section covers important details about `LOOKUP JOIN` that impact query behavior and results. Review these details to ensure your queries work as expected and to troubleshoot unexpected results.
  194. [discrete]
  195. [[esql-lookup-join-usage-notes-name-collisions]]
  196. ===== Handling name collisions
  197. When fields from the lookup index match existing column names, the new columns override the existing ones.
  198. Before the `LOOKUP JOIN` command, preserve columns by either:
  199. * Using `RENAME` to assign non-conflicting names
  200. * Using `EVAL` to create new columns with different names
  201. [discrete]
  202. [[esql-lookup-join-usage-notes-sorting]]
  203. ===== Sorting behavior
  204. The output rows produced by `LOOKUP JOIN` can be in any order and may not
  205. respect preceding `SORT` commands. To guarantee a certain ordering, place a `SORT` after any `LOOKUP JOIN` commands.
  206. [discrete]
  207. [[esql-lookup-join-limitations]]
  208. ==== Limitations
  209. The following are the current limitations with `LOOKUP JOIN`
  210. * Indices in <<index-mode-setting,`lookup`>> mode are always single-sharded.
  211. * Cross cluster search is unsupported initially. Both source and lookup indices
  212. must be local.
  213. * Currently, only matching on equality is supported.
  214. * `LOOKUP JOIN` can only use a single match field and a single index.
  215. Wildcards, aliases, datemath, and datastreams are not supported.
  216. * The name of the match field in
  217. `LOOKUP JOIN lu++_++idx ON match++_++field` must match an existing field
  218. in the query. This may require renames or evals to achieve.
  219. * The query will circuit break if there are too many matching documents
  220. in the lookup index, or if the documents are too large. More precisely,
  221. `LOOKUP JOIN` works in batches of, normally, about 10,000 rows; a large
  222. amount of heap space is needed if the matching documents from the lookup
  223. index for a batch are multiple megabytes or larger. This is roughly the
  224. same as for `ENRICH`.