Skip to content

Commit d56a476

Browse files
authored
[ES|QL] Implement new syntax for RERANK and COMPLETION (elastic#129716)
1 parent c1d7e79 commit d56a476

File tree

24 files changed

+1533
-1767
lines changed

24 files changed

+1533
-1767
lines changed

docs/reference/query-languages/esql/_snippets/commands/layout/completion.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,26 @@ The `COMPLETION` command allows you to send prompts and context to a Large Langu
99

1010
**Syntax**
1111

12+
::::{tab-set}
13+
14+
:::{tab-item} 9.2.0+
15+
1216
```esql
13-
COMPLETION [column =] prompt WITH inference_id
17+
COMPLETION [column =] prompt WITH { "inference_id" : "my_inference_endpoint" }
1418
```
1519

20+
:::
21+
22+
:::{tab-item} 9.1.x only
23+
24+
```esql
25+
COMPLETION [column =] prompt WITH my_inference_endpoint
26+
```
27+
28+
:::
29+
30+
::::
31+
1632
**Parameters**
1733

1834
`column`
@@ -24,7 +40,7 @@ COMPLETION [column =] prompt WITH inference_id
2440
: The input text or expression used to prompt the LLM.
2541
This can be a string literal or a reference to a column containing text.
2642

27-
`inference_id`
43+
`my_inference_endpoint`
2844
: The ID of the [inference endpoint](docs-content://explore-analyze/elastic-inference/inference-api.md) to use for the task.
2945
The inference endpoint must be configured with the `completion` task type.
3046

@@ -75,7 +91,7 @@ How you increase the timeout depends on your deployment type:
7591
If you don't want to increase the timeout limit, try the following:
7692

7793
* Reduce data volume with `LIMIT` or more selective filters before the `COMPLETION` command
78-
* Split complex operations into multiple simpler queries
94+
* Split complex operations into multiple simpler queries
7995
* Configure your HTTP client's response timeout (Refer to [HTTP client configuration](/reference/elasticsearch/configuration-reference/networking-settings.md#_http_client_configuration))
8096

8197

@@ -85,7 +101,7 @@ Use the default column name (results stored in `completion` column):
85101

86102
```esql
87103
ROW question = "What is Elasticsearch?"
88-
| COMPLETION question WITH test_completion_model
104+
| COMPLETION question WITH { "inference_id" : "my_inference_endpoint" }
89105
| KEEP question, completion
90106
```
91107

@@ -97,7 +113,7 @@ Specify the output column (results stored in `answer` column):
97113

98114
```esql
99115
ROW question = "What is Elasticsearch?"
100-
| COMPLETION answer = question WITH test_completion_model
116+
| COMPLETION answer = question WITH { "inference_id" : "my_inference_endpoint" }
101117
| KEEP question, answer
102118
```
103119

@@ -117,7 +133,7 @@ FROM movies
117133
"Synopsis: ", synopsis, "\n",
118134
"Actors: ", MV_CONCAT(actors, ", "), "\n",
119135
)
120-
| COMPLETION summary = prompt WITH test_completion_model
136+
| COMPLETION summary = prompt WITH { "inference_id" : "my_inference_endpoint" }
121137
| KEEP title, summary, rating
122138
```
123139

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/MapExpression.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,18 @@ public Expression get(Object key) {
120120
return map.get(key);
121121
} else {
122122
// the key(literal) could be converted to BytesRef by ConvertStringToByteRef
123-
return keyFoldedMap.containsKey(key) ? keyFoldedMap.get(key) : keyFoldedMap.get(new BytesRef(key.toString()));
123+
return keyFoldedMap.containsKey(key) ? keyFoldedMap.get(key) : keyFoldedMap.get(foldKey(key));
124124
}
125125
}
126126

127+
public boolean containsKey(Object key) {
128+
return keyFoldedMap.containsKey(key) || keyFoldedMap.containsKey(foldKey(key));
129+
}
130+
131+
private BytesRef foldKey(Object key) {
132+
return new BytesRef(key.toString());
133+
}
134+
127135
@Override
128136
public boolean equals(Object obj) {
129137
if (this == obj) {

x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/RestRerankTestCase.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void testRerankWithSingleField() throws IOException {
9393
String query = """
9494
FROM rerank-test-index
9595
| WHERE match(title, "exploration")
96-
| RERANK "exploration" ON title WITH test_reranker
96+
| RERANK "exploration" ON title WITH { "inference_id" : "test_reranker" }
9797
| EVAL _score = ROUND(_score, 5)
9898
""";
9999

@@ -112,7 +112,7 @@ public void testRerankWithMultipleFields() throws IOException {
112112
String query = """
113113
FROM rerank-test-index
114114
| WHERE match(title, "exploration")
115-
| RERANK "exploration" ON title, author WITH test_reranker
115+
| RERANK "exploration" ON title, author WITH { "inference_id" : "test_reranker" }
116116
| EVAL _score = ROUND(_score, 5)
117117
""";
118118

@@ -131,7 +131,7 @@ public void testRerankWithPositionalParams() throws IOException {
131131
String query = """
132132
FROM rerank-test-index
133133
| WHERE match(title, "exploration")
134-
| RERANK ? ON title WITH ?
134+
| RERANK ? ON title WITH { "inference_id" : ? }
135135
| EVAL _score = ROUND(_score, 5)
136136
""";
137137

@@ -150,7 +150,7 @@ public void testRerankWithNamedParams() throws IOException {
150150
String query = """
151151
FROM rerank-test-index
152152
| WHERE match(title, ?queryText)
153-
| RERANK ?queryText ON title WITH ?inferenceId
153+
| RERANK ?queryText ON title WITH { "inference_id" : ?inferenceId }
154154
| EVAL _score = ROUND(_score, 5)
155155
""";
156156

@@ -169,7 +169,7 @@ public void testRerankWithMissingInferenceId() {
169169
String query = """
170170
FROM rerank-test-index
171171
| WHERE match(title, "exploration")
172-
| RERANK "exploration" ON title WITH test_missing
172+
| RERANK "exploration" ON title WITH { "inference_id" : "test_missing" }
173173
| EVAL _score = ROUND(_score, 5)
174174
""";
175175

x-pack/plugin/esql/qa/testFixtures/src/main/resources/completion.csv-spec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ completion using a ROW source operator
66
required_capability: completion
77

88
ROW prompt="Who is Victor Hugo?"
9-
| COMPLETION completion_output = prompt WITH test_completion
9+
| COMPLETION completion_output = prompt WITH { "inference_id" : "test_completion" }
1010
;
1111

1212
prompt:keyword | completion_output:keyword
@@ -18,7 +18,7 @@ completion using a ROW source operator and prompt is a multi-valued field
1818
required_capability: completion
1919

2020
ROW prompt=["Answer the following question:", "Who is Victor Hugo?"]
21-
| COMPLETION completion_output = prompt WITH test_completion
21+
| COMPLETION completion_output = prompt WITH { "inference_id" : "test_completion" }
2222
;
2323

2424
prompt:keyword | completion_output:keyword
@@ -34,7 +34,7 @@ FROM books METADATA _score
3434
| WHERE title:"war and peace" AND author:"Tolstoy"
3535
| SORT _score DESC
3636
| LIMIT 2
37-
| COMPLETION title WITH test_completion
37+
| COMPLETION title WITH { "inference_id" : "test_completion" }
3838
| KEEP title, completion
3939
;
4040

@@ -51,7 +51,7 @@ FROM books METADATA _score
5151
| WHERE title:"war and peace" AND author:"Tolstoy"
5252
| SORT _score DESC
5353
| LIMIT 2
54-
| COMPLETION CONCAT("This is a prompt: ", title) WITH test_completion
54+
| COMPLETION CONCAT("This is a prompt: ", title) WITH { "inference_id" : "test_completion" }
5555
| KEEP title, completion
5656
;
5757

x-pack/plugin/esql/qa/testFixtures/src/main/resources/fork.csv-spec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ FROM employees
809809
| KEEP emp_no, first_name, last_name
810810
| FORK (WHERE emp_no == 10048 OR emp_no == 10081)
811811
(WHERE emp_no == 10081 OR emp_no == 10087)
812-
| COMPLETION x = CONCAT(first_name, " ", last_name) WITH test_completion
812+
| COMPLETION x=CONCAT(first_name, " ", last_name) WITH { "inference_id" : "test_completion" }
813813
| SORT _fork, emp_no
814814
;
815815

@@ -827,7 +827,7 @@ required_capability: completion
827827
FROM employees
828828
| KEEP emp_no, first_name, last_name
829829
| FORK (WHERE emp_no == 10048 OR emp_no == 10081
830-
| COMPLETION x = CONCAT(first_name, " ", last_name) WITH test_completion)
830+
| COMPLETION x=CONCAT(first_name, " ", last_name) WITH { "inference_id" : "test_completion" })
831831
(WHERE emp_no == 10081 OR emp_no == 10087)
832832
| SORT _fork, emp_no
833833
;
@@ -845,7 +845,7 @@ required_capability: completion
845845

846846
FROM employees
847847
| KEEP emp_no, first_name, last_name
848-
| COMPLETION x = CONCAT(first_name, " ", last_name) WITH test_completion
848+
| COMPLETION x=CONCAT(first_name, " ", last_name) WITH { "inference_id" : "test_completion" }
849849
| FORK (WHERE emp_no == 10048 OR emp_no == 10081)
850850
(WHERE emp_no == 10081 OR emp_no == 10087)
851851
| SORT _fork, emp_no

x-pack/plugin/esql/qa/testFixtures/src/main/resources/rerank.csv-spec

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ required_capability: match_operator_colon
1010
FROM books METADATA _score
1111
| WHERE title:"war and peace" AND author:"Tolstoy"
1212
| SORT _score DESC, book_no ASC
13-
| RERANK "war and peace" ON title WITH inferenceId=test_reranker
13+
| RERANK "war and peace" ON title WITH { "inference_id" : "test_reranker" }
1414
| EVAL _score=ROUND(_score, 2)
1515
| KEEP book_no, title, author, _score
1616
;
@@ -29,7 +29,7 @@ required_capability: match_operator_colon
2929
FROM books METADATA _score
3030
| WHERE title:"war and peace" AND author:"Tolstoy"
3131
| SORT _score DESC, book_no ASC
32-
| RERANK "war and peace" ON title WITH inferenceId=test_reranker, scoreColumn=rerank_score
32+
| RERANK rerank_score="war and peace" ON title WITH { "inference_id" : "test_reranker" }
3333
| EVAL _score=ROUND(_score, 2), rerank_score=ROUND(rerank_score, 2)
3434
| KEEP book_no, title, author, rerank_score
3535
;
@@ -48,7 +48,7 @@ required_capability: match_operator_colon
4848
FROM books METADATA _score
4949
| WHERE title:"war and peace" AND author:"Tolstoy"
5050
| SORT _score DESC
51-
| RERANK "war and peace" ON title WITH inferenceId=test_reranker, scoreColumn=rerank_score
51+
| RERANK rerank_score="war and peace" ON title WITH { "inference_id" : "test_reranker" }
5252
| EVAL _score=ROUND(_score, 2), rerank_score=ROUND(rerank_score, 2)
5353
| SORT rerank_score, _score ASC, book_no ASC
5454
| KEEP book_no, title, author, rerank_score
@@ -68,7 +68,7 @@ required_capability: match_operator_colon
6868

6969
FROM books METADATA _score
7070
| WHERE title:"war and peace" AND author:"Tolstoy"
71-
| RERANK "war and peace" ON title, author WITH inferenceId=test_reranker
71+
| RERANK "war and peace" ON title, author WITH { "inference_id" : "test_reranker" }
7272
| EVAL _score=ROUND(_score, 2)
7373
| SORT _score DESC, book_no ASC
7474
| KEEP book_no, title, author, _score
@@ -90,7 +90,7 @@ FROM books METADATA _score
9090
| WHERE title:"war and peace" AND author:"Tolstoy"
9191
| SORT _score DESC, book_no ASC
9292
| LIMIT 3
93-
| RERANK "war and peace" ON title WITH inferenceId=test_reranker
93+
| RERANK "war and peace" ON title WITH { "inference_id" : "test_reranker" }
9494
| EVAL _score=ROUND(_score, 2)
9595
| SORT _score DESC, book_no ASC
9696
| KEEP book_no, title, author, _score
@@ -109,7 +109,7 @@ required_capability: match_operator_colon
109109

110110
FROM books METADATA _score
111111
| WHERE title:"war and peace" AND author:"Tolstoy"
112-
| RERANK "war and peace" ON title WITH inferenceId=test_reranker
112+
| RERANK "war and peace" ON title WITH { "inference_id" : "test_reranker" }
113113
| EVAL _score=ROUND(_score, 2)
114114
| SORT _score DESC, book_no ASC
115115
| KEEP book_no, title, author, _score
@@ -129,7 +129,7 @@ required_capability: match_operator_colon
129129

130130
FROM books
131131
| WHERE title:"war and peace" AND author:"Tolstoy"
132-
| RERANK "war and peace" ON title WITH inferenceId=test_reranker
132+
| RERANK "war and peace" ON title WITH { "inference_id" : "test_reranker" }
133133
| EVAL _score=ROUND(_score, 2)
134134
| KEEP book_no, title, author, _score
135135
| SORT author, title
@@ -153,7 +153,7 @@ FROM books METADATA _id, _index, _score
153153
| FORK ( WHERE title:"Tolkien" | SORT _score, _id DESC | LIMIT 3 )
154154
( WHERE author:"Tolkien" | SORT _score, _id DESC | LIMIT 3 )
155155
| FUSE
156-
| RERANK "Tolkien" ON title WITH inferenceId=test_reranker
156+
| RERANK "Tolkien" ON title WITH { "inference_id" : "test_reranker" }
157157
| EVAL _score=ROUND(_score, 2)
158158
| SORT _score DESC, book_no ASC
159159
| LIMIT 2

x-pack/plugin/esql/src/main/antlr/EsqlBaseParser.g4

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -219,23 +219,28 @@ renameClause:
219219
;
220220

221221
dissectCommand
222-
: DISSECT primaryExpression string commandOptions?
222+
: DISSECT primaryExpression string dissectCommandOptions?
223223
;
224224

225-
grokCommand
226-
: GROK primaryExpression string
225+
dissectCommandOptions
226+
: dissectCommandOption (COMMA dissectCommandOption)*
227227
;
228228

229-
mvExpandCommand
230-
: MV_EXPAND qualifiedName
229+
dissectCommandOption
230+
: identifier ASSIGN constant
231231
;
232232

233-
commandOptions
234-
: commandOption (COMMA commandOption)*
233+
234+
commandNamedParameters
235+
: (WITH mapExpression)?
235236
;
236237

237-
commandOption
238-
: identifier ASSIGN constant
238+
grokCommand
239+
: GROK primaryExpression string
240+
;
241+
242+
mvExpandCommand
243+
: MV_EXPAND qualifiedName
239244
;
240245

241246
explainCommand
@@ -293,7 +298,7 @@ forkSubQueryProcessingCommand
293298
;
294299

295300
completionCommand
296-
: COMPLETION (targetField=qualifiedName ASSIGN)? prompt=primaryExpression WITH inferenceId=identifierOrParameter
301+
: COMPLETION (targetField=qualifiedName ASSIGN)? prompt=primaryExpression commandNamedParameters
297302
;
298303

299304
//
@@ -315,19 +320,6 @@ fuseCommand
315320
: DEV_FUSE
316321
;
317322

318-
inferenceCommandOptions
319-
: inferenceCommandOption (COMMA inferenceCommandOption)*
320-
;
321-
322-
inferenceCommandOption
323-
: identifier ASSIGN inferenceCommandOptionValue
324-
;
325-
326-
inferenceCommandOptionValue
327-
: constant
328-
| identifier
329-
;
330-
331323
rerankCommand
332-
: DEV_RERANK queryText=constant ON rerankFields (WITH inferenceCommandOptions)?
324+
: DEV_RERANK (targetField=qualifiedName ASSIGN)? queryText=constant ON rerankFields commandNamedParameters
333325
;

x-pack/plugin/esql/src/main/antlr/parser/Expression.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ functionName
5757
;
5858

5959
mapExpression
60-
: LEFT_BRACES entryExpression (COMMA entryExpression)* RIGHT_BRACES
60+
: LEFT_BRACES (entryExpression (COMMA entryExpression)*)? RIGHT_BRACES
6161
;
6262

6363
entryExpression

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/analysis/Analyzer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ private BitSet gatherPreAnalysisMetrics(LogicalPlan plan, BitSet b) {
14051405
private static class ImplicitCasting extends ParameterizedRule<LogicalPlan, LogicalPlan, AnalyzerContext> {
14061406
@Override
14071407
public LogicalPlan apply(LogicalPlan plan, AnalyzerContext context) {
1408-
// do implicit casting for function arguments
1408+
// do implicit casting for named parameters
14091409
return plan.transformExpressionsUp(
14101410
org.elasticsearch.xpack.esql.core.expression.function.Function.class,
14111411
e -> ImplicitCasting.cast(e, context.functionRegistry().snapshotRegistry())

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/EsqlBaseParser.interp

Lines changed: 4 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)