Java Code Examples for org.elasticsearch.rest.action.support.RestActions#parseQuerySource()

The following examples show how to use org.elasticsearch.rest.action.support.RestActions#parseQuerySource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestCountAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void doRequest(final RestRequest request, final RestChannel channel, final Client client) {
    String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    CountRequest countRequest = new CountRequest(indices);
    String source = request.param("source");
    if (source != null) {
        countRequest.source(source);
    } else {
        QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
        if (querySourceBuilder != null) {
            countRequest.source(querySourceBuilder);
        }
    }

    client.search(countRequest.toSearchRequest(), new RestResponseListener<SearchResponse>(channel) {
        @Override
        public RestResponse buildResponse(SearchResponse countResponse) throws Exception {
            return RestTable.buildResponse(buildTable(request, countResponse), channel);
        }
    });
}
 
Example 2
Source File: RestExistsAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    final ExistsRequest existsRequest = new ExistsRequest(Strings.splitStringByCommaToArray(request.param("index")));
    existsRequest.indicesOptions(IndicesOptions.fromRequest(request, existsRequest.indicesOptions()));
    if (RestActions.hasBodyContent(request)) {
        existsRequest.source(RestActions.getRestContent(request));
    } else {
        QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
        if (querySourceBuilder != null) {
            existsRequest.source(querySourceBuilder);
        }
    }
    existsRequest.routing(request.param("routing"));
    existsRequest.minScore(request.paramAsFloat("min_score", DEFAULT_MIN_SCORE));
    existsRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
    existsRequest.preference(request.param("preference"));

    client.exists(existsRequest, new RestBuilderListener<ExistsResponse>(channel) {
        @Override
        public RestResponse buildResponse(ExistsResponse response, XContentBuilder builder) throws Exception {
            RestStatus status = response.exists() ? OK : NOT_FOUND;
            builder.startObject();
            builder.field("exists", response.exists());
            builder.endObject();
            return new BytesRestResponse(status, builder);
        }
    });
}
 
Example 3
Source File: RestCountAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    CountRequest countRequest = new CountRequest(Strings.splitStringByCommaToArray(request.param("index")));
    countRequest.indicesOptions(IndicesOptions.fromRequest(request, countRequest.indicesOptions()));
    if (RestActions.hasBodyContent(request)) {
        countRequest.source(RestActions.getRestContent(request));
    } else {
        QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
        if (querySourceBuilder != null) {
            countRequest.source(querySourceBuilder);
        }
    }
    countRequest.routing(request.param("routing"));
    countRequest.minScore(request.paramAsFloat("min_score", DEFAULT_MIN_SCORE));
    countRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
    countRequest.preference(request.param("preference"));

    final int terminateAfter = request.paramAsInt("terminate_after", DEFAULT_TERMINATE_AFTER);
    if (terminateAfter < 0) {
        throw new IllegalArgumentException("terminateAfter must be > 0");
    } else if (terminateAfter > 0) {
        countRequest.terminateAfter(terminateAfter);
    }
    client.search(countRequest.toSearchRequest(), new RestBuilderListener<SearchResponse>(channel) {
        @Override
        public RestResponse buildResponse(SearchResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            if (terminateAfter != DEFAULT_TERMINATE_AFTER) {
                builder.field("terminated_early", response.isTerminatedEarly());
            }
            builder.field("count", response.getHits().totalHits());
            buildBroadcastShardsHeader(builder, request, response.getTotalShards(), response.getSuccessfulShards(),
                    response.getFailedShards(), response.getShardFailures());

            builder.endObject();
            return new BytesRestResponse(response.status(), builder);
        }
    });
}
 
Example 4
Source File: RestValidateQueryAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(Strings.splitStringByCommaToArray(request.param("index")));
    validateQueryRequest.indicesOptions(IndicesOptions.fromRequest(request, validateQueryRequest.indicesOptions()));
    if (RestActions.hasBodyContent(request)) {
        validateQueryRequest.source(RestActions.getRestContent(request));
    } else {
        QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
        if (querySourceBuilder != null) {
            validateQueryRequest.source(querySourceBuilder);
        }
    }
    validateQueryRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
    if (request.paramAsBoolean("explain", false)) {
        validateQueryRequest.explain(true);
    } else {
        validateQueryRequest.explain(false);
    }
    if (request.paramAsBoolean("rewrite", false)) {
        validateQueryRequest.rewrite(true);
    } else {
        validateQueryRequest.rewrite(false);
    }

    client.admin().indices().validateQuery(validateQueryRequest, new RestBuilderListener<ValidateQueryResponse>(channel) {
        @Override
        public RestResponse buildResponse(ValidateQueryResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            builder.field("valid", response.isValid());

            buildBroadcastShardsHeader(builder, request, response);

            if (response.getQueryExplanation() != null && !response.getQueryExplanation().isEmpty()) {
                builder.startArray("explanations");
                for (QueryExplanation explanation : response.getQueryExplanation()) {
                    builder.startObject();
                    if (explanation.getIndex() != null) {
                        builder.field("index", explanation.getIndex(), XContentBuilder.FieldCaseConversion.NONE);
                    }
                    builder.field("valid", explanation.isValid());
                    if (explanation.getError() != null) {
                        builder.field("error", explanation.getError());
                    }
                    if (explanation.getExplanation() != null) {
                        builder.field("explanation", explanation.getExplanation());
                    }
                    builder.endObject();
                }
                builder.endArray();
            }
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}