1010import { BasicPrettyPrinter } from '../../../pretty_print' ;
1111import * as commands from '..' ;
1212import { EsqlQuery } from '../../../query' ;
13+ import type { ESQLAstItem , ESQLCommandOption , ESQLMap } from '../../../types' ;
1314
14- /**
15- * @todo Tests skipped, while RERANK command grammar is being stabilized. We will
16- * get back to it after 9.1 release.
17- */
18- describe . skip ( 'commands.rerank' , ( ) => {
15+ describe ( 'commands.rerank' , ( ) => {
1916 describe ( '.list()' , ( ) => {
2017 it ( 'lists the only "RERANK" commands' , ( ) => {
2118 const src =
22- 'FROM index | LIMIT 1 | RERANK "star wars" ON title, overview=SUBSTRING(overview, 0, 100), actors WITH rerankerInferenceId | LIMIT 2' ;
19+ 'FROM index | LIMIT 1 | RERANK "star wars" ON title, overview=SUBSTRING(overview, 0, 100), actors WITH { "inference_id": "model_id" } | LIMIT 2' ;
2320 const query = EsqlQuery . fromSrc ( src ) ;
2421
2522 const nodes = [ ...commands . rerank . list ( query . ast ) ] ;
@@ -35,22 +32,74 @@ describe.skip('commands.rerank', () => {
3532
3633 describe ( '.setQuery()' , ( ) => {
3734 it ( 'can change query' , ( ) => {
38- const src = 'FROM index | LIMIT 1 | RERANK "star wars" ON field WITH id | LIMIT 2' ;
35+ const src =
36+ 'FROM index | RERANK "star wars" ON field WITH { "inference_id": "model_id" } | LIMIT 2' ;
3937 const query = EsqlQuery . fromSrc ( src ) ;
4038
4139 const cmd = [ ...commands . rerank . list ( query . ast ) ] [ 0 ] ;
4240 commands . rerank . setQuery ( cmd , 'new query' ) ;
4341
4442 expect ( BasicPrettyPrinter . expression ( cmd . query ) ) . toBe ( '"new query"' ) ;
45- expect ( query . print ( ) ) . toBe (
46- 'FROM index | LIMIT 1 | RERANK "new query" ON field WITH id | LIMIT 2'
43+ expect ( query . print ( { wrap : Infinity } ) ) . toBe (
44+ 'FROM index | RERANK "new query" ON field WITH {"inference_id": "model_id"} | LIMIT 2'
4745 ) ;
4846 } ) ;
4947 } ) ;
5048
49+ it ( 'can change query on a command with a target assignment' , ( ) => {
50+ const src =
51+ 'FROM index | RERANK my_target = "star wars" ON field WITH { "inference_id": "model_id" }' ;
52+ const query = EsqlQuery . fromSrc ( src ) ;
53+
54+ const cmd = [ ...commands . rerank . list ( query . ast ) ] [ 0 ] ;
55+ commands . rerank . setQuery ( cmd , 'new query' ) ;
56+
57+ expect ( BasicPrettyPrinter . expression ( cmd . query ) ) . toBe ( '"new query"' ) ;
58+ expect ( query . print ( { wrap : Infinity } ) ) . toBe (
59+ 'FROM index | RERANK my_target = "new query" ON field WITH {"inference_id": "model_id"}'
60+ ) ;
61+ } ) ;
62+ } ) ;
63+
64+ describe ( '.setTargetField()' , ( ) => {
65+ it ( 'can add a target field to a simple command' , ( ) => {
66+ const src = 'FROM index | RERANK "star wars" ON field' ;
67+ const query = EsqlQuery . fromSrc ( src ) ;
68+
69+ const cmd = [ ...commands . rerank . list ( query . ast ) ] [ 0 ] ;
70+ commands . rerank . setTargetField ( cmd , 'my_target' ) ;
71+
72+ expect ( query . print ( { wrap : Infinity } ) ) . toBe (
73+ 'FROM index | RERANK my_target = "star wars" ON field'
74+ ) ;
75+ } ) ;
76+
77+ it ( 'can change an existing target field' , ( ) => {
78+ const src = 'FROM index | RERANK old_target = "star wars" ON field' ;
79+ const query = EsqlQuery . fromSrc ( src ) ;
80+
81+ const cmd = [ ...commands . rerank . list ( query . ast ) ] [ 0 ] ;
82+ commands . rerank . setTargetField ( cmd , 'new_target' ) ;
83+
84+ expect ( query . print ( { wrap : Infinity } ) ) . toBe (
85+ 'FROM index | RERANK new_target = "star wars" ON field'
86+ ) ;
87+ } ) ;
88+
89+ it ( 'can remove an existing target field' , ( ) => {
90+ const src = 'FROM index | RERANK my_target = "star wars" ON field' ;
91+ const query = EsqlQuery . fromSrc ( src ) ;
92+
93+ const cmd = [ ...commands . rerank . list ( query . ast ) ] [ 0 ] ;
94+ commands . rerank . setTargetField ( cmd , null ) ;
95+
96+ expect ( query . print ( { wrap : Infinity } ) ) . toBe ( 'FROM index | RERANK "star wars" ON field' ) ;
97+ } ) ;
98+
5199 describe ( '.setFields()' , ( ) => {
52100 it ( 'can change query' , ( ) => {
53- const src = 'FROM index | LIMIT 1 | RERANK "star wars" ON field WITH id | LIMIT 2' ;
101+ const src =
102+ 'FROM index | RERANK "star wars" ON field WITH { "inference_id": "model_id" } | LIMIT 2' ;
54103 const query = EsqlQuery . fromSrc ( src ) ;
55104
56105 const cmd = [ ...commands . rerank . list ( query . ast ) ] [ 0 ] ;
@@ -61,23 +110,44 @@ describe.skip('commands.rerank', () => {
61110 'b' ,
62111 '@timestamp' ,
63112 ] ) ;
64- expect ( query . print ( ) ) . toBe (
65- 'FROM index | LIMIT 1 | RERANK "star wars" ON a, b, @timestamp WITH id | LIMIT 2'
113+ expect ( query . print ( { wrap : Infinity } ) ) . toBe (
114+ 'FROM index | RERANK "star wars" ON a, b, @timestamp WITH {"inference_id": "model_id"} | LIMIT 2'
115+ ) ;
116+ } ) ;
117+
118+ it ( 'should throw error when ON option is missing' , ( ) => {
119+ const src = 'FROM index | RERANK "star wars"' ;
120+ const query = EsqlQuery . fromSrc ( src ) ;
121+
122+ const cmd = [ ...commands . rerank . list ( query . ast ) ] [ 0 ] ;
123+
124+ expect ( ( ) => commands . rerank . setFields ( cmd , [ 'newField' ] ) ) . toThrow (
125+ 'RERANK command must have a ON option'
66126 ) ;
67127 } ) ;
68128 } ) ;
69129
70- describe ( '.setInferenceId()' , ( ) => {
71- it ( 'can change query' , ( ) => {
72- const src = 'FROM index | LIMIT 1 | RERANK "star wars" ON field WITH id | LIMIT 2' ;
130+ describe ( '.setWithParameter()' , ( ) => {
131+ it ( 'can add and update a new parameter to WITH map' , ( ) => {
132+ const src =
133+ 'FROM index | RERANK "star wars" ON field WITH { "inference_id": "model_id" } | LIMIT 2' ;
73134 const query = EsqlQuery . fromSrc ( src ) ;
74135
75136 const cmd = [ ...commands . rerank . list ( query . ast ) ] [ 0 ] ;
76- commands . rerank . setInferenceId ( cmd , 'new_id' ) ;
137+ commands . rerank . setWithParameter ( cmd , 'scoreColumn' , 'first_rank_score' ) ; // create
138+ commands . rerank . setWithParameter ( cmd , 'scoreColumn' , 'rank_score' ) ; // update
139+
140+ const isWithOption = ( arg : ESQLAstItem ) : arg is ESQLCommandOption =>
141+ ! ! arg && ! Array . isArray ( arg ) && arg . type === 'option' && arg . name === 'with' ;
142+
143+ const map = cmd . args . find ( isWithOption ) ! . args [ 0 ] as ESQLMap ;
144+ const scoreColumnEntry = map ?. entries ?. find (
145+ ( entry ) => entry . key . valueUnquoted === 'scoreColumn'
146+ ) ;
77147
78- expect ( BasicPrettyPrinter . expression ( cmd . inferenceId ) ) . toBe ( 'new_id ' ) ;
79- expect ( query . print ( ) ) . toBe (
80- 'FROM index | LIMIT 1 | RERANK "star wars" ON field WITH new_id | LIMIT 2'
148+ expect ( BasicPrettyPrinter . expression ( scoreColumnEntry ! . value ) ) . toBe ( '"rank_score" ' ) ;
149+ expect ( query . print ( { wrap : Infinity } ) ) . toBe (
150+ 'FROM index | RERANK "star wars" ON field WITH {"inference_id": "model_id", "scoreColumn": "rank_score"} | LIMIT 2'
81151 ) ;
82152 } ) ;
83153 } ) ;
0 commit comments