Skip to content

Commit 814cde1

Browse files
authored
Improve index resolution test (#133308)
1 parent a1d1a81 commit 814cde1

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/IndexResolutionIT.java

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222
import org.elasticsearch.xpack.esql.VerificationException;
2323
import org.elasticsearch.xpack.esql.action.AbstractEsqlIntegTestCase;
2424
import org.elasticsearch.xpack.esql.action.EsqlQueryResponse;
25+
import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute;
2526

2627
import java.util.Collection;
2728
import java.util.List;
29+
import java.util.Objects;
2830
import java.util.UUID;
2931

3032
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
3133
import static org.elasticsearch.xpack.esql.action.EsqlQueryRequest.syncEsqlQueryRequest;
34+
import static org.hamcrest.Matchers.containsInAnyOrder;
3235
import static org.hamcrest.Matchers.containsString;
3336
import static org.hamcrest.Matchers.equalTo;
3437

@@ -41,7 +44,7 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
4144

4245
public void testResolvesConcreteIndex() {
4346
assertAcked(client().admin().indices().prepareCreate("index-1"));
44-
indexRandom(true, "index-1", 10);
47+
indexRandom(true, "index-1", 1);
4548

4649
try (var response = run(syncEsqlQueryRequest().query("FROM index-1"))) {
4750
assertOk(response);
@@ -50,7 +53,7 @@ public void testResolvesConcreteIndex() {
5053

5154
public void testResolvesAlias() {
5255
assertAcked(client().admin().indices().prepareCreate("index-1"));
53-
indexRandom(true, "index-1", 10);
56+
indexRandom(true, "index-1", 1);
5457
assertAcked(client().admin().indices().prepareAliases(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT).addAlias("index-1", "alias-1"));
5558

5659
try (var response = run(syncEsqlQueryRequest().query("FROM alias-1"))) {
@@ -84,9 +87,9 @@ public void testResolvesDataStream() {
8487

8588
public void testResolvesPattern() {
8689
assertAcked(client().admin().indices().prepareCreate("index-1"));
87-
indexRandom(true, "index-1", 10);
90+
indexRandom(true, "index-1", 1);
8891
assertAcked(client().admin().indices().prepareCreate("index-2"));
89-
indexRandom(true, "index-2", 10);
92+
indexRandom(true, "index-2", 1);
9093

9194
try (var response = run(syncEsqlQueryRequest().query("FROM index-*"))) {
9295
assertOk(response);
@@ -111,13 +114,13 @@ public void testDoesNotResolveEmptyPattern() {
111114

112115
public void testDoesNotResolveClosedIndex() {
113116
assertAcked(client().admin().indices().prepareCreate("index-1"));
114-
indexRandom(true, "index-1", 10);
117+
indexRandom(true, "index-1", 1);
115118
// Create index only waits for primary/indexing shard to be assigned.
116119
// This is enough to index and search documents, however all shards (including replicas) must be assigned before close.
117120
ensureGreen("index-1");
118121
assertAcked(client().admin().indices().prepareClose("index-1"));
119122
assertAcked(client().admin().indices().prepareCreate("index-2"));
120-
indexRandom(true, "index-2", 15);
123+
indexRandom(true, "index-2", 1);
121124

122125
expectThrows(
123126
ClusterBlockException.class,
@@ -129,40 +132,40 @@ public void testDoesNotResolveClosedIndex() {
129132
containsString("index [index-1] blocked by: [FORBIDDEN/4/index closed]"),
130133
() -> run(syncEsqlQueryRequest().query("FROM index-1,index-2"))
131134
);
132-
try (var response = run(syncEsqlQueryRequest().query("FROM index-*"))) {
135+
try (var response = run(syncEsqlQueryRequest().query("FROM index-* METADATA _index"))) {
133136
assertOk(response);
134-
assertResultCount(response, 15);// only index-2 records match
137+
assertResultConcreteIndices(response, "index-2"); // only open index-2 matches
135138
}
136139
}
137140

138141
public void testHiddenIndices() {
139142
assertAcked(client().admin().indices().prepareCreate("regular-index-1"));
140-
indexRandom(true, "regular-index-1", 10);
143+
indexRandom(true, "regular-index-1", 1);
141144
assertAcked(
142145
client().admin()
143146
.indices()
144147
.prepareCreate(".hidden-index-1")
145148
.setSettings(Settings.builder().put(IndexMetadata.SETTING_INDEX_HIDDEN, true))
146149
);
147-
indexRandom(true, ".hidden-index-1", 15);
150+
indexRandom(true, ".hidden-index-1", 1);
148151

149-
try (var response = run(syncEsqlQueryRequest().query("FROM .hidden-index-1"))) {
152+
try (var response = run(syncEsqlQueryRequest().query("FROM .hidden-index-1 METADATA _index"))) {
150153
assertOk(response);
151-
assertResultCount(response, 15);
154+
assertResultConcreteIndices(response, ".hidden-index-1");
152155
}
153-
try (var response = run(syncEsqlQueryRequest().query("FROM *-index-1"))) {
156+
try (var response = run(syncEsqlQueryRequest().query("FROM *-index-1 METADATA _index"))) {
154157
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
156159
}
157-
try (var response = run(syncEsqlQueryRequest().query("FROM .hidden-*"))) {
160+
try (var response = run(syncEsqlQueryRequest().query("FROM .hidden-* METADATA _index"))) {
158161
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
160163
}
161164
}
162165

163166
public void testUnavailableIndex() {
164167
assertAcked(client().admin().indices().prepareCreate("available-index-1"));
165-
indexRandom(true, "available-index-1", 10);
168+
indexRandom(true, "available-index-1", 1);
166169
assertAcked(
167170
client().admin()
168171
.indices()
@@ -176,7 +179,6 @@ public void testUnavailableIndex() {
176179
containsString("index [unavailable-index-1] has no active shard copy"),
177180
() -> run(syncEsqlQueryRequest().query("FROM unavailable-index-1"))
178181
);
179-
180182
expectThrows(
181183
NoShardAvailableActionException.class,
182184
containsString("index [unavailable-index-1] has no active shard copy"),
@@ -194,25 +196,36 @@ public void testPartialResolution() {
194196
assertAcked(client().admin().indices().prepareCreate("index-2"));
195197
indexRandom(true, "index-2", 10);
196198

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
199201
}
200202
expectThrows(
201203
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"))
204211
);
205212
}
206213

207214
private static void assertOk(EsqlQueryResponse response) {
208215
assertThat(response.isPartial(), equalTo(false));
209216
}
210217

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+
}
215228
}
216-
assertThat(count, equalTo(rows));
229+
throw new AssertionError("no _index column found");
217230
}
218231
}

0 commit comments

Comments
 (0)