more fixes from review

This commit is contained in:
Colin Goodheart-Smithe 2015-10-08 11:33:45 +01:00
parent 42d7bfdf12
commit 7aaba20420
3 changed files with 16 additions and 26 deletions

View File

@ -19,17 +19,15 @@
package org.elasticsearch.rest.action.cat; package org.elasticsearch.rest.action.cat;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.count.CountRequest; import org.elasticsearch.action.count.CountRequest;
import org.elasticsearch.action.count.CountResponse; import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.support.QuerySourceBuilder; import org.elasticsearch.action.support.QuerySourceBuilder;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.Table; import org.elasticsearch.common.Table;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; 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.QueryBuilder;
import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.indices.query.IndicesQueriesRegistry; import org.elasticsearch.indices.query.IndicesQueriesRegistry;
@ -43,7 +41,6 @@ import org.elasticsearch.rest.action.support.RestTable;
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestRequest.Method.GET;
@ -73,15 +70,7 @@ public class RestCountAction extends AbstractCatAction {
String source = request.param("source"); String source = request.param("source");
if (source != null) { if (source != null) {
QueryParseContext context = new QueryParseContext(indicesQueriesRegistry); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry);
try (XContentParser requestParser = XContentFactory.xContent(source).createParser(source)) { countRequest.query(RestActions.getQueryContent(new BytesArray(source), context));
context.reset(requestParser);
final QueryBuilder<?> builder = context.parseInnerQueryBuilder();
countRequest.query(builder);
} catch (IOException e) {
throw new ElasticsearchException("failed to parse source", e);
} finally {
context.reset(null);
}
} else { } else {
QueryBuilder<?> queryBuilder = RestActions.urlParamsToQueryBuilder(request); QueryBuilder<?> queryBuilder = RestActions.urlParamsToQueryBuilder(request);
if (queryBuilder != null) { if (queryBuilder != null) {

View File

@ -19,7 +19,6 @@
package org.elasticsearch.rest.action.count; package org.elasticsearch.rest.action.count;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.count.CountRequest; import org.elasticsearch.action.count.CountRequest;
import org.elasticsearch.action.count.CountResponse; import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.IndicesOptions;
@ -29,8 +28,6 @@ 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;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.indices.query.IndicesQueriesRegistry; import org.elasticsearch.indices.query.IndicesQueriesRegistry;
@ -43,8 +40,6 @@ import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.support.RestActions; import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestBuilderListener; import org.elasticsearch.rest.action.support.RestBuilderListener;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestRequest.Method.POST; import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.action.support.RestActions.buildBroadcastShardsHeader; import static org.elasticsearch.rest.action.support.RestActions.buildBroadcastShardsHeader;
@ -75,14 +70,8 @@ public class RestCountAction extends BaseRestHandler {
countRequest.indicesOptions(IndicesOptions.fromRequest(request, countRequest.indicesOptions())); countRequest.indicesOptions(IndicesOptions.fromRequest(request, countRequest.indicesOptions()));
if (RestActions.hasBodyContent(request)) { if (RestActions.hasBodyContent(request)) {
BytesReference restContent = RestActions.getRestContent(request); BytesReference restContent = RestActions.getRestContent(request);
try (XContentParser requestParser = XContentFactory.xContent(restContent).createParser(restContent)) { QueryParseContext context = new QueryParseContext(indicesQueriesRegistry);
QueryParseContext context = new QueryParseContext(indicesQueriesRegistry); countRequest.query(RestActions.getQueryContent(restContent, context));
context.reset(requestParser);
final QueryBuilder<?> builder = context.parseInnerQueryBuilder();
countRequest.query(builder);
} catch (IOException e) {
throw new ElasticsearchException("failed to parse source", e);
}
} else { } else {
QueryBuilder<?> queryBuilder = RestActions.urlParamsToQueryBuilder(request); QueryBuilder<?> queryBuilder = RestActions.urlParamsToQueryBuilder(request);
if (queryBuilder != null) { if (queryBuilder != null) {

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.support; package org.elasticsearch.rest.action.support;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ShardOperationFailedException; import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.BroadcastResponse; import org.elasticsearch.action.support.broadcast.BroadcastResponse;
@ -139,6 +140,17 @@ public class RestActions {
return content; return content;
} }
public static QueryBuilder<?> getQueryContent(BytesReference source, QueryParseContext context) {
try (XContentParser requestParser = XContentFactory.xContent(source).createParser(source)) {
context.reset(requestParser);
return context.parseInnerQueryBuilder();
} catch (IOException e) {
throw new ElasticsearchException("failed to parse source", e);
} finally {
context.reset(null);
}
}
/** /**
* guesses the content type from either payload or source parameter * guesses the content type from either payload or source parameter
* @param request Rest request * @param request Rest request