From 6f1b398b33856aff0ba396c5276e4b3dd22235f8 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Wed, 22 Apr 2015 15:30:04 +0200 Subject: [PATCH] Fix RestAnalyzeAction to also accept source parameter instead of the request body --- .../indices/analyze/RestAnalyzeAction.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/elasticsearch/rest/action/admin/indices/analyze/RestAnalyzeAction.java b/src/main/java/org/elasticsearch/rest/action/admin/indices/analyze/RestAnalyzeAction.java index 0495d3420cb..0d4dea78218 100644 --- a/src/main/java/org/elasticsearch/rest/action/admin/indices/analyze/RestAnalyzeAction.java +++ b/src/main/java/org/elasticsearch/rest/action/admin/indices/analyze/RestAnalyzeAction.java @@ -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"); + } +