controller.registerHandler(POST, "/{index}/{type}/{id}/_explain", this);
}
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
final ExplainRequest explainRequest = new ExplainRequest(request.param("index"), request.param("type"), request.param("id"));
explainRequest.parent(request.param("parent"));
explainRequest.routing(request.param("routing"));
explainRequest.preference(request.param("preference"));
String sourceString = request.param("source");
String queryString = request.param("q");
if (request.hasContent()) {
explainRequest.source(request.content(), request.contentUnsafe());
} else if (sourceString != null) {
explainRequest.source(new BytesArray(request.param("source")), false);
} else if (queryString != null) {
QueryStringQueryBuilder queryStringBuilder = QueryBuilders.queryString(queryString);
queryStringBuilder.defaultField(request.param("df"));
queryStringBuilder.analyzer(request.param("analyzer"));
queryStringBuilder.analyzeWildcard(request.paramAsBoolean("analyze_wildcard", false));
queryStringBuilder.lowercaseExpandedTerms(request.paramAsBoolean("lowercase_expanded_terms", true));
queryStringBuilder.lenient(request.paramAsBoolean("lenient", null));
String defaultOperator = request.param("default_operator");
if (defaultOperator != null) {
if ("OR".equals(defaultOperator)) {
queryStringBuilder.defaultOperator(QueryStringQueryBuilder.Operator.OR);
} else if ("AND".equals(defaultOperator)) {
queryStringBuilder.defaultOperator(QueryStringQueryBuilder.Operator.AND);
} else {
throw new ElasticsearchIllegalArgumentException("Unsupported defaultOperator [" + defaultOperator + "], can either be [OR] or [AND]");
}
}
QuerySourceBuilder querySourceBuilder = new QuerySourceBuilder();
querySourceBuilder.setQuery(queryStringBuilder);
explainRequest.source(querySourceBuilder);
}
String sField = request.param("fields");
if (sField != null) {
String[] sFields = Strings.splitStringByCommaToArray(sField);
if (sFields != null) {
explainRequest.fields(sFields);
}
}
explainRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request));
client.explain(explainRequest, new RestBuilderListener<ExplainResponse>(channel) {
@Override
public RestResponse buildResponse(ExplainResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
//null checks for bw comp, since we only added in 1.4 index, type and id to ExplainResponse
builder.field(Fields._INDEX, response.getIndex() != null ? response.getIndex() : explainRequest.index())
.field(Fields._TYPE, response.getType() != null ? response.getType() : explainRequest.type())
.field(Fields._ID, response.getId() != null ? response.getId() : explainRequest.id())
.field(Fields.MATCHED, response.isMatch());
if (response.hasExplanation()) {
builder.startObject(Fields.EXPLANATION);
buildExplanation(builder, response.getExplanation());