Fix RestAnalyzeAction to also accept source parameter instead of the request body

This commit is contained in:
Simon Willnauer 2015-04-22 15:30:04 +02:00
parent 05c3d05cff
commit 6f1b398b33
1 changed files with 25 additions and 4 deletions

View File

@ -23,6 +23,7 @@ import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
@ -55,7 +56,9 @@ public class RestAnalyzeAction extends BaseRestHandler {
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
String text = request.param("text");
AnalyzeRequest analyzeRequest = new AnalyzeRequest(request.param("index"));
analyzeRequest.text(text);
analyzeRequest.listenerThreaded(false);
@ -66,16 +69,16 @@ public class RestAnalyzeAction extends BaseRestHandler {
analyzeRequest.tokenFilters(request.paramAsStringArray("token_filters", request.paramAsStringArray("filters", analyzeRequest.tokenFilters())));
analyzeRequest.charFilters(request.paramAsStringArray("char_filters", analyzeRequest.charFilters()));
if (request.hasContent()) {
XContentType type = XContentFactory.xContentType(request.content());
if (request.hasContent() || request.hasParam("source")) {
XContentType type = contentType(request);
if (type == null) {
if (text == null) {
text = request.content().toUtf8();
text = bodyContent(request).toUtf8();
analyzeRequest.text(text);
}
} else {
// NOTE: if rest request with xcontent body has request parameters, the parameters does not override xcontent values
buildFromContent(request.content(), analyzeRequest);
buildFromContent(bodyContent(request), analyzeRequest);
}
}
@ -130,6 +133,24 @@ public class RestAnalyzeAction extends BaseRestHandler {
}
}
private XContentType contentType(final RestRequest request) {
if (request.hasContent()) {
return XContentFactory.xContentType(request.content());
} else if (request.hasParam("source")) {
return XContentFactory.xContentType(request.param("source"));
}
throw new ElasticsearchIllegalArgumentException("Can't guess contentType neither source nor content available");
}
private BytesReference bodyContent(final RestRequest request) {
if (request.hasContent()) {
return request.content();
} else if (request.hasParam("source")) {
return new BytesArray(request.param("source"));
}
throw new ElasticsearchIllegalArgumentException("Can't guess contentType neither source nor content available");
}