elasticsearch/docs/reference/esql/processing-commands/lookup.asciidoc
Nik Everett b5c6c2da30
ESQL: INLINESTATS (#109583)
This implements `INLINESTATS`. Most of the heavy lifting is done by
`LOOKUP`, with this change mostly adding a new abstraction to logical
plans, and interface I'm calling `Phased`. Implementing this interface
allows a logical plan node to cut the query into phases. `INLINESTATS`
implements it by asking for a "first phase" that's the same query, up to
`INLINESTATS`, but with `INLINESTATS` replaced with `STATS`. The next
phase replaces the `INLINESTATS` with a `LOOKUP` on the results of the
first phase.

So, this query:
```
FROM foo
| EVAL bar = a * b
| INLINESTATS m = MAX(bar) BY b
| WHERE m = bar
| LIMIT 1
```

gets split into
```
FROM foo
| EVAL bar = a * b
| STATS m = MAX(bar) BY b
```

followed by
```
FROM foo
| EVAL bar = a * b
| LOOKUP (results of m = MAX(bar) BY b) ON b
| WHERE m = bar
| LIMIT 1
```
2024-07-24 17:16:37 -04:00

64 lines
1.6 KiB
Text

[discrete]
[[esql-lookup]]
=== `LOOKUP`
experimental::["LOOKUP is highly experimental and only available in SNAPSHOT versions."]
`LOOKUP` matches values from the input against a `table` provided in the request,
adding the other fields from the `table` to the output.
**Syntax**
[source,esql]
----
LOOKUP table ON match_field1[, match_field2, ...]
----
*Parameters*
`table`::
The name of the `table` provided in the request to match.
If the table's column names conflict with existing columns, the existing columns will be dropped.
`match_field`::
The fields in the input to match against the table.
*Examples*
// tag::examples[]
[source,console,id=esql-lookup-example]
----
POST /_query?format=txt
{
"query": """
FROM library
| SORT page_count DESC
| KEEP name, author
| LOOKUP era ON author
| LIMIT 5
""",
"tables": {
"era": {
"author": {"keyword": ["Frank Herbert", "Peter F. Hamilton", "Vernor Vinge", "Alastair Reynolds", "James S.A. Corey"]},
"era": {"keyword": [ "The New Wave", "Diamond", "Diamond", "Diamond", "Hadron"]}
}
}
}
----
// TEST[setup:library]
Which returns:
[source,text]
----
name | author | era
--------------------+-----------------+---------------
Pandora's Star |Peter F. Hamilton|Diamond
A Fire Upon the Deep|Vernor Vinge |Diamond
Dune |Frank Herbert |The New Wave
Revelation Space |Alastair Reynolds|Diamond
Leviathan Wakes |James S.A. Corey |Hadron
----
// TESTRESPONSE[s/\|/\\|/ s/\+/\\+/]
// TESTRESPONSE[non_json]
// end::examples[]