Search in sources :

Example 61 with NodeClient

use of org.opensearch.client.node.NodeClient 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 62 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class RestDeletePipelineAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
    DeletePipelineRequest request = new DeletePipelineRequest(restRequest.param("id"));
    request.masterNodeTimeout(restRequest.paramAsTime("cluster_manager_timeout", request.masterNodeTimeout()));
    parseDeprecatedMasterTimeoutParameter(request, restRequest, deprecationLogger, getName());
    request.timeout(restRequest.paramAsTime("timeout", request.timeout()));
    return channel -> client.admin().cluster().deletePipeline(request, new RestToXContentListener<>(channel));
}
Also used : DeletePipelineRequest(org.opensearch.action.ingest.DeletePipelineRequest) DeprecationLogger(org.opensearch.common.logging.DeprecationLogger) DeletePipelineRequest(org.opensearch.action.ingest.DeletePipelineRequest) DELETE(org.opensearch.rest.RestRequest.Method.DELETE) List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Collections.singletonList(java.util.Collections.singletonList)

Example 63 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class RestSimulatePipelineAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
    Tuple<XContentType, BytesReference> sourceTuple = restRequest.contentOrSourceParam();
    SimulatePipelineRequest request = new SimulatePipelineRequest(sourceTuple.v2(), sourceTuple.v1());
    request.setId(restRequest.param("id"));
    request.setVerbose(restRequest.paramAsBoolean("verbose", false));
    return channel -> client.admin().cluster().simulatePipeline(request, new RestToXContentListener<>(channel));
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) POST(org.opensearch.rest.RestRequest.Method.POST) NodeClient(org.opensearch.client.node.NodeClient) BytesReference(org.opensearch.common.bytes.BytesReference) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) Tuple(org.opensearch.common.collect.Tuple) SimulatePipelineRequest(org.opensearch.action.ingest.SimulatePipelineRequest) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) XContentType(org.opensearch.common.xcontent.XContentType) BaseRestHandler(org.opensearch.rest.BaseRestHandler) XContentType(org.opensearch.common.xcontent.XContentType) SimulatePipelineRequest(org.opensearch.action.ingest.SimulatePipelineRequest)

Example 64 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class RestFielddataAction method doCatRequest.

@Override
protected RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    final NodesStatsRequest nodesStatsRequest = new NodesStatsRequest("data:true");
    nodesStatsRequest.clear();
    nodesStatsRequest.indices(true);
    String[] fields = request.paramAsStringArray("fields", null);
    nodesStatsRequest.indices().fieldDataFields(fields == null ? new String[] { "*" } : fields);
    return channel -> client.admin().cluster().nodesStats(nodesStatsRequest, new RestResponseListener<NodesStatsResponse>(channel) {

        @Override
        public RestResponse buildResponse(NodesStatsResponse nodeStatses) throws Exception {
            return RestTable.buildResponse(buildTable(request, nodeStatses), channel);
        }
    });
}
Also used : NodesStatsRequest(org.opensearch.action.admin.cluster.node.stats.NodesStatsRequest) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Table(org.opensearch.common.Table) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) RestResponse(org.opensearch.rest.RestResponse) List(java.util.List) Arrays.asList(java.util.Arrays.asList) NodesStatsResponse(org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse) NodeStats(org.opensearch.action.admin.cluster.node.stats.NodeStats) NodesStatsRequest(org.opensearch.action.admin.cluster.node.stats.NodesStatsRequest) ObjectLongCursor(com.carrotsearch.hppc.cursors.ObjectLongCursor) RestResponseListener(org.opensearch.rest.action.RestResponseListener) NodesStatsResponse(org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse) RestResponse(org.opensearch.rest.RestResponse)

Example 65 with NodeClient

use of org.opensearch.client.node.NodeClient in project OpenSearch by opensearch-project.

the class AbstractBaseReindexRestHandler method doPrepareRequest.

protected RestChannelConsumer doPrepareRequest(RestRequest request, NodeClient client, boolean includeCreated, boolean includeUpdated) throws IOException {
    // Build the internal request
    Request internal = setCommonOptions(request, buildRequest(request, client.getNamedWriteableRegistry()));
    // Executes the request and waits for completion
    if (request.paramAsBoolean("wait_for_completion", true)) {
        Map<String, String> params = new HashMap<>();
        params.put(BulkByScrollTask.Status.INCLUDE_CREATED, Boolean.toString(includeCreated));
        params.put(BulkByScrollTask.Status.INCLUDE_UPDATED, Boolean.toString(includeUpdated));
        return channel -> client.executeLocally(action, internal, new BulkIndexByScrollResponseContentListener(channel, params));
    } else {
        internal.setShouldStoreResult(true);
    }
    /*
         * Let's try and validate before forking so the user gets some error. The
         * task can't totally validate until it starts but this is better than
         * nothing.
         */
    ActionRequestValidationException validationException = internal.validate();
    if (validationException != null) {
        throw validationException;
    }
    return sendTask(client.getLocalNodeId(), client.executeLocally(action, internal, LoggingTaskListener.instance()));
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) Task(org.opensearch.tasks.Task) RestStatus(org.opensearch.rest.RestStatus) BytesRestResponse(org.opensearch.rest.BytesRestResponse) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) LoggingTaskListener(org.opensearch.tasks.LoggingTaskListener) Map(java.util.Map) ActionType(org.opensearch.action.ActionType) BaseRestHandler(org.opensearch.rest.BaseRestHandler) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) HashMap(java.util.HashMap) RestRequest(org.opensearch.rest.RestRequest)

Aggregations

NodeClient (org.opensearch.client.node.NodeClient)183 RestRequest (org.opensearch.rest.RestRequest)142 List (java.util.List)140 IOException (java.io.IOException)138 BaseRestHandler (org.opensearch.rest.BaseRestHandler)111 Arrays.asList (java.util.Arrays.asList)76 Collections.unmodifiableList (java.util.Collections.unmodifiableList)76 RestToXContentListener (org.opensearch.rest.action.RestToXContentListener)70 Strings (org.opensearch.common.Strings)66 GET (org.opensearch.rest.RestRequest.Method.GET)64 DeprecationLogger (org.opensearch.common.logging.DeprecationLogger)59 POST (org.opensearch.rest.RestRequest.Method.POST)47 RestResponse (org.opensearch.rest.RestResponse)39 IndicesOptions (org.opensearch.action.support.IndicesOptions)35 Settings (org.opensearch.common.settings.Settings)34 Set (java.util.Set)31 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)31 BytesRestResponse (org.opensearch.rest.BytesRestResponse)29 Collections (java.util.Collections)28 Collections.singletonList (java.util.Collections.singletonList)28