Search in sources :

Example 6 with RestStatusToXContentListener

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

the class RestGetPipelineAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
    GetPipelineRequest request = new GetPipelineRequest(Strings.splitStringByCommaToArray(restRequest.param("id")));
    request.masterNodeTimeout(restRequest.paramAsTime("master_timeout", request.masterNodeTimeout()));
    return channel -> client.admin().cluster().getPipeline(request, new RestStatusToXContentListener<>(channel));
}
Also used : GetPipelineRequest(org.opensearch.action.ingest.GetPipelineRequest) List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) Arrays.asList(java.util.Arrays.asList) IOException(java.io.IOException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Strings(org.opensearch.common.Strings) GetPipelineRequest(org.opensearch.action.ingest.GetPipelineRequest)

Example 7 with RestStatusToXContentListener

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

the class RestClearScrollAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    String scrollIds = request.param("scroll_id");
    ClearScrollRequest clearRequest = new ClearScrollRequest();
    clearRequest.setScrollIds(asList(Strings.splitStringByCommaToArray(scrollIds)));
    request.withContentOrSourceParamParserOrNull((xContentParser -> {
        if (xContentParser != null) {
            // NOTE: if rest request with xcontent body has request parameters, values parsed from request body have the precedence
            try {
                clearRequest.fromXContent(xContentParser);
            } catch (IOException e) {
                throw new IllegalArgumentException("Failed to parse request body", e);
            }
        }
    }));
    return channel -> client.clearScroll(clearRequest, new RestStatusToXContentListener<>(channel));
}
Also used : DELETE(org.opensearch.rest.RestRequest.Method.DELETE) List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) RestRequest(org.opensearch.rest.RestRequest) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) Arrays.asList(java.util.Arrays.asList) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) IOException(java.io.IOException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Strings(org.opensearch.common.Strings) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) IOException(java.io.IOException)

Example 8 with RestStatusToXContentListener

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

the class RestExplainAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ExplainRequest explainRequest = new ExplainRequest(request.param("index"), request.param("id"));
    explainRequest.parent(request.param("parent"));
    explainRequest.routing(request.param("routing"));
    explainRequest.preference(request.param("preference"));
    String queryString = request.param("q");
    request.withContentOrSourceParamParserOrNull(parser -> {
        if (parser != null) {
            explainRequest.query(RestActions.getQueryContent(parser));
        } else if (queryString != null) {
            QueryBuilder query = RestActions.urlParamsToQueryBuilder(request);
            explainRequest.query(query);
        }
    });
    if (request.param("fields") != null) {
        throw new IllegalArgumentException("The parameter [fields] is no longer supported, " + "please use [stored_fields] to retrieve stored fields");
    }
    String sField = request.param("stored_fields");
    if (sField != null) {
        String[] sFields = Strings.splitStringByCommaToArray(sField);
        if (sFields != null) {
            explainRequest.storedFields(sFields);
        }
    }
    explainRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request));
    return channel -> client.explain(explainRequest, new RestStatusToXContentListener<>(channel));
}
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) IOException(java.io.IOException) Strings(org.opensearch.common.Strings) List(java.util.List) RestActions(org.opensearch.rest.action.RestActions) QueryBuilder(org.opensearch.index.query.QueryBuilder) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) Arrays.asList(java.util.Arrays.asList) ExplainRequest(org.opensearch.action.explain.ExplainRequest) BaseRestHandler(org.opensearch.rest.BaseRestHandler) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) QueryBuilder(org.opensearch.index.query.QueryBuilder) ExplainRequest(org.opensearch.action.explain.ExplainRequest)

Example 9 with RestStatusToXContentListener

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

the class RestBulkAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    BulkRequest bulkRequest = Requests.bulkRequest();
    String defaultIndex = request.param("index");
    String defaultRouting = request.param("routing");
    FetchSourceContext defaultFetchSourceContext = FetchSourceContext.parseFromRestRequest(request);
    String defaultPipeline = request.param("pipeline");
    String waitForActiveShards = request.param("wait_for_active_shards");
    if (waitForActiveShards != null) {
        bulkRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards));
    }
    Boolean defaultRequireAlias = request.paramAsBoolean(DocWriteRequest.REQUIRE_ALIAS, null);
    bulkRequest.timeout(request.paramAsTime("timeout", BulkShardRequest.DEFAULT_TIMEOUT));
    bulkRequest.setRefreshPolicy(request.param("refresh"));
    bulkRequest.add(request.requiredContent(), defaultIndex, defaultRouting, defaultFetchSourceContext, defaultPipeline, defaultRequireAlias, allowExplicitIndex, request.getXContentType());
    return channel -> client.bulk(bulkRequest, new RestStatusToXContentListener<>(channel));
}
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) BulkShardRequest(org.opensearch.action.bulk.BulkShardRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) Requests(org.opensearch.client.Requests) List(java.util.List) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) PUT(org.opensearch.rest.RestRequest.Method.PUT) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) BulkRequest(org.opensearch.action.bulk.BulkRequest)

Example 10 with RestStatusToXContentListener

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

the class RestIndexAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    IndexRequest indexRequest = new IndexRequest(request.param("index"));
    indexRequest.id(request.param("id"));
    indexRequest.routing(request.param("routing"));
    indexRequest.setPipeline(request.param("pipeline"));
    indexRequest.source(request.requiredContent(), request.getXContentType());
    indexRequest.timeout(request.paramAsTime("timeout", IndexRequest.DEFAULT_TIMEOUT));
    indexRequest.setRefreshPolicy(request.param("refresh"));
    indexRequest.version(RestActions.parseVersion(request));
    indexRequest.versionType(VersionType.fromString(request.param("version_type"), indexRequest.versionType()));
    indexRequest.setIfSeqNo(request.paramAsLong("if_seq_no", indexRequest.ifSeqNo()));
    indexRequest.setIfPrimaryTerm(request.paramAsLong("if_primary_term", indexRequest.ifPrimaryTerm()));
    indexRequest.setRequireAlias(request.paramAsBoolean(DocWriteRequest.REQUIRE_ALIAS, indexRequest.isRequireAlias()));
    String sOpType = request.param("op_type");
    String waitForActiveShards = request.param("wait_for_active_shards");
    if (waitForActiveShards != null) {
        indexRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards));
    }
    if (sOpType != null) {
        indexRequest.opType(sOpType);
    }
    return channel -> client.index(indexRequest, new RestStatusToXContentListener<>(channel, r -> r.getLocation(indexRequest.routing())));
}
Also used : POST(org.opensearch.rest.RestRequest.Method.POST) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) 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) Supplier(java.util.function.Supplier) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) VersionType(org.opensearch.index.VersionType) List(java.util.List) RestActions(org.opensearch.rest.action.RestActions) LegacyESVersion(org.opensearch.LegacyESVersion) RestStatusToXContentListener(org.opensearch.rest.action.RestStatusToXContentListener) Locale(java.util.Locale) Arrays.asList(java.util.Arrays.asList) IndexRequest(org.opensearch.action.index.IndexRequest) BaseRestHandler(org.opensearch.rest.BaseRestHandler) PUT(org.opensearch.rest.RestRequest.Method.PUT) IndexRequest(org.opensearch.action.index.IndexRequest)

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