22
22
import org .elasticsearch .xpack .esql .VerificationException ;
23
23
import org .elasticsearch .xpack .esql .action .AbstractEsqlIntegTestCase ;
24
24
import org .elasticsearch .xpack .esql .action .EsqlQueryResponse ;
25
+ import org .elasticsearch .xpack .esql .core .expression .MetadataAttribute ;
25
26
26
27
import java .util .Collection ;
27
28
import java .util .List ;
29
+ import java .util .Objects ;
28
30
import java .util .UUID ;
29
31
30
32
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
31
33
import static org .elasticsearch .xpack .esql .action .EsqlQueryRequest .syncEsqlQueryRequest ;
34
+ import static org .hamcrest .Matchers .containsInAnyOrder ;
32
35
import static org .hamcrest .Matchers .containsString ;
33
36
import static org .hamcrest .Matchers .equalTo ;
34
37
@@ -41,7 +44,7 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
41
44
42
45
public void testResolvesConcreteIndex () {
43
46
assertAcked (client ().admin ().indices ().prepareCreate ("index-1" ));
44
- indexRandom (true , "index-1" , 10 );
47
+ indexRandom (true , "index-1" , 1 );
45
48
46
49
try (var response = run (syncEsqlQueryRequest ().query ("FROM index-1" ))) {
47
50
assertOk (response );
@@ -50,7 +53,7 @@ public void testResolvesConcreteIndex() {
50
53
51
54
public void testResolvesAlias () {
52
55
assertAcked (client ().admin ().indices ().prepareCreate ("index-1" ));
53
- indexRandom (true , "index-1" , 10 );
56
+ indexRandom (true , "index-1" , 1 );
54
57
assertAcked (client ().admin ().indices ().prepareAliases (TEST_REQUEST_TIMEOUT , TEST_REQUEST_TIMEOUT ).addAlias ("index-1" , "alias-1" ));
55
58
56
59
try (var response = run (syncEsqlQueryRequest ().query ("FROM alias-1" ))) {
@@ -84,9 +87,9 @@ public void testResolvesDataStream() {
84
87
85
88
public void testResolvesPattern () {
86
89
assertAcked (client ().admin ().indices ().prepareCreate ("index-1" ));
87
- indexRandom (true , "index-1" , 10 );
90
+ indexRandom (true , "index-1" , 1 );
88
91
assertAcked (client ().admin ().indices ().prepareCreate ("index-2" ));
89
- indexRandom (true , "index-2" , 10 );
92
+ indexRandom (true , "index-2" , 1 );
90
93
91
94
try (var response = run (syncEsqlQueryRequest ().query ("FROM index-*" ))) {
92
95
assertOk (response );
@@ -111,13 +114,13 @@ public void testDoesNotResolveEmptyPattern() {
111
114
112
115
public void testDoesNotResolveClosedIndex () {
113
116
assertAcked (client ().admin ().indices ().prepareCreate ("index-1" ));
114
- indexRandom (true , "index-1" , 10 );
117
+ indexRandom (true , "index-1" , 1 );
115
118
// Create index only waits for primary/indexing shard to be assigned.
116
119
// This is enough to index and search documents, however all shards (including replicas) must be assigned before close.
117
120
ensureGreen ("index-1" );
118
121
assertAcked (client ().admin ().indices ().prepareClose ("index-1" ));
119
122
assertAcked (client ().admin ().indices ().prepareCreate ("index-2" ));
120
- indexRandom (true , "index-2" , 15 );
123
+ indexRandom (true , "index-2" , 1 );
121
124
122
125
expectThrows (
123
126
ClusterBlockException .class ,
@@ -129,40 +132,40 @@ public void testDoesNotResolveClosedIndex() {
129
132
containsString ("index [index-1] blocked by: [FORBIDDEN/4/index closed]" ),
130
133
() -> run (syncEsqlQueryRequest ().query ("FROM index-1,index-2" ))
131
134
);
132
- try (var response = run (syncEsqlQueryRequest ().query ("FROM index-*" ))) {
135
+ try (var response = run (syncEsqlQueryRequest ().query ("FROM index-* METADATA _index " ))) {
133
136
assertOk (response );
134
- assertResultCount (response , 15 ); // only index-2 records match
137
+ assertResultConcreteIndices (response , "index-2" ); // only open index-2 matches
135
138
}
136
139
}
137
140
138
141
public void testHiddenIndices () {
139
142
assertAcked (client ().admin ().indices ().prepareCreate ("regular-index-1" ));
140
- indexRandom (true , "regular-index-1" , 10 );
143
+ indexRandom (true , "regular-index-1" , 1 );
141
144
assertAcked (
142
145
client ().admin ()
143
146
.indices ()
144
147
.prepareCreate (".hidden-index-1" )
145
148
.setSettings (Settings .builder ().put (IndexMetadata .SETTING_INDEX_HIDDEN , true ))
146
149
);
147
- indexRandom (true , ".hidden-index-1" , 15 );
150
+ indexRandom (true , ".hidden-index-1" , 1 );
148
151
149
- try (var response = run (syncEsqlQueryRequest ().query ("FROM .hidden-index-1" ))) {
152
+ try (var response = run (syncEsqlQueryRequest ().query ("FROM .hidden-index-1 METADATA _index " ))) {
150
153
assertOk (response );
151
- assertResultCount (response , 15 );
154
+ assertResultConcreteIndices (response , ".hidden-index-1" );
152
155
}
153
- try (var response = run (syncEsqlQueryRequest ().query ("FROM *-index-1" ))) {
156
+ try (var response = run (syncEsqlQueryRequest ().query ("FROM *-index-1 METADATA _index " ))) {
154
157
assertOk (response );
155
- assertResultCount (response , 10 ); // only non-hidden index matches when specifying pattern
158
+ assertResultConcreteIndices (response , "regular-index-1" ); // only non-hidden index matches when specifying pattern
156
159
}
157
- try (var response = run (syncEsqlQueryRequest ().query ("FROM .hidden-*" ))) {
160
+ try (var response = run (syncEsqlQueryRequest ().query ("FROM .hidden-* METADATA _index " ))) {
158
161
assertOk (response );
159
- assertResultCount (response , 15 ); // hidden indices do match when specifying hidden/dot pattern
162
+ assertResultConcreteIndices (response , ".hidden-index-1" ); // hidden indices do match when specifying hidden/dot pattern
160
163
}
161
164
}
162
165
163
166
public void testUnavailableIndex () {
164
167
assertAcked (client ().admin ().indices ().prepareCreate ("available-index-1" ));
165
- indexRandom (true , "available-index-1" , 10 );
168
+ indexRandom (true , "available-index-1" , 1 );
166
169
assertAcked (
167
170
client ().admin ()
168
171
.indices ()
@@ -176,7 +179,6 @@ public void testUnavailableIndex() {
176
179
containsString ("index [unavailable-index-1] has no active shard copy" ),
177
180
() -> run (syncEsqlQueryRequest ().query ("FROM unavailable-index-1" ))
178
181
);
179
-
180
182
expectThrows (
181
183
NoShardAvailableActionException .class ,
182
184
containsString ("index [unavailable-index-1] has no active shard copy" ),
@@ -194,25 +196,36 @@ public void testPartialResolution() {
194
196
assertAcked (client ().admin ().indices ().prepareCreate ("index-2" ));
195
197
indexRandom (true , "index-2" , 10 );
196
198
197
- try (var response = run (syncEsqlQueryRequest ().query ("FROM index-1,nonexisting" ))) {
198
- assertOk (response );
199
+ try (var response = run (syncEsqlQueryRequest ().query ("FROM index-1,nonexisting-1 " ))) {
200
+ assertOk (response ); // okay when present index is empty
199
201
}
200
202
expectThrows (
201
203
IndexNotFoundException .class ,
202
- equalTo ("no such index [nonexisting]" ),
203
- () -> run (syncEsqlQueryRequest ().query ("FROM index-2,nonexisting" ))
204
+ equalTo ("no such index [nonexisting-1]" ), // fails when present index is non-empty
205
+ () -> run (syncEsqlQueryRequest ().query ("FROM index-2,nonexisting-1" ))
206
+ );
207
+ expectThrows (
208
+ IndexNotFoundException .class ,
209
+ equalTo ("no such index [nonexisting-1]" ), // only the first missing index is reported
210
+ () -> run (syncEsqlQueryRequest ().query ("FROM index-2,nonexisting-1,nonexisting-2" ))
204
211
);
205
212
}
206
213
207
214
private static void assertOk (EsqlQueryResponse response ) {
208
215
assertThat (response .isPartial (), equalTo (false ));
209
216
}
210
217
211
- private static void assertResultCount (EsqlQueryResponse response , long rows ) {
212
- long count = 0 ;
213
- for (var iterator = response .column (0 ); iterator .hasNext (); iterator .next ()) {
214
- count ++;
218
+ private static void assertResultConcreteIndices (EsqlQueryResponse response , Object ... indices ) {
219
+ var indexColumn = findIndexColumn (response );
220
+ assertThat (() -> response .column (indexColumn ), containsInAnyOrder (indices ));
221
+ }
222
+
223
+ private static int findIndexColumn (EsqlQueryResponse response ) {
224
+ for (int c = 0 ; c < response .columns ().size (); c ++) {
225
+ if (Objects .equals (response .columns ().get (c ).name (), MetadataAttribute .INDEX )) {
226
+ return c ;
227
+ }
215
228
}
216
- assertThat ( count , equalTo ( rows ) );
229
+ throw new AssertionError ( "no _index column found" );
217
230
}
218
231
}
0 commit comments