-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
REST high-level client: add validate query API #31077
Changes from 7 commits
6a6b61f
aad5341
7bcc283
53b0747
2d8f6b3
ed05533
8c9cb3c
e4da255
1677ed6
00e7169
c5095db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ | |
import org.elasticsearch.action.admin.indices.shrink.ResizeType; | ||
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest; | ||
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; | ||
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; | ||
import org.elasticsearch.action.bulk.BulkRequest; | ||
import org.elasticsearch.action.delete.DeleteRequest; | ||
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest; | ||
|
@@ -490,7 +491,7 @@ static Request update(UpdateRequest updateRequest) throws IOException { | |
XContentType upsertContentType = updateRequest.upsertRequest().getContentType(); | ||
if ((xContentType != null) && (xContentType != upsertContentType)) { | ||
throw new IllegalStateException("Update request cannot have different content types for doc [" + xContentType + "]" + | ||
" and upsert [" + upsertContentType + "] documents"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you revert these formatting changes that aren't part the change? It make git blame harder to work with and I kind of like the old formatting better anyway. |
||
" and upsert [" + upsertContentType + "] documents"); | ||
} else { | ||
xContentType = upsertContentType; | ||
} | ||
|
@@ -579,7 +580,7 @@ static Request searchTemplate(SearchTemplateRequest searchTemplateRequest) throw | |
|
||
static Request existsAlias(GetAliasesRequest getAliasesRequest) { | ||
if ((getAliasesRequest.indices() == null || getAliasesRequest.indices().length == 0) && | ||
(getAliasesRequest.aliases() == null || getAliasesRequest.aliases().length == 0)) { | ||
(getAliasesRequest.aliases() == null || getAliasesRequest.aliases().length == 0)) { | ||
throw new IllegalArgumentException("existsAlias requires at least an alias or an index"); | ||
} | ||
String[] indices = getAliasesRequest.indices() == null ? Strings.EMPTY_ARRAY : getAliasesRequest.indices(); | ||
|
@@ -628,8 +629,8 @@ static Request shrink(ResizeRequest resizeRequest) throws IOException { | |
|
||
private static Request resize(ResizeRequest resizeRequest) throws IOException { | ||
String endpoint = new EndpointBuilder().addPathPart(resizeRequest.getSourceIndex()) | ||
.addPathPartAsIs("_" + resizeRequest.getResizeType().name().toLowerCase(Locale.ROOT)) | ||
.addPathPart(resizeRequest.getTargetIndexRequest().index()).build(); | ||
.addPathPartAsIs("_" + resizeRequest.getResizeType().name().toLowerCase(Locale.ROOT)) | ||
.addPathPart(resizeRequest.getTargetIndexRequest().index()).build(); | ||
Request request = new Request(HttpPut.METHOD_NAME, endpoint); | ||
|
||
Params params = new Params(request); | ||
|
@@ -733,7 +734,7 @@ static Request clusterHealth(ClusterHealthRequest healthRequest) { | |
|
||
static Request rollover(RolloverRequest rolloverRequest) throws IOException { | ||
String endpoint = new EndpointBuilder().addPathPart(rolloverRequest.getAlias()).addPathPartAsIs("_rollover") | ||
.addPathPart(rolloverRequest.getNewIndexName()).build(); | ||
.addPathPart(rolloverRequest.getNewIndexName()).build(); | ||
Request request = new Request(HttpPost.METHOD_NAME, endpoint); | ||
|
||
Params params = new Params(request); | ||
|
@@ -856,6 +857,21 @@ static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) thro | |
return request; | ||
} | ||
|
||
static Request validateQuery(ValidateQueryRequest validateQueryRequest) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thanks for catching this. |
||
String[] indices = validateQueryRequest.indices() == null ? Strings.EMPTY_ARRAY : validateQueryRequest.indices(); | ||
String[] types = validateQueryRequest.types() == null || indices.length <= 0 ? Strings.EMPTY_ARRAY : validateQueryRequest.types(); | ||
String endpoint = endpoint(indices, types, "_validate/query"); | ||
Request request = new Request(HttpGet.METHOD_NAME, endpoint); | ||
Params params = new Params(request); | ||
params.withIndicesOptions(validateQueryRequest.indicesOptions()); | ||
params.putParam("explain", Boolean.toString(validateQueryRequest.explain())); | ||
params.putParam("all_shards", Boolean.toString(validateQueryRequest.allShards())); | ||
params.putParam("rewrite", Boolean.toString(validateQueryRequest.rewrite())); | ||
request.setEntity(createEntity(validateQueryRequest, REQUEST_BODY_CONTENT_TYPE)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks like we might be missing a few more options such as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return request; | ||
} | ||
|
||
|
||
static Request getAlias(GetAliasesRequest getAliasesRequest) { | ||
String[] indices = getAliasesRequest.indices() == null ? Strings.EMPTY_ARRAY : getAliasesRequest.indices(); | ||
String[] aliases = getAliasesRequest.aliases() == null ? Strings.EMPTY_ARRAY : getAliasesRequest.aliases(); | ||
|
@@ -900,12 +916,12 @@ static String endpoint(String[] indices, String endpoint) { | |
|
||
static String endpoint(String[] indices, String[] types, String endpoint) { | ||
return new EndpointBuilder().addCommaSeparatedPathParts(indices).addCommaSeparatedPathParts(types) | ||
.addPathPartAsIs(endpoint).build(); | ||
.addPathPartAsIs(endpoint).build(); | ||
} | ||
|
||
static String endpoint(String[] indices, String endpoint, String[] suffixes) { | ||
return new EndpointBuilder().addCommaSeparatedPathParts(indices).addPathPartAsIs(endpoint) | ||
.addCommaSeparatedPathParts(suffixes).build(); | ||
.addCommaSeparatedPathParts(suffixes).build(); | ||
} | ||
|
||
static String endpoint(String[] indices, String endpoint, String type) { | ||
|
@@ -1201,14 +1217,14 @@ static XContentType enforceSameContentType(IndexRequest indexRequest, @Nullable | |
XContentType requestContentType = indexRequest.getContentType(); | ||
if (requestContentType != XContentType.JSON && requestContentType != XContentType.SMILE) { | ||
throw new IllegalArgumentException("Unsupported content-type found for request with content-type [" + requestContentType | ||
+ "], only JSON and SMILE are supported"); | ||
+ "], only JSON and SMILE are supported"); | ||
} | ||
if (xContentType == null) { | ||
return requestContentType; | ||
} | ||
if (requestContentType != xContentType) { | ||
throw new IllegalArgumentException("Mismatching content-type found for request with content-type [" + requestContentType | ||
+ "], previous requests have content-type [" + xContentType + "]"); | ||
+ "], previous requests have content-type [" + xContentType + "]"); | ||
} | ||
return xContentType; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ | |
import org.elasticsearch.action.admin.indices.shrink.ResizeType; | ||
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest; | ||
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; | ||
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; | ||
import org.elasticsearch.action.bulk.BulkRequest; | ||
import org.elasticsearch.action.bulk.BulkShardRequest; | ||
import org.elasticsearch.action.delete.DeleteRequest; | ||
|
@@ -712,8 +713,8 @@ public void testSyncedFlush() { | |
Request request = RequestConverters.flushSynced(syncedFlushRequest); | ||
StringJoiner endpoint = new StringJoiner("/", "/", ""); | ||
if (indices != null && indices.length > 0) { | ||
endpoint.add(String.join(",", indices)); | ||
} | ||
endpoint.add(String.join(",", indices)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This formatting change is fine though, because the old code was just wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm aware that this is somewhat contradictory. |
||
} | ||
endpoint.add("_flush/synced"); | ||
assertThat(request.getEndpoint(), equalTo(endpoint.toString())); | ||
assertThat(request.getParameters(), equalTo(expectedParams)); | ||
|
@@ -1895,6 +1896,40 @@ public void testPutTemplateRequest() throws Exception { | |
assertToXContentBody(putTemplateRequest, request.getEntity()); | ||
} | ||
|
||
public void testValidateQuery() throws Exception { | ||
String[] indices = randomBoolean() ? null : randomIndicesNames(0, 5); | ||
String[] types = randomBoolean() ? generateRandomStringArray(5, 5, false, false) : null; | ||
ValidateQueryRequest validateQueryRequest; | ||
if (randomBoolean()) { | ||
validateQueryRequest = new ValidateQueryRequest(indices); | ||
} else { | ||
validateQueryRequest = new ValidateQueryRequest(); | ||
validateQueryRequest.indices(indices); | ||
} | ||
validateQueryRequest.types(types); | ||
Map<String, String> expectedParams = new HashMap<>(); | ||
setRandomIndicesOptions(validateQueryRequest::indicesOptions, validateQueryRequest::indicesOptions, expectedParams); | ||
validateQueryRequest.explain(randomBoolean()); | ||
validateQueryRequest.rewrite(randomBoolean()); | ||
validateQueryRequest.allShards(randomBoolean()); | ||
expectedParams.put("explain", Boolean.toString(validateQueryRequest.explain())); | ||
expectedParams.put("rewrite", Boolean.toString(validateQueryRequest.rewrite())); | ||
expectedParams.put("all_shards", Boolean.toString(validateQueryRequest.allShards())); | ||
Request request = RequestConverters.validateQuery(validateQueryRequest); | ||
StringJoiner endpoint = new StringJoiner("/", "/", ""); | ||
if (indices != null && indices.length > 0) { | ||
endpoint.add(String.join(",", indices)); | ||
if (types != null && types.length > 0) { | ||
endpoint.add(String.join(",", types)); | ||
} | ||
} | ||
endpoint.add("_validate/query"); | ||
assertThat(request.getEndpoint(), equalTo(endpoint.toString())); | ||
assertThat(request.getParameters(), equalTo(expectedParams)); | ||
assertToXContentBody(validateQueryRequest, request.getEntity()); | ||
assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME)); | ||
} | ||
|
||
public void testGetTemplateRequest() throws Exception { | ||
Map<String, String> encodes = new HashMap<>(); | ||
encodes.put("log", "log"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please add params tag like we have now in all of the other methods?