Search in sources :

Example 1 with RestStatusToXContentListener

use of org.elasticsearch.rest.action.RestStatusToXContentListener in project elasticsearch by elastic.

the class RestClusterHealthAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClusterHealthRequest clusterHealthRequest = clusterHealthRequest(Strings.splitStringByCommaToArray(request.param("index")));
    clusterHealthRequest.local(request.paramAsBoolean("local", clusterHealthRequest.local()));
    clusterHealthRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterHealthRequest.masterNodeTimeout()));
    clusterHealthRequest.timeout(request.paramAsTime("timeout", clusterHealthRequest.timeout()));
    String waitForStatus = request.param("wait_for_status");
    if (waitForStatus != null) {
        clusterHealthRequest.waitForStatus(ClusterHealthStatus.valueOf(waitForStatus.toUpperCase(Locale.ROOT)));
    }
    clusterHealthRequest.waitForNoRelocatingShards(request.paramAsBoolean("wait_for_no_relocating_shards", clusterHealthRequest.waitForNoRelocatingShards()));
    if (request.hasParam("wait_for_relocating_shards")) {
        // wait_for_relocating_shards has been removed in favor of wait_for_no_relocating_shards
        throw new IllegalArgumentException("wait_for_relocating_shards has been removed, " + "use wait_for_no_relocating_shards [true/false] instead");
    }
    String waitForActiveShards = request.param("wait_for_active_shards");
    if (waitForActiveShards != null) {
        clusterHealthRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards));
    }
    clusterHealthRequest.waitForNodes(request.param("wait_for_nodes", clusterHealthRequest.waitForNodes()));
    if (request.param("wait_for_events") != null) {
        clusterHealthRequest.waitForEvents(Priority.valueOf(request.param("wait_for_events").toUpperCase(Locale.ROOT)));
    }
    return channel -> client.admin().cluster().health(clusterHealthRequest, new RestStatusToXContentListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Priority(org.elasticsearch.common.Priority) Requests.clusterHealthRequest(org.elasticsearch.client.Requests.clusterHealthRequest) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) Set(java.util.Set) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) Settings(org.elasticsearch.common.settings.Settings) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) Locale(java.util.Locale) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) Collections(java.util.Collections) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)

Example 2 with RestStatusToXContentListener

use of org.elasticsearch.rest.action.RestStatusToXContentListener in project elasticsearch by elastic.

the class RestSearchAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    request.withContentOrSourceParamParserOrNull(parser -> parseSearchRequest(searchRequest, request, parser));
    return channel -> client.search(searchRequest, new RestStatusToXContentListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Arrays(java.util.Arrays) GET(org.elasticsearch.rest.RestRequest.Method.GET) SearchContext(org.elasticsearch.search.internal.SearchContext) SearchRequest(org.elasticsearch.action.search.SearchRequest) Strings(org.elasticsearch.common.Strings) RestActions(org.elasticsearch.rest.action.RestActions) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) NodeClient(org.elasticsearch.client.node.NodeClient) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) Set(java.util.Set) Scroll(org.elasticsearch.search.Scroll) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) SuggestBuilder(org.elasticsearch.search.suggest.SuggestBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) StoredFieldsContext(org.elasticsearch.search.fetch.StoredFieldsContext) SuggestBuilders.termSuggestion(org.elasticsearch.search.suggest.SuggestBuilders.termSuggestion) SuggestMode(org.elasticsearch.search.suggest.term.TermSuggestionBuilder.SuggestMode) SortOrder(org.elasticsearch.search.sort.SortOrder) TimeValue.parseTimeValue(org.elasticsearch.common.unit.TimeValue.parseTimeValue) Collections(java.util.Collections) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) SearchRequest(org.elasticsearch.action.search.SearchRequest)

Example 3 with RestStatusToXContentListener

use of org.elasticsearch.rest.action.RestStatusToXContentListener in project elasticsearch by elastic.

the class RestSearchScrollAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    String scrollId = request.param("scroll_id");
    SearchScrollRequest searchScrollRequest = new SearchScrollRequest();
    searchScrollRequest.scrollId(scrollId);
    String scroll = request.param("scroll");
    if (scroll != null) {
        searchScrollRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll")));
    }
    request.withContentOrSourceParamParserOrNull(xContentParser -> {
        if (xContentParser != null) {
            try {
                buildFromContent(xContentParser, searchScrollRequest);
            } catch (IOException e) {
                throw new IllegalArgumentException("Failed to parse request body", e);
            }
        }
    });
    return channel -> client.searchScroll(searchScrollRequest, new RestStatusToXContentListener<>(channel));
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) Scroll(org.elasticsearch.search.Scroll) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) XContentParser(org.elasticsearch.common.xcontent.XContentParser) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) TimeValue(org.elasticsearch.common.unit.TimeValue) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) TimeValue.parseTimeValue(org.elasticsearch.common.unit.TimeValue.parseTimeValue) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) Scroll(org.elasticsearch.search.Scroll) IOException(java.io.IOException) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest)

Example 4 with RestStatusToXContentListener

use of org.elasticsearch.rest.action.RestStatusToXContentListener 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 5 with RestStatusToXContentListener

use of org.elasticsearch.rest.action.RestStatusToXContentListener 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)

Aggregations

IOException (java.io.IOException)10 NodeClient (org.elasticsearch.client.node.NodeClient)10 Settings (org.elasticsearch.common.settings.Settings)10 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)10 RestController (org.elasticsearch.rest.RestController)10 RestRequest (org.elasticsearch.rest.RestRequest)10 RestStatusToXContentListener (org.elasticsearch.rest.action.RestStatusToXContentListener)10 Strings (org.elasticsearch.common.Strings)6 POST (org.elasticsearch.rest.RestRequest.Method.POST)6 ActiveShardCount (org.elasticsearch.action.support.ActiveShardCount)5 XContentParser (org.elasticsearch.common.xcontent.XContentParser)4 RestActions (org.elasticsearch.rest.action.RestActions)4 Collections (java.util.Collections)3 Set (java.util.Set)3 VersionType (org.elasticsearch.index.VersionType)3 GET (org.elasticsearch.rest.RestRequest.Method.GET)3 FetchSourceContext (org.elasticsearch.search.fetch.subphase.FetchSourceContext)3 Arrays (java.util.Arrays)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)2