Skip to content

Commit

Permalink
_validate request does not honour ignore_unavailable (elastic#116656) (
Browse files Browse the repository at this point in the history
…elastic#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.
  • Loading branch information
drempapis authored Nov 13, 2024
1 parent 7d33c5c commit 08f8312
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/116656.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 116656
summary: _validate does not honour ignore_unavailable
area: Search
type: bug
issues:
- 116594
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public final class ValidateQueryRequest extends BroadcastRequest<ValidateQueryRequest> 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();

Expand Down

0 comments on commit 08f8312

Please sign in to comment.