Java Code Examples for org.elasticsearch.client.node.NodeClient#execute()

The following examples show how to use org.elasticsearch.client.node.NodeClient#execute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestFeatureStoreCaches.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
private RestChannelConsumer clearCache(RestRequest request, NodeClient client) {
    String storeName = indexName(request);
    ClearCachesAction.ClearCachesNodesRequest cacheRequest = new ClearCachesAction.ClearCachesNodesRequest();
    cacheRequest.clearStore(storeName);
    return (channel) -> client.execute(ClearCachesAction.INSTANCE, cacheRequest,
        new RestBuilderListener<ClearCachesNodesResponse>(channel) {
            @Override
            public RestResponse buildResponse(ClearCachesNodesResponse clearCachesNodesResponse,
                                              XContentBuilder builder) throws Exception {
                builder.startObject()
                        .field("acknowledged", true);
                builder.endObject();
                return new BytesRestResponse(OK, builder);
            }
        }
    );
}
 
Example 2
Source File: RestLangdetectAction.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    final LangdetectRequest langdetectRequest = new LangdetectRequest();
    langdetectRequest.setText(request.param("text"));
    langdetectRequest.setProfile(request.param("profile", ""));
    withContent(request, parser -> {
        if (parser != null) {
            XContentParser.Token token;
            while ((token = parser.nextToken()) != null) {
                if (token == XContentParser.Token.VALUE_STRING) {
                    if ("text".equals(parser.currentName())) {
                        langdetectRequest.setText(parser.text());
                    } else if ("profile".equals(parser.currentName())) {
                        langdetectRequest.setProfile(parser.text());
                    }
                }
            }
        }
    });
    return channel -> client.execute(LangdetectAction.INSTANCE, langdetectRequest,
            new RestStatusToXContentListener<>(channel));
}
 
Example 3
Source File: RestPrometheusMetricsAction.java    From elasticsearch-prometheus-exporter with Apache License 2.0 5 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
    if (logger.isTraceEnabled()) {
        String remoteAddress = NetworkAddress.format(request.getHttpChannel().getRemoteAddress());
        logger.trace(String.format(Locale.ENGLISH, "Received request for Prometheus metrics from %s",
                remoteAddress));
    }

    NodePrometheusMetricsRequest metricsRequest = new NodePrometheusMetricsRequest();

    return channel -> client.execute(INSTANCE, metricsRequest,
            new RestResponseListener<NodePrometheusMetricsResponse>(channel) {

                @Override
                public RestResponse buildResponse(NodePrometheusMetricsResponse response) throws Exception {
            String clusterName = response.getClusterHealth().getClusterName();
            String nodeName = response.getNodeStats().getNode().getName();
            String nodeId = response.getNodeStats().getNode().getId();
            if (logger.isTraceEnabled()) {
                logger.trace("Prepare new Prometheus metric collector for: [{}], [{}], [{}]", clusterName, nodeId,
                        nodeName);
            }
            PrometheusMetricsCatalog catalog = new PrometheusMetricsCatalog(clusterName, nodeName, nodeId, "es_");
            PrometheusMetricsCollector collector = new PrometheusMetricsCollector(
                    catalog,
                    prometheusSettings.getPrometheusIndices(),
                    prometheusSettings.getPrometheusClusterSettings());
            collector.registerMetrics();
            collector.updateMetrics(response.getClusterHealth(), response.getNodeStats(), response.getIndicesStats(),
                    response.getClusterStatsData());
            return new BytesRestResponse(RestStatus.OK, collector.getCatalog().toTextFormat());
        }
    });
}
 
Example 4
Source File: RestISBNFormatterAction.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    final String value = request.param("value");
    final ISBNFormatRequest isbnFormatRequest = new ISBNFormatRequest().setValue(value);
    return channel -> client.execute(ISBNFormatAction.INSTANCE, isbnFormatRequest,
                new RestStatusToXContentListener<>(channel));
}
 
Example 5
Source File: RestExecuteAnomalyDetectorAction.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    if (!EnabledSetting.isADPluginEnabled()) {
        throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG);
    }
    AnomalyDetectorExecutionInput input = getAnomalyDetectorExecutionInput(request);
    return channel -> {
        String rawPath = request.rawPath();
        String error = validateAdExecutionInput(input);
        if (StringUtils.isNotBlank(error)) {
            channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, error));
            return;
        }

        if (rawPath.endsWith(PREVIEW)) {
            if (input.getDetector() != null) {
                error = validateDetector(input.getDetector());
                if (StringUtils.isNotBlank(error)) {
                    channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, error));
                    return;
                }
                anomalyDetectorRunner
                    .executeDetector(
                        input.getDetector(),
                        input.getPeriodStart(),
                        input.getPeriodEnd(),
                        getPreviewDetectorActionListener(channel, input.getDetector())
                    );
            } else {
                preivewAnomalyDetector(client, channel, input);
            }
        } else if (rawPath.endsWith(RUN)) {
            AnomalyResultRequest getRequest = new AnomalyResultRequest(
                input.getDetectorId(),
                input.getPeriodStart().toEpochMilli(),
                input.getPeriodEnd().toEpochMilli()
            );
            client.execute(AnomalyResultAction.INSTANCE, getRequest, new RestToXContentListener<>(channel));
        }
    };
}
 
Example 6
Source File: RestFeatureStoreCaches.java    From elasticsearch-learning-to-rank with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private RestChannelConsumer getStats(NodeClient client) {
    return (channel) -> client.execute(CachesStatsAction.INSTANCE, new CachesStatsAction.CachesStatsNodesRequest(),
    new NodesResponseRestListener(channel));
}