Search in sources :

Example 1 with RestStatusToXContentListener

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

the class RestSearchTemplateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    // Creates the search request with all required params
    SearchRequest searchRequest = new SearchRequest();
    RestSearchAction.parseSearchRequest(searchRequest, request, null, client.getNamedWriteableRegistry(), size -> searchRequest.source().size(size));
    // Creates the search template request
    SearchTemplateRequest searchTemplateRequest;
    try (XContentParser parser = request.contentOrSourceParamParser()) {
        searchTemplateRequest = SearchTemplateRequest.fromXContent(parser);
    }
    searchTemplateRequest.setRequest(searchRequest);
    return channel -> client.execute(SearchTemplateAction.INSTANCE, searchTemplateRequest, new RestStatusToXContentListener<>(channel));
}
Also used : POST(org.opensearch.rest.RestRequest.Method.POST) Arrays(java.util.Arrays) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Set(java.util.Set) IOException(java.io.IOException) XContentParser(org.opensearch.common.xcontent.XContentParser) RestSearchAction(org.opensearch.rest.action.search.RestSearchAction) HashSet(java.util.HashSet) List(java.util.List) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) Arrays.asList(java.util.Arrays.asList) SearchRequest(org.opensearch.action.search.SearchRequest) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Collections(java.util.Collections) SearchRequest(org.opensearch.action.search.SearchRequest) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with RestStatusToXContentListener

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

the class RestGetStoredScriptAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, NodeClient client) throws IOException {
    String id = request.param("id");
    GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id);
    getRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getRequest.masterNodeTimeout()));
    return channel -> client.admin().cluster().getStoredScript(getRequest, new RestStatusToXContentListener<>(channel));
}
Also used : List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) IOException(java.io.IOException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Collections.singletonList(java.util.Collections.singletonList) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)

Example 3 with RestStatusToXContentListener

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

the class RestDeleteAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    DeleteRequest deleteRequest = new DeleteRequest(request.param("index"), request.param("id"));
    deleteRequest.routing(request.param("routing"));
    deleteRequest.timeout(request.paramAsTime("timeout", DeleteRequest.DEFAULT_TIMEOUT));
    deleteRequest.setRefreshPolicy(request.param("refresh"));
    deleteRequest.version(RestActions.parseVersion(request));
    deleteRequest.versionType(VersionType.fromString(request.param("version_type"), deleteRequest.versionType()));
    deleteRequest.setIfSeqNo(request.paramAsLong("if_seq_no", deleteRequest.ifSeqNo()));
    deleteRequest.setIfPrimaryTerm(request.paramAsLong("if_primary_term", deleteRequest.ifPrimaryTerm()));
    String waitForActiveShards = request.param("wait_for_active_shards");
    if (waitForActiveShards != null) {
        deleteRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards));
    }
    return channel -> client.delete(deleteRequest, new RestStatusToXContentListener<>(channel));
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) DELETE(org.opensearch.rest.RestRequest.Method.DELETE) VersionType(org.opensearch.index.VersionType) List(java.util.List) RestActions(org.opensearch.rest.action.RestActions) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) DeleteRequest(org.opensearch.action.delete.DeleteRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest)

Example 4 with RestStatusToXContentListener

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

the class RestUpdateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    UpdateRequest updateRequest;
    updateRequest = new UpdateRequest(request.param("index"), request.param("id"));
    updateRequest.routing(request.param("routing"));
    updateRequest.timeout(request.paramAsTime("timeout", updateRequest.timeout()));
    updateRequest.setRefreshPolicy(request.param("refresh"));
    String waitForActiveShards = request.param("wait_for_active_shards");
    if (waitForActiveShards != null) {
        updateRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards));
    }
    updateRequest.docAsUpsert(request.paramAsBoolean("doc_as_upsert", updateRequest.docAsUpsert()));
    FetchSourceContext fetchSourceContext = FetchSourceContext.parseFromRestRequest(request);
    if (fetchSourceContext != null) {
        updateRequest.fetchSource(fetchSourceContext);
    }
    updateRequest.retryOnConflict(request.paramAsInt("retry_on_conflict", updateRequest.retryOnConflict()));
    if (request.hasParam("version") || request.hasParam("version_type")) {
        final ActionRequestValidationException versioningError = new ActionRequestValidationException();
        versioningError.addValidationError("internal versioning can not be used for optimistic concurrency control. " + "Please use `if_seq_no` and `if_primary_term` instead");
        throw versioningError;
    }
    updateRequest.setIfSeqNo(request.paramAsLong("if_seq_no", updateRequest.ifSeqNo()));
    updateRequest.setIfPrimaryTerm(request.paramAsLong("if_primary_term", updateRequest.ifPrimaryTerm()));
    updateRequest.setRequireAlias(request.paramAsBoolean(DocWriteRequest.REQUIRE_ALIAS, updateRequest.isRequireAlias()));
    request.applyContentParser(parser -> {
        updateRequest.fromXContent(parser);
        IndexRequest upsertRequest = updateRequest.upsertRequest();
        if (upsertRequest != null) {
            upsertRequest.routing(request.param("routing"));
            upsertRequest.version(RestActions.parseVersion(request));
            upsertRequest.versionType(VersionType.fromString(request.param("version_type"), upsertRequest.versionType()));
        }
        IndexRequest doc = updateRequest.doc();
        if (doc != null) {
            doc.routing(request.param("routing"));
            doc.version(RestActions.parseVersion(request));
            doc.versionType(VersionType.fromString(request.param("version_type"), doc.versionType()));
        }
    });
    return channel -> client.update(updateRequest, new RestStatusToXContentListener<>(channel, r -> r.getLocation(updateRequest.routing())));
}
Also used : POST(org.opensearch.rest.RestRequest.Method.POST) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) RestRequest(org.opensearch.rest.RestRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) IOException(java.io.IOException) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) VersionType(org.opensearch.index.VersionType) List(java.util.List) RestActions(org.opensearch.rest.action.RestActions) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) Arrays.asList(java.util.Arrays.asList) UpdateRequest(org.opensearch.action.update.UpdateRequest) IndexRequest(org.opensearch.action.index.IndexRequest) BaseRestHandler(org.opensearch.rest.BaseRestHandler) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) UpdateRequest(org.opensearch.action.update.UpdateRequest) IndexRequest(org.opensearch.action.index.IndexRequest)

