From 08f8312457b5eecfaa6424334b2e4c179edc69ea Mon Sep 17 00:00:00 2001 From: Dimitris Rempapis Date: Wed, 13 Nov 2024 14:31:18 +0200 Subject: [PATCH] _validate request does not honour ignore_unavailable (#116656) (#116717) 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. --- docs/changelog/116656.yaml | 6 ++ .../indices/IndicesOptionsIntegrationIT.java | 4 +- .../validate/SimpleValidateQueryIT.java | 60 ++++++++++++++++--- .../validate/query/ValidateQueryRequest.java | 2 +- 4 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 docs/changelog/116656.yaml diff --git a/docs/changelog/116656.yaml b/docs/changelog/116656.yaml new file mode 100644 index 0000000000000..eb5d5a1cfc201 --- /dev/null +++ b/docs/changelog/116656.yaml @@ -0,0 +1,6 @@ +pr: 116656 +summary: _validate does not honour ignore_unavailable +area: Search +type: bug +issues: + - 116594 diff --git a/server/src/internalClusterTest/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java b/server/src/internalClusterTest/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java index cd6af7730113a..2a097e7ce980c 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java @@ -287,7 +287,7 @@ public void testWildcardBehaviour() throws Exception { verify(indicesStats(indices), false); verify(forceMerge(indices), false); verify(refreshBuilder(indices), false); - verify(validateQuery(indices), true); + verify(validateQuery(indices), false); verify(getAliases(indices), false); verify(getFieldMapping(indices), false); verify(getMapping(indices), false); @@ -338,7 +338,7 @@ public void testWildcardBehaviour() throws Exception { verify(indicesStats(indices), false); verify(forceMerge(indices), false); verify(refreshBuilder(indices), false); - verify(validateQuery(indices), true); + verify(validateQuery(indices), false); verify(getAliases(indices), false); verify(getFieldMapping(indices), false); verify(getMapping(indices), false); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/validate/SimpleValidateQueryIT.java b/server/src/internalClusterTest/java/org/elasticsearch/validate/SimpleValidateQueryIT.java index 37d2f4e1a9387..388421b6dd53f 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/validate/SimpleValidateQueryIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/validate/SimpleValidateQueryIT.java @@ -9,16 +9,18 @@ package org.elasticsearch.validate; import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse; +import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.internal.Client; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; -import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermsQueryBuilder; +import org.elasticsearch.indices.IndexClosedException; import org.elasticsearch.indices.TermsLookup; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase.ClusterScope; @@ -207,12 +209,8 @@ public void testExplainDateRangeInQueryString() { } public void testValidateEmptyCluster() { - try { - indicesAdmin().prepareValidateQuery().get(); - fail("Expected IndexNotFoundException"); - } catch (IndexNotFoundException e) { - assertThat(e.getMessage(), is("no such index [_all] and no indices exist")); - } + ValidateQueryResponse response = indicesAdmin().prepareValidateQuery().get(); + assertThat(response.getTotalShards(), is(0)); } public void testExplainNoQuery() { @@ -379,4 +377,52 @@ public void testExplainTermsQueryWithLookup() { ValidateQueryResponse response = indicesAdmin().prepareValidateQuery("twitter").setQuery(termsLookupQuery).setExplain(true).get(); assertThat(response.isValid(), is(true)); } + + public void testOneClosedIndex() { + createIndex("test"); + + boolean ignoreUnavailable = false; + IndicesOptions options = IndicesOptions.fromOptions(ignoreUnavailable, true, true, false, true, true, false, false); + client().admin().indices().close(new CloseIndexRequest("test")).actionGet(); + IndexClosedException ex = expectThrows( + IndexClosedException.class, + indicesAdmin().prepareValidateQuery("test").setIndicesOptions(options) + ); + assertEquals("closed", ex.getMessage()); + } + + public void testOneClosedIndexIgnoreUnavailable() { + createIndex("test"); + + boolean ignoreUnavailable = true; + IndicesOptions options = IndicesOptions.fromOptions(ignoreUnavailable, true, true, false, true, true, false, false); + client().admin().indices().close(new CloseIndexRequest("test")).actionGet(); + ValidateQueryResponse response = indicesAdmin().prepareValidateQuery("test").setIndicesOptions(options).get(); + assertThat(response.getTotalShards(), is(0)); + } + + public void testTwoIndicesOneClosed() { + createIndex("test1"); + createIndex("test2"); + + boolean ignoreUnavailable = false; + IndicesOptions options = IndicesOptions.fromOptions(ignoreUnavailable, true, true, false, true, true, false, false); + client().admin().indices().close(new CloseIndexRequest("test1")).actionGet(); + IndexClosedException ex = expectThrows( + IndexClosedException.class, + indicesAdmin().prepareValidateQuery("test1", "test2").setIndicesOptions(options) + ); + assertEquals("closed", ex.getMessage()); + } + + public void testTwoIndicesOneClosedIgnoreUnavailable() { + createIndex("test1"); + createIndex("test2"); + + boolean ignoreUnavailable = true; + IndicesOptions options = IndicesOptions.fromOptions(ignoreUnavailable, true, true, false, true, true, false, false); + client().admin().indices().close(new CloseIndexRequest("test1")).actionGet(); + ValidateQueryResponse response = indicesAdmin().prepareValidateQuery("test1", "test2").setIndicesOptions(options).get(); + assertThat(response.getTotalShards(), is(1)); + } } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java index 4c3f32240ca8c..f30206c1d238a 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/validate/query/ValidateQueryRequest.java @@ -32,7 +32,7 @@ */ public final class ValidateQueryRequest extends BroadcastRequest implements ToXContentObject { - public static final IndicesOptions DEFAULT_INDICES_OPTIONS = IndicesOptions.fromOptions(false, false, true, false); + public static final IndicesOptions DEFAULT_INDICES_OPTIONS = IndicesOptions.strictExpandOpenAndForbidClosed(); private QueryBuilder query = new MatchAllQueryBuilder();