Skip to content

Commit 9584d10

Browse files
authored
_validate request does not honour ignore_unavailable (#116656)
The IndicesOption has been updated into the ValidateQueryRequest to encapsulate the following logic. If we target a closed index and ignore_unavailable=false, we get an IndexClosedException, otherwise if the request contains ignore_unavailable=true, we safely skip the closed index.
1 parent bf67e23 commit 9584d10

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-10
lines changed

docs/changelog/116656.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 116656
2+
summary: _validate does not honour ignore_unavailable
3+
area: Search
4+
type: bug
5+
issues:
6+
- 116594

server/src/internalClusterTest/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void testWildcardBehaviour() throws Exception {
287287
verify(indicesStats(indices), false);
288288
verify(forceMerge(indices), false);
289289
verify(refreshBuilder(indices), false);
290-
verify(validateQuery(indices), true);
290+
verify(validateQuery(indices), false);
291291
verify(getAliases(indices), false);
292292
verify(getFieldMapping(indices), false);
293293
verify(getMapping(indices), false);
@@ -338,7 +338,7 @@ public void testWildcardBehaviour() throws Exception {
338338
verify(indicesStats(indices), false);
339339
verify(forceMerge(indices), false);
340340
verify(refreshBuilder(indices), false);
341-
verify(validateQuery(indices), true);
341+
verify(validateQuery(indices), false);
342342
verify(getAliases(indices), false);
343343
verify(getFieldMapping(indices), false);
344344
verify(getMapping(indices), false);

server/src/internalClusterTest/java/org/elasticsearch/validate/SimpleValidateQueryIT.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
package org.elasticsearch.validate;
1010

1111
import org.elasticsearch.action.admin.indices.alias.Alias;
12+
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
1213
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse;
14+
import org.elasticsearch.action.support.IndicesOptions;
1315
import org.elasticsearch.client.internal.Client;
1416
import org.elasticsearch.common.bytes.BytesArray;
1517
import org.elasticsearch.common.settings.Settings;
1618
import org.elasticsearch.common.unit.Fuzziness;
17-
import org.elasticsearch.index.IndexNotFoundException;
1819
import org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item;
1920
import org.elasticsearch.index.query.QueryBuilder;
2021
import org.elasticsearch.index.query.QueryBuilders;
2122
import org.elasticsearch.index.query.TermsQueryBuilder;
23+
import org.elasticsearch.indices.IndexClosedException;
2224
import org.elasticsearch.indices.TermsLookup;
2325
import org.elasticsearch.test.ESIntegTestCase;
2426
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
@@ -207,12 +209,8 @@ public void testExplainDateRangeInQueryString() {
207209
}
208210

209211
public void testValidateEmptyCluster() {
210-
try {
211-
indicesAdmin().prepareValidateQuery().get();
212-
fail("Expected IndexNotFoundException");
213-
} catch (IndexNotFoundException e) {
214-
assertThat(e.getMessage(), is("no such index [_all] and no indices exist"));
215-
}
212+
ValidateQueryResponse response = indicesAdmin().prepareValidateQuery().get();
213+
assertThat(response.getTotalShards(), is(0));
216214
}
217215

218216
public void testExplainNoQuery() {
@@ -379,4 +377,52 @@ public void testExplainTermsQueryWithLookup() {
379377
ValidateQueryResponse response = indicesAdmin().prepareValidateQuery("twitter").setQuery(termsLookupQuery).setExplain(true).get();
380378
assertThat(response.isValid(), is(true));
381379
}
380+
381+
public void testOneClosedIndex() {
382+
createIndex("test");
383+
384+
boolean ignoreUnavailable = false;
385+
IndicesOptions options = IndicesOptions.fromOptions(ignoreUnavailable, true, true, false, true, true, false, false);
386+
client().admin().indices().close(new CloseIndexRequest("test")).actionGet();
387+
IndexClosedException ex = expectThrows(
388+
IndexClosedException.class,
389+
indicesAdmin().prepareValidateQuery("test").setIndicesOptions(options)
390+
);
391+
assertEquals("closed", ex.getMessage());
392+
}
393+
394+
public void testOneClosedIndexIgnoreUnavailable() {
395+
createIndex("test");
396+
397+
boolean ignoreUnavailable = true;
398+
IndicesOptions options = IndicesOptions.fromOptions(ignoreUnavailable, true, true, false, true, true, false, false);
399+
client().admin().indices().close(new CloseIndexRequest("test")).actionGet();
400+
ValidateQueryResponse response = indicesAdmin().prepareValidateQuery("test").setIndicesOptions(options).get();
401+
assertThat(response.getTotalShards(), is(0));
402+
}
403+
404+
public void testTwoIndicesOneClosed() {
405+
createIndex("test1");
406+
createIndex("test2");
407+
408+
boolean ignoreUnavailable = false;
409+
IndicesOptions options = IndicesOptions.fromOptions(ignoreUnavailable, true, true, false, true, true, false, false);
410+
client().admin().indices().close(new CloseIndexRequest("test1")).actionGet();
411+
IndexClosedException ex = expectThrows(
412+
IndexClosedException.class,
413+
indicesAdmin().prepareValidateQuery("test1", "test2").setIndicesOptions(options)
414+
);
415+
assertEquals("closed", ex.getMessage());
416+
}
417+
418+
public void testTwoIndicesOneClosedIgnoreUnavailable() {
419+
createIndex("test1");
420+
createIndex("test2");
421+
422+
boolean ignoreUnavailable = true;
423+
IndicesOptions options = IndicesOptions.fromOptions(ignoreUnavailable, true, true, false, true, true, false, false);
424+
client().admin().indices().close(new CloseIndexRequest("test1")).actionGet();
425+
ValidateQueryResponse response = indicesAdmin().prepareValidateQuery("test1", "test2").setIndicesOptions(options).get();
426+
assertThat(response.getTotalShards(), is(1));
427+
}
382428
}

server/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
public final class ValidateQueryRequest extends BroadcastRequest<ValidateQueryRequest> implements ToXContentObject {
3434

35-
public static final IndicesOptions DEFAULT_INDICES_OPTIONS = IndicesOptions.fromOptions(false, false, true, false);
35+
public static final IndicesOptions DEFAULT_INDICES_OPTIONS = IndicesOptions.strictExpandOpenAndForbidClosed();
3636

3737
private QueryBuilder query = new MatchAllQueryBuilder();
3838

0 commit comments

Comments
 (0)