From 96dcc8b152204c98c7ccae4c7c25ba53b9ed87e8 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Mon, 27 Jan 2014 14:15:08 +0100 Subject: [PATCH] percolate REST API should support source parameter As stated in documentation, we should support `?source=` parameter in percolate REST operations. This is how to reproduce it: ```sh curl -XDELETE "http://localhost:9200/test" curl -XPUT "http://localhost:9200/test/.percolator/1" -d' { "query" : { "match" : { "foo" : "bar" } } }' # This one works curl -XPOST "http://localhost:9200/test/message/_percolate" -d '{ "doc" : { "foo" : "bar is in foo" } }' # This one gives: BroadcastShardOperationFailedException[[test][2] ]; nested: PercolateException[failed to percolate]; nested: ElasticsearchIllegalArgumentException[Nothing to percolate]; curl -XGET "http://localhost:9200/test/message/_percolate?source=%7B%22doc%22%3A%7B%22foo%22%3A%22bar%20is%20in%20foo%22%7D%7D" ``` Closes #4903. --- .../action/percolate/RestPercolateAction.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/elasticsearch/rest/action/percolate/RestPercolateAction.java b/src/main/java/org/elasticsearch/rest/action/percolate/RestPercolateAction.java index b0806a7ddcf41..b83bf7d8dbad0 100644 --- a/src/main/java/org/elasticsearch/rest/action/percolate/RestPercolateAction.java +++ b/src/main/java/org/elasticsearch/rest/action/percolate/RestPercolateAction.java @@ -26,6 +26,8 @@ import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading; import org.elasticsearch.client.Client; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -69,7 +71,18 @@ void parseDocPercolate(PercolateRequest percolateRequest, RestRequest restReques percolateRequest.documentType(restRequest.param("type")); percolateRequest.routing(restRequest.param("routing")); percolateRequest.preference(restRequest.param("preference")); - percolateRequest.source(restRequest.content(), restRequest.contentUnsafe()); + + BytesReference content = null; + if (restRequest.hasContent()) { + content = restRequest.content(); + } else { + String source = restRequest.param("source"); + if (source != null) { + content = new BytesArray(source); + } + } + + percolateRequest.source(content, restRequest.contentUnsafe()); percolateRequest.indicesOptions(IndicesOptions.fromRequest(restRequest, percolateRequest.indicesOptions())); executePercolate(percolateRequest, restRequest, restChannel);