Search in sources :

Example 51 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class RestGetAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final GetRequest getRequest = new GetRequest(request.param("index"), request.param("type"), request.param("id"));
    getRequest.operationThreaded(true);
    getRequest.refresh(request.paramAsBoolean("refresh", getRequest.refresh()));
    getRequest.routing(request.param("routing"));
    getRequest.parent(request.param("parent"));
    getRequest.preference(request.param("preference"));
    getRequest.realtime(request.paramAsBoolean("realtime", getRequest.realtime()));
    if (request.param("fields") != null) {
        throw new IllegalArgumentException("the parameter [fields] is no longer supported, " + "please use [stored_fields] to retrieve stored fields or [_source] to load the field from _source");
    }
    final String fieldsParam = request.param("stored_fields");
    if (fieldsParam != null) {
        final String[] fields = Strings.splitStringByCommaToArray(fieldsParam);
        if (fields != null) {
            getRequest.storedFields(fields);
        }
    }
    getRequest.version(RestActions.parseVersion(request));
    getRequest.versionType(VersionType.fromString(request.param("version_type"), getRequest.versionType()));
    getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request));
    return channel -> client.get(getRequest, new RestToXContentListener<GetResponse>(channel) {

        @Override
        protected RestStatus getStatus(final GetResponse response) {
            return response.isExists() ? OK : NOT_FOUND;
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GetResponse(org.elasticsearch.action.get.GetResponse) GetRequest(org.elasticsearch.action.get.GetRequest) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestToXContentListener(org.elasticsearch.rest.action.RestToXContentListener) IOException(java.io.IOException) NOT_FOUND(org.elasticsearch.rest.RestStatus.NOT_FOUND) RestController(org.elasticsearch.rest.RestController) VersionType(org.elasticsearch.index.VersionType) Strings(org.elasticsearch.common.Strings) RestActions(org.elasticsearch.rest.action.RestActions) Settings(org.elasticsearch.common.settings.Settings) RestStatus(org.elasticsearch.rest.RestStatus) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) HEAD(org.elasticsearch.rest.RestRequest.Method.HEAD) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) RestStatus(org.elasticsearch.rest.RestStatus) GetRequest(org.elasticsearch.action.get.GetRequest) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 52 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class RestIndexAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    IndexRequest indexRequest = new IndexRequest(request.param("index"), request.param("type"), request.param("id"));
    indexRequest.routing(request.param("routing"));
    indexRequest.parent(request.param("parent"));
    indexRequest.setPipeline(request.param("pipeline"));
    indexRequest.source(request.content(), 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()));
    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 : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) PUT(org.elasticsearch.rest.RestRequest.Method.PUT) VersionType(org.elasticsearch.index.VersionType) RestActions(org.elasticsearch.rest.action.RestActions) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) POST(org.elasticsearch.rest.RestRequest.Method.POST) IndexRequest(org.elasticsearch.action.index.IndexRequest) Settings(org.elasticsearch.common.settings.Settings) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 53 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class RestMultiTermVectorsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    MultiTermVectorsRequest multiTermVectorsRequest = new MultiTermVectorsRequest();
    TermVectorsRequest template = new TermVectorsRequest();
    template.index(request.param("index"));
    template.type(request.param("type"));
    RestTermVectorsAction.readURIParameters(template, request);
    multiTermVectorsRequest.ids(Strings.commaDelimitedListToStringArray(request.param("ids")));
    request.withContentOrSourceParamParserOrNull(p -> multiTermVectorsRequest.add(template, p));
    return channel -> client.multiTermVectors(multiTermVectorsRequest, new RestToXContentListener<MultiTermVectorsResponse>(channel));
}
Also used : MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestToXContentListener(org.elasticsearch.rest.action.RestToXContentListener) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) Strings(org.elasticsearch.common.Strings) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) MultiTermVectorsRequest(org.elasticsearch.action.termvectors.MultiTermVectorsRequest) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse)

Example 54 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class RestUpdateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    UpdateRequest updateRequest = new UpdateRequest(request.param("index"), request.param("type"), request.param("id"));
    updateRequest.routing(request.param("routing"));
    updateRequest.parent(request.param("parent"));
    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);
    String sField = request.param("fields");
    if (sField != null && fetchSourceContext != null) {
        throw new IllegalArgumentException("[fields] and [_source] cannot be used in the same request");
    }
    if (sField != null) {
        DEPRECATION_LOGGER.deprecated("Deprecated field [fields] used, expected [_source] instead");
        String[] sFields = Strings.splitStringByCommaToArray(sField);
        updateRequest.fields(sFields);
    } else if (fetchSourceContext != null) {
        updateRequest.fetchSource(fetchSourceContext);
    }
    updateRequest.retryOnConflict(request.paramAsInt("retry_on_conflict", updateRequest.retryOnConflict()));
    updateRequest.version(RestActions.parseVersion(request));
    updateRequest.versionType(VersionType.fromString(request.param("version_type"), updateRequest.versionType()));
    request.applyContentParser(parser -> {
        updateRequest.fromXContent(parser);
        IndexRequest upsertRequest = updateRequest.upsertRequest();
        if (upsertRequest != null) {
            upsertRequest.routing(request.param("routing"));
            upsertRequest.parent(request.param("parent"));
            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.parent(request.param("parent"));
            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 : Loggers(org.elasticsearch.common.logging.Loggers) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) DeprecationLogger(org.elasticsearch.common.logging.DeprecationLogger) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) VersionType(org.elasticsearch.index.VersionType) Strings(org.elasticsearch.common.Strings) RestActions(org.elasticsearch.rest.action.RestActions) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) POST(org.elasticsearch.rest.RestRequest.Method.POST) IndexRequest(org.elasticsearch.action.index.IndexRequest) Settings(org.elasticsearch.common.settings.Settings) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 55 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

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("master_timeout", request.masterNodeTimeout()));
    request.timeout(restRequest.paramAsTime("timeout", request.timeout()));
    return channel -> client.admin().cluster().deletePipeline(request, new AcknowledgedRestListener<>(channel));
}
Also used : DeletePipelineRequest(org.elasticsearch.action.ingest.DeletePipelineRequest) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Settings(org.elasticsearch.common.settings.Settings) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) DeletePipelineRequest(org.elasticsearch.action.ingest.DeletePipelineRequest) RestController(org.elasticsearch.rest.RestController) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener)

Aggregations

NodeClient (org.elasticsearch.client.node.NodeClient)125 Settings (org.elasticsearch.common.settings.Settings)115 RestRequest (org.elasticsearch.rest.RestRequest)108 RestController (org.elasticsearch.rest.RestController)107 IOException (java.io.IOException)103 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)88 Strings (org.elasticsearch.common.Strings)61 GET (org.elasticsearch.rest.RestRequest.Method.GET)57 RestResponse (org.elasticsearch.rest.RestResponse)50 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)36 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)35 POST (org.elasticsearch.rest.RestRequest.Method.POST)34 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)30 RestBuilderListener (org.elasticsearch.rest.action.RestBuilderListener)27 Set (java.util.Set)24 Table (org.elasticsearch.common.Table)24 OK (org.elasticsearch.rest.RestStatus.OK)23 AcknowledgedRestListener (org.elasticsearch.rest.action.AcknowledgedRestListener)23 RestResponseListener (org.elasticsearch.rest.action.RestResponseListener)20 Collections (java.util.Collections)18