Search in sources :

Example 11 with RestBuilderListener

use of org.opensearch.rest.action.RestBuilderListener in project OpenSearch by opensearch-project.

the class RestCountAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    SearchRequest countRequest = new SearchRequest(Strings.splitStringByCommaToArray(request.param("index")));
    countRequest.indicesOptions(IndicesOptions.fromRequest(request, countRequest.indicesOptions()));
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().size(0).trackTotalHits(true);
    countRequest.source(searchSourceBuilder);
    request.withContentOrSourceParamParserOrNull(parser -> {
        if (parser == null) {
            QueryBuilder queryBuilder = RestActions.urlParamsToQueryBuilder(request);
            if (queryBuilder != null) {
                searchSourceBuilder.query(queryBuilder);
            }
        } else {
            searchSourceBuilder.query(RestActions.getQueryContent(parser));
        }
    });
    countRequest.routing(request.param("routing"));
    float minScore = request.paramAsFloat("min_score", -1f);
    if (minScore != -1f) {
        searchSourceBuilder.minScore(minScore);
    }
    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) {
        searchSourceBuilder.terminateAfter(terminateAfter);
    }
    return channel -> client.search(countRequest, 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().getTotalHits().value);
            buildBroadcastShardsHeader(builder, request, response.getTotalShards(), response.getSuccessfulShards(), 0, response.getFailedShards(), response.getShardFailures());
            builder.endObject();
            return new BytesRestResponse(response.status(), builder);
        }
    });
}
Also used : POST(org.opensearch.rest.RestRequest.Method.POST) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) DEFAULT_TERMINATE_AFTER(org.opensearch.search.internal.SearchContext.DEFAULT_TERMINATE_AFTER) IOException(java.io.IOException) IndicesOptions(org.opensearch.action.support.IndicesOptions) RestActions.buildBroadcastShardsHeader(org.opensearch.rest.action.RestActions.buildBroadcastShardsHeader) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) Strings(org.opensearch.common.Strings) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) List(java.util.List) RestActions(org.opensearch.rest.action.RestActions) QueryBuilder(org.opensearch.index.query.QueryBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) Arrays.asList(java.util.Arrays.asList) SearchRequest(org.opensearch.action.search.SearchRequest) SearchResponse(org.opensearch.action.search.SearchResponse) BaseRestHandler(org.opensearch.rest.BaseRestHandler) SearchRequest(org.opensearch.action.search.SearchRequest) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) QueryBuilder(org.opensearch.index.query.QueryBuilder) IOException(java.io.IOException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchResponse(org.opensearch.action.search.SearchResponse) BytesRestResponse(org.opensearch.rest.BytesRestResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

IOException (java.io.IOException)11 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)11 BytesRestResponse (org.opensearch.rest.BytesRestResponse)11 RestResponse (org.opensearch.rest.RestResponse)11 RestBuilderListener (org.opensearch.rest.action.RestBuilderListener)11 List (java.util.List)10 NodeClient (org.opensearch.client.node.NodeClient)10 BaseRestHandler (org.opensearch.rest.BaseRestHandler)10 RestRequest (org.opensearch.rest.RestRequest)10 GET (org.opensearch.rest.RestRequest.Method.GET)10 Arrays.asList (java.util.Arrays.asList)9 Collections.unmodifiableList (java.util.Collections.unmodifiableList)9 Strings (org.opensearch.common.Strings)8 RestStatus (org.opensearch.rest.RestStatus)8 IndicesOptions (org.opensearch.action.support.IndicesOptions)6 Set (java.util.Set)5 ToXContent (org.opensearch.common.xcontent.ToXContent)4 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 Locale (java.util.Locale)2