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

This commit is contained in:
Jun Ohtani 2015-04-22 23:55:15 +09:00
parent 1118965aa5
commit 7f08ab301d
1 changed files with 24 additions and 4 deletions

View File

@ -23,6 +23,7 @@ import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest; import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -68,16 +69,16 @@ public class RestSearchScrollAction extends BaseRestHandler {
searchScrollRequest.scroll(new Scroll(parseTimeValue(scroll, null))); searchScrollRequest.scroll(new Scroll(parseTimeValue(scroll, null)));
} }
if (request.hasContent()) { if (request.hasContent() || request.hasParam("source")) {
XContentType type = XContentFactory.xContentType(request.content()); XContentType type = contentType(request);
if (type == null) { if (type == null) {
if (scrollId == null) { if (scrollId == null) {
scrollId = RestActions.getRestContent(request).toUtf8(); scrollId = bodyContent(request).toUtf8();
searchScrollRequest.scrollId(scrollId); searchScrollRequest.scrollId(scrollId);
} }
} else { } else {
// NOTE: if rest request with xcontent body has request parameters, these parameters override xcontent values // NOTE: if rest request with xcontent body has request parameters, these parameters override xcontent values
buildFromContent(request.content(), searchScrollRequest); buildFromContent(bodyContent(request), searchScrollRequest);
} }
} }
client.searchScroll(searchScrollRequest, new RestStatusToXContentListener<SearchResponse>(channel)); client.searchScroll(searchScrollRequest, new RestStatusToXContentListener<SearchResponse>(channel));
@ -107,4 +108,23 @@ public class RestSearchScrollAction 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");
}
} }