Parse query from deleteByQuery request body

This commit is contained in:
Colin Goodheart-Smithe 2015-09-23 22:06:35 +01:00
parent 6d307a3cfd
commit 3c9e69e15f
1 changed files with 22 additions and 4 deletions

View File

@ -26,7 +26,11 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
@ -34,6 +38,8 @@ import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestToXContentListener;
import java.io.IOException;
import static org.elasticsearch.action.deletebyquery.DeleteByQueryAction.INSTANCE;
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
@ -42,15 +48,19 @@ import static org.elasticsearch.rest.RestRequest.Method.DELETE;
*/
public class RestDeleteByQueryAction extends BaseRestHandler {
private IndicesQueriesRegistry indicesQueriesRegistry;
@Inject
public RestDeleteByQueryAction(Settings settings, RestController controller, Client client) {
public RestDeleteByQueryAction(Settings settings, RestController controller, Client client,
IndicesQueriesRegistry indicesQueriesRegistry) {
super(settings, controller, client);
this.indicesQueriesRegistry = indicesQueriesRegistry;
controller.registerHandler(DELETE, "/{index}/_query", this);
controller.registerHandler(DELETE, "/{index}/{type}/_query", this);
}
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws IOException {
DeleteByQueryRequest delete = new DeleteByQueryRequest(Strings.splitStringByCommaToArray(request.param("index")));
delete.indicesOptions(IndicesOptions.fromRequest(request, delete.indicesOptions()));
delete.routing(request.param("routing"));
@ -58,11 +68,19 @@ public class RestDeleteByQueryAction extends BaseRestHandler {
delete.timeout(request.paramAsTime("timeout", null));
}
if (request.hasContent()) {
// delete.source(request.content()); NORELEASE parse request.content() into a queryBuilder
XContentParser requestParser = XContentFactory.xContent(request.content()).createParser(request.content());
QueryParseContext context = new QueryParseContext(indicesQueriesRegistry);
context.reset(requestParser);
final QueryBuilder<?> builder = context.parseInnerQueryBuilder();
delete.query(builder);
} else {
String source = request.param("source");
if (source != null) {
// delete.source(source); NORELEASE parse source into a queryBuilder
XContentParser requestParser = XContentFactory.xContent(source).createParser(source);
QueryParseContext context = new QueryParseContext(indicesQueriesRegistry);
context.reset(requestParser);
final QueryBuilder<?> builder = context.parseInnerQueryBuilder();
delete.query(builder);
} else {
QueryBuilder<?> queryBuilder = RestActions.parseQuerySource(request);
if (queryBuilder != null) {