30
30
import org .junit .ClassRule ;
31
31
32
32
import java .io .IOException ;
33
+ import java .util .Arrays ;
33
34
import java .util .List ;
34
35
import java .util .Locale ;
35
36
import java .util .Map ;
@@ -87,9 +88,11 @@ private void indexDocument(String index, int id, double value, String org) throw
87
88
88
89
@ Before
89
90
public void indexDocuments () throws IOException {
91
+ Settings lookupSettings = Settings .builder ().put ("index.mode" , "lookup" ).build ();
90
92
String mapping = """
91
93
"properties":{"value": {"type": "double"}, "org": {"type": "keyword"}}
92
94
""" ;
95
+
93
96
createIndex ("index" , Settings .EMPTY , mapping );
94
97
indexDocument ("index" , 1 , 10.0 , "sales" );
95
98
indexDocument ("index" , 2 , 20.0 , "engineering" );
@@ -110,6 +113,16 @@ public void indexDocuments() throws IOException {
110
113
indexDocument ("indexpartial" , 2 , 40.0 , "sales" );
111
114
refresh ("indexpartial" );
112
115
116
+ createIndex ("lookup-user1" , lookupSettings , mapping );
117
+ indexDocument ("lookup-user1" , 1 , 12.0 , "engineering" );
118
+ indexDocument ("lookup-user1" , 2 , 31.0 , "sales" );
119
+ refresh ("lookup-user1" );
120
+
121
+ createIndex ("lookup-user2" , lookupSettings , mapping );
122
+ indexDocument ("lookup-user2" , 1 , 32.0 , "marketing" );
123
+ indexDocument ("lookup-user2" , 2 , 40.0 , "sales" );
124
+ refresh ("lookup-user2" );
125
+
113
126
if (aliasExists ("second-alias" ) == false ) {
114
127
Request aliasRequest = new Request ("POST" , "_aliases" );
115
128
aliasRequest .setJsonEntity ("""
@@ -126,6 +139,17 @@ public void indexDocuments() throws IOException {
126
139
}
127
140
}
128
141
},
142
+ {
143
+ "add": {
144
+ "alias": "lookup-first-alias",
145
+ "index": "lookup-user1",
146
+ "filter": {
147
+ "term": {
148
+ "org": "sales"
149
+ }
150
+ }
151
+ }
152
+ },
129
153
{
130
154
"add": {
131
155
"alias": "second-alias",
@@ -229,22 +253,30 @@ public void testAliasFilter() throws Exception {
229
253
public void testUnauthorizedIndices () throws IOException {
230
254
ResponseException error ;
231
255
error = expectThrows (ResponseException .class , () -> runESQLCommand ("user1" , "from index-user2 | stats sum(value)" ));
256
+ assertThat (error .getMessage (), containsString ("Unknown index [index-user2]" ));
232
257
assertThat (error .getResponse ().getStatusLine ().getStatusCode (), equalTo (400 ));
233
258
234
259
error = expectThrows (ResponseException .class , () -> runESQLCommand ("user2" , "from index-user1 | stats sum(value)" ));
260
+ assertThat (error .getMessage (), containsString ("Unknown index [index-user1]" ));
235
261
assertThat (error .getResponse ().getStatusLine ().getStatusCode (), equalTo (400 ));
236
262
237
263
error = expectThrows (ResponseException .class , () -> runESQLCommand ("alias_user2" , "from index-user2 | stats sum(value)" ));
264
+ assertThat (error .getMessage (), containsString ("Unknown index [index-user2]" ));
238
265
assertThat (error .getResponse ().getStatusLine ().getStatusCode (), equalTo (400 ));
239
266
240
267
error = expectThrows (ResponseException .class , () -> runESQLCommand ("metadata1_read2" , "from index-user1 | stats sum(value)" ));
268
+ assertThat (error .getMessage (), containsString ("Unknown index [index-user1]" ));
241
269
assertThat (error .getResponse ().getStatusLine ().getStatusCode (), equalTo (400 ));
242
270
}
243
271
244
272
public void testInsufficientPrivilege () {
245
- Exception error = expectThrows (Exception .class , () -> runESQLCommand ("metadata1_read2" , "FROM index-user1 | STATS sum=sum(value)" ));
273
+ ResponseException error = expectThrows (
274
+ ResponseException .class ,
275
+ () -> runESQLCommand ("metadata1_read2" , "FROM index-user1 | STATS sum=sum(value)" )
276
+ );
246
277
logger .info ("error" , error );
247
278
assertThat (error .getMessage (), containsString ("Unknown index [index-user1]" ));
279
+ assertThat (error .getResponse ().getStatusLine ().getStatusCode (), equalTo (HttpStatus .SC_BAD_REQUEST ));
248
280
}
249
281
250
282
public void testIndexPatternErrorMessageComparison_ESQL_SearchDSL () throws Exception {
@@ -511,6 +543,63 @@ record Listen(long timestamp, String songId, double duration) {
511
543
}
512
544
}
513
545
546
+ public void testLookupJoinIndexAllowed () throws Exception {
547
+ Response resp = runESQLCommand (
548
+ "metadata1_read2" ,
549
+ "ROW x = 40.0 | EVAL value = x | LOOKUP JOIN `lookup-user2` ON value | KEEP x, org"
550
+ );
551
+ assertOK (resp );
552
+ Map <String , Object > respMap = entityAsMap (resp );
553
+ assertThat (
554
+ respMap .get ("columns" ),
555
+ equalTo (List .of (Map .of ("name" , "x" , "type" , "double" ), Map .of ("name" , "org" , "type" , "keyword" )))
556
+ );
557
+ assertThat (respMap .get ("values" ), equalTo (List .of (List .of (40.0 , "sales" ))));
558
+
559
+ // Alias, should find the index and the row
560
+ resp = runESQLCommand ("alias_user1" , "ROW x = 31.0 | EVAL value = x | LOOKUP JOIN `lookup-first-alias` ON value | KEEP x, org" );
561
+ assertOK (resp );
562
+ respMap = entityAsMap (resp );
563
+ assertThat (
564
+ respMap .get ("columns" ),
565
+ equalTo (List .of (Map .of ("name" , "x" , "type" , "double" ), Map .of ("name" , "org" , "type" , "keyword" )))
566
+ );
567
+ assertThat (respMap .get ("values" ), equalTo (List .of (List .of (31.0 , "sales" ))));
568
+
569
+ // Alias, for a row that's filtered out
570
+ resp = runESQLCommand ("alias_user1" , "ROW x = 123.0 | EVAL value = x | LOOKUP JOIN `lookup-first-alias` ON value | KEEP x, org" );
571
+ assertOK (resp );
572
+ respMap = entityAsMap (resp );
573
+ assertThat (
574
+ respMap .get ("columns" ),
575
+ equalTo (List .of (Map .of ("name" , "x" , "type" , "double" ), Map .of ("name" , "org" , "type" , "keyword" )))
576
+ );
577
+ assertThat (respMap .get ("values" ), equalTo (List .of (Arrays .asList (123.0 , null ))));
578
+ }
579
+
580
+ public void testLookupJoinIndexForbidden () {
581
+ var resp = expectThrows (
582
+ ResponseException .class ,
583
+ () -> runESQLCommand ("metadata1_read2" , "FROM lookup-user2 | EVAL value = 10.0 | LOOKUP JOIN `lookup-user1` ON value | KEEP x" )
584
+ );
585
+ assertThat (resp .getMessage (), containsString ("Unknown index [lookup-user1]" ));
586
+ assertThat (resp .getResponse ().getStatusLine ().getStatusCode (), equalTo (HttpStatus .SC_BAD_REQUEST ));
587
+
588
+ resp = expectThrows (
589
+ ResponseException .class ,
590
+ () -> runESQLCommand ("metadata1_read2" , "ROW x = 10.0 | EVAL value = x | LOOKUP JOIN `lookup-user1` ON value | KEEP x" )
591
+ );
592
+ assertThat (resp .getMessage (), containsString ("Unknown index [lookup-user1]" ));
593
+ assertThat (resp .getResponse ().getStatusLine ().getStatusCode (), equalTo (HttpStatus .SC_BAD_REQUEST ));
594
+
595
+ resp = expectThrows (
596
+ ResponseException .class ,
597
+ () -> runESQLCommand ("alias_user1" , "ROW x = 10.0 | EVAL value = x | LOOKUP JOIN `lookup-user1` ON value | KEEP x" )
598
+ );
599
+ assertThat (resp .getMessage (), containsString ("Unknown index [lookup-user1]" ));
600
+ assertThat (resp .getResponse ().getStatusLine ().getStatusCode (), equalTo (HttpStatus .SC_BAD_REQUEST ));
601
+ }
602
+
514
603
private void createEnrichPolicy () throws Exception {
515
604
createIndex ("songs" , Settings .EMPTY , """
516
605
"properties":{"song_id": {"type": "keyword"}, "title": {"type": "keyword"}, "artist": {"type": "keyword"} }
0 commit comments