summaryrefslogtreecommitdiff
path: root/core/src/main/java/org/elasticsearch/rest/action/search
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main/java/org/elasticsearch/rest/action/search')
-rw-r--r--core/src/main/java/org/elasticsearch/rest/action/search/RestClearScrollAction.java30
-rw-r--r--core/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java12
-rw-r--r--core/src/main/java/org/elasticsearch/rest/action/search/RestSearchScrollAction.java27
3 files changed, 42 insertions, 27 deletions
diff --git a/core/src/main/java/org/elasticsearch/rest/action/search/RestClearScrollAction.java b/core/src/main/java/org/elasticsearch/rest/action/search/RestClearScrollAction.java
index e7ee7c7397..47252f5a10 100644
--- a/core/src/main/java/org/elasticsearch/rest/action/search/RestClearScrollAction.java
+++ b/core/src/main/java/org/elasticsearch/rest/action/search/RestClearScrollAction.java
@@ -20,12 +20,10 @@
package org.elasticsearch.rest.action.search;
import org.elasticsearch.action.search.ClearScrollRequest;
-import org.elasticsearch.action.search.ClearScrollResponse;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
-import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestController;
@@ -50,26 +48,34 @@ public class RestClearScrollAction extends BaseRestHandler {
String scrollIds = request.param("scroll_id");
ClearScrollRequest clearRequest = new ClearScrollRequest();
clearRequest.setScrollIds(Arrays.asList(splitScrollIds(scrollIds)));
- BytesReference body = request.contentOrSourceParam();
- if (body.length() > 0) {
- if (XContentFactory.xContentType(body) == null) {
- scrollIds = body.utf8ToString();
- clearRequest.setScrollIds(Arrays.asList(splitScrollIds(scrollIds)));
+ request.withContentOrSourceParamParserOrNullLenient((xContentParser -> {
+ if (xContentParser == null) {
+ if (request.hasContent()) {
+ // TODO: why do we accept this plain text value? maybe we can just use the scroll params?
+ BytesReference body = request.content();
+ String bodyScrollIds = body.utf8ToString();
+ clearRequest.setScrollIds(Arrays.asList(splitScrollIds(bodyScrollIds)));
+ }
} else {
// NOTE: if rest request with xcontent body has request parameters, these parameters does not override xcontent value
clearRequest.setScrollIds(null);
- try (XContentParser parser = request.contentOrSourceParamParser()) {
- buildFromContent(parser, clearRequest);
+ try {
+ buildFromContent(xContentParser, clearRequest);
} catch (IOException e) {
throw new IllegalArgumentException("Failed to parse request body", e);
}
}
- }
+ }));
- return channel -> client.clearScroll(clearRequest, new RestStatusToXContentListener<ClearScrollResponse>(channel));
+ return channel -> client.clearScroll(clearRequest, new RestStatusToXContentListener<>(channel));
+ }
+
+ @Override
+ public boolean supportsPlainText() {
+ return true;
}
- public static String[] splitScrollIds(String scrollIds) {
+ private static String[] splitScrollIds(String scrollIds) {
if (scrollIds == null) {
return Strings.EMPTY_ARRAY;
}
diff --git a/core/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java b/core/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java
index 567823a6e2..a803958618 100644
--- a/core/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java
+++ b/core/src/main/java/org/elasticsearch/rest/action/search/RestMultiSearchAction.java
@@ -26,10 +26,11 @@ import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
+import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContent;
-import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestController;
@@ -93,7 +94,7 @@ public class RestMultiSearchAction extends BaseRestHandler {
}
/**
- * Parses a multi-line {@link RestRequest} body, instanciating a {@link SearchRequest} for each line and applying the given consumer.
+ * Parses a multi-line {@link RestRequest} body, instantiating a {@link SearchRequest} for each line and applying the given consumer.
*/
public static void parseMultiLineRequest(RestRequest request, IndicesOptions indicesOptions, boolean allowExplicitIndex,
BiConsumer<SearchRequest, XContentParser> consumer) throws IOException {
@@ -103,9 +104,10 @@ public class RestMultiSearchAction extends BaseRestHandler {
String searchType = request.param("search_type");
String routing = request.param("routing");
- final BytesReference data = request.contentOrSourceParam();
+ final Tuple<XContentType, BytesReference> sourceTuple = request.contentOrSourceParam();
+ final XContent xContent = sourceTuple.v1().xContent();
+ final BytesReference data = sourceTuple.v2();
- XContent xContent = XContentFactory.xContent(data);
int from = 0;
int length = data.length();
byte marker = xContent.streamSeparator();
@@ -176,7 +178,7 @@ public class RestMultiSearchAction extends BaseRestHandler {
break;
}
BytesReference bytes = data.slice(from, nextMarker - from);
- try (XContentParser parser = XContentFactory.xContent(bytes).createParser(request.getXContentRegistry(), bytes)) {
+ try (XContentParser parser = xContent.createParser(request.getXContentRegistry(), bytes)) {
consumer.accept(searchRequest, parser);
}
// move pointers
diff --git a/core/src/main/java/org/elasticsearch/rest/action/search/RestSearchScrollAction.java b/core/src/main/java/org/elasticsearch/rest/action/search/RestSearchScrollAction.java
index 87a6f464cc..2a60fc6317 100644
--- a/core/src/main/java/org/elasticsearch/rest/action/search/RestSearchScrollAction.java
+++ b/core/src/main/java/org/elasticsearch/rest/action/search/RestSearchScrollAction.java
@@ -24,7 +24,6 @@ import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
-import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestController;
@@ -58,25 +57,33 @@ public class RestSearchScrollAction extends BaseRestHandler {
searchScrollRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll")));
}
- BytesReference body = request.contentOrSourceParam();
- if (body.length() > 0) {
- if (XContentFactory.xContentType(body) == null) {
- if (scrollId == null) {
- scrollId = body.utf8ToString();
- searchScrollRequest.scrollId(scrollId);
+ request.withContentOrSourceParamParserOrNull(xContentParser -> {
+ if (xContentParser == null) {
+ if (request.hasContent()) {
+ // TODO: why do we accept this plain text value? maybe we can just use the scroll params?
+ BytesReference body = request.getContentOrSourceParamOnly();
+ if (scrollId == null) {
+ String bodyScrollId = body.utf8ToString();
+ searchScrollRequest.scrollId(bodyScrollId);
+ }
}
} else {
// NOTE: if rest request with xcontent body has request parameters, these parameters override xcontent values
- try (XContentParser parser = request.contentOrSourceParamParser()) {
- buildFromContent(parser, searchScrollRequest);
+ try {
+ buildFromContent(xContentParser, searchScrollRequest);
} catch (IOException e) {
throw new IllegalArgumentException("Failed to parse request body", e);
}
}
- }
+ });
return channel -> client.searchScroll(searchScrollRequest, new RestStatusToXContentListener<>(channel));
}
+ @Override
+ public boolean supportsPlainText() {
+ return true;
+ }
+
public static void buildFromContent(XContentParser parser, SearchScrollRequest searchScrollRequest) throws IOException {
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
throw new IllegalArgumentException("Malformed content, must start with an object");