use of org.elasticsearch.action.admin.indices.validate.query.QueryExplanation in project elasticsearch by elastic.
the class RestValidateQueryAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(Strings.splitStringByCommaToArray(request.param("index")));
validateQueryRequest.indicesOptions(IndicesOptions.fromRequest(request, validateQueryRequest.indicesOptions()));
validateQueryRequest.explain(request.paramAsBoolean("explain", false));
validateQueryRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
validateQueryRequest.rewrite(request.paramAsBoolean("rewrite", false));
Exception bodyParsingException = null;
try {
request.withContentOrSourceParamParserOrNull(parser -> {
if (parser != null) {
validateQueryRequest.query(RestActions.getQueryContent(parser));
} else if (request.hasParam("q")) {
validateQueryRequest.query(RestActions.urlParamsToQueryBuilder(request));
}
});
} catch (Exception e) {
bodyParsingException = e;
}
final Exception finalBodyParsingException = bodyParsingException;
return channel -> {
if (finalBodyParsingException != null) {
if (finalBodyParsingException instanceof ParsingException) {
handleException(validateQueryRequest, ((ParsingException) finalBodyParsingException).getDetailedMessage(), channel);
} else {
handleException(validateQueryRequest, finalBodyParsingException.getMessage(), channel);
}
} else {
client.admin().indices().validateQuery(validateQueryRequest, new RestBuilderListener<ValidateQueryResponse>(channel) {
@Override
public RestResponse buildResponse(ValidateQueryResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
builder.field(VALID_FIELD, response.isValid());
buildBroadcastShardsHeader(builder, request, response);
if (response.getQueryExplanation() != null && !response.getQueryExplanation().isEmpty()) {
builder.startArray(EXPLANATIONS_FIELD);
for (QueryExplanation explanation : response.getQueryExplanation()) {
builder.startObject();
if (explanation.getIndex() != null) {
builder.field(INDEX_FIELD, explanation.getIndex());
}
builder.field(VALID_FIELD, explanation.isValid());
if (explanation.getError() != null) {
builder.field(ERROR_FIELD, explanation.getError());
}
if (explanation.getExplanation() != null) {
builder.field(EXPLANATION_FIELD, explanation.getExplanation());
}
builder.endObject();
}
builder.endArray();
}
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
}
};
}
Aggregations