Example 5 with RestStatusToXContentListener

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

the class RestSearchAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    /*
         * We have to pull out the call to `source().size(size)` because
         * _update_by_query and _delete_by_query uses this same parsing
         * path but sets a different variable when it sees the `size`
         * url parameter.
         *
         * Note that we can't use `searchRequest.source()::size` because
         * `searchRequest.source()` is null right now. We don't have to
         * guard against it being null in the IntConsumer because it can't
         * be null later. If that is confusing to you then you are in good
         * company.
         */
    IntConsumer setSize = size -> searchRequest.source().size(size);
    request.withContentOrSourceParamParserOrNull(parser -> parseSearchRequest(searchRequest, request, parser, client.getNamedWriteableRegistry(), setSize));
    return channel -> {
        RestCancellableNodeClient cancelClient = new RestCancellableNodeClient(client, request.getHttpChannel());
        cancelClient.execute(SearchAction.INSTANCE, searchRequest, new RestStatusToXContentListener<>(channel));
    };
}
Also used : SearchAction(org.opensearch.action.search.SearchAction) POST(org.opensearch.rest.RestRequest.Method.POST) Arrays(java.util.Arrays) Collections.unmodifiableList(java.util.Collections.unmodifiableList) TimeValue.parseTimeValue(org.opensearch.common.unit.TimeValue.parseTimeValue) SearchContext(org.opensearch.search.internal.SearchContext) IntConsumer(java.util.function.IntConsumer) IndicesOptions(org.opensearch.action.support.IndicesOptions) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) Strings(org.opensearch.common.Strings) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) XContentParser(org.opensearch.common.xcontent.XContentParser) HashSet(java.util.HashSet) SortOrder(org.opensearch.search.sort.SortOrder) SuggestMode(org.opensearch.search.suggest.term.TermSuggestionBuilder.SuggestMode) Booleans(org.opensearch.common.Booleans) Arrays.asList(java.util.Arrays.asList) SearchRequest(org.opensearch.action.search.SearchRequest) RestCancellableNodeClient(org.opensearch.rest.action.RestCancellableNodeClient) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Scroll(org.opensearch.search.Scroll) SearchContextId(org.opensearch.action.search.SearchContextId) NodeClient(org.opensearch.client.node.NodeClient) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) ValidateActions.addValidationError(org.opensearch.action.ValidateActions.addValidationError) ExceptionsHelper(org.opensearch.ExceptionsHelper) Set(java.util.Set) SuggestBuilders.termSuggestion(org.opensearch.search.suggest.SuggestBuilders.termSuggestion) IOException(java.io.IOException) List(java.util.List) RestActions(org.opensearch.rest.action.RestActions) SuggestBuilder(org.opensearch.search.suggest.SuggestBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) StoredFieldsContext(org.opensearch.search.fetch.StoredFieldsContext) Collections(java.util.Collections) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) SearchRequest(org.opensearch.action.search.SearchRequest) RestCancellableNodeClient(org.opensearch.rest.action.RestCancellableNodeClient) IntConsumer(java.util.function.IntConsumer) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener)

Aggregations

IOException (java.io.IOException)11 List (java.util.List)11 NodeClient (org.opensearch.client.node.NodeClient)11 BaseRestHandler (org.opensearch.rest.BaseRestHandler)11 RestRequest (org.opensearch.rest.RestRequest)11 RestStatusToXContentListener (org.opensearch.rest.action.RestStatusToXContentListener)11 Arrays.asList (java.util.Arrays.asList)10 Collections.unmodifiableList (java.util.Collections.unmodifiableList)10 POST (org.opensearch.rest.RestRequest.Method.POST)7 GET (org.opensearch.rest.RestRequest.Method.GET)6 RestActions (org.opensearch.rest.action.RestActions)5 ActiveShardCount (org.opensearch.action.support.ActiveShardCount)4 Strings (org.opensearch.common.Strings)4 FetchSourceContext (org.opensearch.search.fetch.subphase.FetchSourceContext)4 Collections (java.util.Collections)3 Set (java.util.Set)3 DocWriteRequest (org.opensearch.action.DocWriteRequest)3 VersionType (org.opensearch.index.VersionType)3 Arrays (java.util.Arrays)2 HashSet (java.util.HashSet)2