org.elasticsearch.client.node.NodeClient Java Examples

The following examples show how to use org.elasticsearch.client.node.NodeClient. 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: AnomalyDetectorActionHandler.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
/**
 * Get detector job for update/delete AD job.
 * If AD job exist, will return error message; otherwise, execute function.
 *
 * @param clusterService ES cluster service
 * @param client ES node client
 * @param detectorId detector identifier
 * @param channel ES rest channel
 * @param function AD function
 */
public void getDetectorJob(
    ClusterService clusterService,
    NodeClient client,
    String detectorId,
    RestChannel channel,
    AnomalyDetectorFunction function
) {
    if (clusterService.state().metadata().indices().containsKey(ANOMALY_DETECTOR_JOB_INDEX)) {
        GetRequest request = new GetRequest(ANOMALY_DETECTOR_JOB_INDEX).id(detectorId);
        client.get(request, ActionListener.wrap(response -> onGetAdJobResponseForWrite(response, channel, function), exception -> {
            logger.error("Fail to get anomaly detector job: " + detectorId, exception);
            try {
                channel.sendResponse(new BytesRestResponse(channel, exception));
            } catch (IOException e) {
                logger.error("Fail to send exception" + detectorId, e);
            }
        }));
    } else {
        function.execute();
    }
}
 
Example #2
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 #3
Source File: RestSimpleFeatureStore.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
/**
 * Prepare the request for execution. Implementations should consume all request params before
 * returning the runnable for actual execution. Unconsumed params will immediately terminate
 * execution of the request. However, some params are only used in processing the response;
 * implementations can override {@link BaseRestHandler#responseParams()} to indicate such
 * params.
 *
 * @param request the request to execute
 * @param client  client for executing actions on the local node
 * @return the action to execute
 * @throws IOException if an I/O exception occurred parsing the request and preparing for
 *                     execution
 */
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    String indexName = indexName(request);
    if (request.method() == RestRequest.Method.PUT) {
        if (request.hasParam("store")) {
            IndexFeatureStore.validateFeatureStoreName(request.param("store"));
        }
        return createIndex(client, indexName);
    } else if (request.method() == RestRequest.Method.POST ) {
        if (request.hasParam("store")) {
            IndexFeatureStore.validateFeatureStoreName(request.param("store"));
        }
        throw new IllegalArgumentException("Updating a feature store is not yet supported.");
    } else if (request.method() == RestRequest.Method.DELETE) {
        return deleteIndex(client, indexName);
    } else {
        assert request.method() == RestRequest.Method.GET;
        // XXX: ambiguous api
        if (request.hasParam("store")) {
            return getStore(client, indexName);
        }
        return listStores(client);
    }
}
 
Example #4
Source File: HomeAction.java    From zentity with Apache License 2.0 6 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) {

    Properties props = ZentityPlugin.properties();
    Boolean pretty = restRequest.paramAsBoolean("pretty", false);

    return channel -> {
        XContentBuilder content = XContentFactory.jsonBuilder();
        if (pretty)
            content.prettyPrint();
        content.startObject();
        content.field("name", props.getProperty("name"));
        content.field("description", props.getProperty("description"));
        content.field("website", props.getProperty("zentity.website"));
        content.startObject("version");
        content.field("zentity", props.getProperty("zentity.version"));
        content.field("elasticsearch", props.getProperty("elasticsearch.version"));
        content.endObject();
        content.endObject();
        channel.sendResponse(new BytesRestResponse(RestStatus.OK, content));
    };
}
 
Example #5
Source File: RestGetAnomalyDetectorAction.java    From anomaly-detection with Apache License 2.0 6 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);
    }
    String detectorId = request.param(DETECTOR_ID);
    String typesStr = request.param(TYPE);
    String rawPath = request.rawPath();
    if (!Strings.isEmpty(typesStr) || rawPath.endsWith(PROFILE) || rawPath.endsWith(PROFILE + "/")) {
        boolean all = request.paramAsBoolean("_all", false);
        return channel -> profileRunner
            .profile(detectorId, getProfileActionListener(channel, detectorId), getProfilesToCollect(typesStr, all));
    } else {
        boolean returnJob = request.paramAsBoolean("job", false);
        MultiGetRequest.Item adItem = new MultiGetRequest.Item(ANOMALY_DETECTORS_INDEX, detectorId)
            .version(RestActions.parseVersion(request));
        MultiGetRequest multiGetRequest = new MultiGetRequest().add(adItem);
        if (returnJob) {
            MultiGetRequest.Item adJobItem = new MultiGetRequest.Item(ANOMALY_DETECTOR_JOB_INDEX, detectorId)
                .version(RestActions.parseVersion(request));
            multiGetRequest.add(adJobItem);
        }

        return channel -> client.multiGet(multiGetRequest, onMultiGetResponse(channel, returnJob, detectorId));
    }
}
 
Example #6
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 #7
Source File: Test.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
public static String sqlToEsQuery(String sql) throws Exception {
        Map actions = new HashMap();
        Settings settings = Settings.builder().build();
//        Client client = new NodeClient(settings, null, null, actions);
//        Settings.builder()
//                .put(ThreadContext.PREFIX + ".key1", "val1")
//                .put(ThreadContext.PREFIX + ".key2", "val 2")
//                .build();

        ThreadPool threadPool = new ThreadPool(settings);
        Client client = new NodeClient(settings, threadPool);
        SearchDao searchDao = new org.nlpcn.es4sql.SearchDao(client);
        try {
            return searchDao.explain(sql).explain().explain();
        } catch (Exception e) {
            throw e;
        }
    }
 
Example #8
Source File: IndexAnomalyDetectorJobActionHandler.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor function.
 *
 * @param clusterService          ClusterService
 * @param client                  ES node client that executes actions on the local node
 * @param channel                 ES channel used to construct bytes / builder based outputs, and send responses
 * @param anomalyDetectionIndices anomaly detector index manager
 * @param detectorId              detector identifier
 * @param seqNo                   sequence number of last modification
 * @param primaryTerm             primary term of last modification
 * @param refreshPolicy           refresh policy
 * @param requestTimeout          request time out configuration
 */
public IndexAnomalyDetectorJobActionHandler(
    ClusterService clusterService,
    NodeClient client,
    RestChannel channel,
    AnomalyDetectionIndices anomalyDetectionIndices,
    String detectorId,
    Long seqNo,
    Long primaryTerm,
    WriteRequest.RefreshPolicy refreshPolicy,
    TimeValue requestTimeout
) {
    super(client, channel);
    this.clusterService = clusterService;
    this.anomalyDetectionIndices = anomalyDetectionIndices;
    this.detectorId = detectorId;
    this.seqNo = seqNo;
    this.primaryTerm = primaryTerm;
    this.refreshPolicy = refreshPolicy;
    this.requestTimeout = requestTimeout;
}
 
Example #9
Source File: TransportSchemaUpdateAction.java    From crate with Apache License 2.0 6 votes vote down vote up
@Inject
public TransportSchemaUpdateAction(TransportService transportService,
                                   ClusterService clusterService,
                                   ThreadPool threadPool,
                                   IndexNameExpressionResolver indexNameExpressionResolver,
                                   NodeClient nodeClient,
                                   NamedXContentRegistry xContentRegistry) {
    super(
        "internal:crate:sql/ddl/schema_update",
        transportService,
        clusterService,
        threadPool,
        SchemaUpdateRequest::new,
        indexNameExpressionResolver
    );
    this.nodeClient = nodeClient;
    this.xContentRegistry = xContentRegistry;
}
 
Example #10
Source File: SetupAction.java    From zentity with Apache License 2.0 6 votes vote down vote up
/**
 * Create the .zentity-models index.
 *
 * @param client           The client that will communicate with Elasticsearch.
 * @param numberOfShards   The value of index.number_of_shards.
 * @param numberOfReplicas The value of index.number_of_replicas.
 * @return
 */
public static CreateIndexResponse createIndex(NodeClient client, int numberOfShards, int numberOfReplicas) {
    // Elasticsearch 7.0.0+ removes mapping types
    Properties props = ZentityPlugin.properties();
    if (props.getProperty("elasticsearch.version").compareTo("7.") >= 0) {
        return client.admin().indices().prepareCreate(ModelsAction.INDEX_NAME)
            .setSettings(Settings.builder()
                    .put("index.number_of_shards", numberOfShards)
                    .put("index.number_of_replicas", numberOfReplicas)
            )
            .addMapping("doc", INDEX_MAPPING, XContentType.JSON)
            .get();
    } else {
        return client.admin().indices().prepareCreate(ModelsAction.INDEX_NAME)
            .setSettings(Settings.builder()
                    .put("index.number_of_shards", numberOfShards)
                    .put("index.number_of_replicas", numberOfReplicas)
            )
            .addMapping("doc", INDEX_MAPPING_ELASTICSEARCH_6, XContentType.JSON)
            .get();
    }
}
 
Example #11
Source File: InternalClusterInfoService.java    From crate with Apache License 2.0 6 votes vote down vote up
public InternalClusterInfoService(Settings settings, ClusterService clusterService, ThreadPool threadPool, NodeClient client) {
    this.leastAvailableSpaceUsages = ImmutableOpenMap.of();
    this.mostAvailableSpaceUsages = ImmutableOpenMap.of();
    this.shardRoutingToDataPath = ImmutableOpenMap.of();
    this.shardSizes = ImmutableOpenMap.of();
    this.clusterService = clusterService;
    this.threadPool = threadPool;
    this.client = client;
    this.updateFrequency = INTERNAL_CLUSTER_INFO_UPDATE_INTERVAL_SETTING.get(settings);
    this.fetchTimeout = INTERNAL_CLUSTER_INFO_TIMEOUT_SETTING.get(settings);
    this.enabled = DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING.get(settings);
    ClusterSettings clusterSettings = clusterService.getClusterSettings();
    //clusterSettings.addSettingsUpdateConsumer(INTERNAL_CLUSTER_INFO_TIMEOUT_SETTING, this::setFetchTimeout);
    //clusterSettings.addSettingsUpdateConsumer(INTERNAL_CLUSTER_INFO_UPDATE_INTERVAL_SETTING, this::setUpdateFrequency);
    clusterSettings.addSettingsUpdateConsumer(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING, this::setEnabled);

    // Add InternalClusterInfoService to listen for Master changes
    this.clusterService.addLocalNodeMasterListener(this);
    // Add to listen for state changes (when nodes are added)
    this.clusterService.addListener(this);
}
 
Example #12
Source File: Netty4Plugin.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Supplier<HttpServerTransport>> getHttpTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays,
                                                                    CircuitBreakerService circuitBreakerService,
                                                                    NamedWriteableRegistry namedWriteableRegistry,
                                                                    NamedXContentRegistry xContentRegistry,
                                                                    NetworkService networkService,
                                                                    NodeClient nodeClient) {
    return Collections.singletonMap(
        NETTY_HTTP_TRANSPORT_NAME,
        () -> new Netty4HttpServerTransport(
            settings,
            networkService,
            bigArrays,
            threadPool,
            xContentRegistry,
            pipelineRegistry,
            nodeClient
        )
    );
}
 
Example #13
Source File: RestDeleteAnomalyDetectorAction.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
private void deleteAnomalyDetectorJobDoc(NodeClient client, String detectorId, RestChannel channel) {
    logger.info("Delete anomaly detector job {}", detectorId);
    DeleteRequest deleteRequest = new DeleteRequest(AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX, detectorId)
        .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    client.delete(deleteRequest, ActionListener.wrap(response -> {
        if (response.getResult() == DocWriteResponse.Result.DELETED || response.getResult() == DocWriteResponse.Result.NOT_FOUND) {
            deleteAnomalyDetectorDoc(client, detectorId, channel);
        } else {
            logger.error("Fail to delete anomaly detector job {}", detectorId);
        }
    }, exception -> {
        if (exception instanceof IndexNotFoundException) {
            deleteAnomalyDetectorDoc(client, detectorId, channel);
        } else {
            logger.error("Failed to delete anomaly detector job", exception);
            try {
                channel.sendResponse(new BytesRestResponse(channel, exception));
            } catch (IOException e) {
                logger.error("Failed to send response of delete anomaly detector job exception", e);
            }
        }
    }));
}
 
Example #14
Source File: RestDeleteAnomalyDetectorAction.java    From anomaly-detection with Apache License 2.0 6 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);
    }

    String detectorId = request.param(DETECTOR_ID);

    return channel -> {
        logger.info("Delete anomaly detector {}", detectorId);
        handler
            .getDetectorJob(
                clusterService,
                client,
                detectorId,
                channel,
                () -> deleteAnomalyDetectorJobDoc(client, detectorId, channel)
            );
    };
}
 
Example #15
Source File: AppendLuceneRestHandler.java    From ES-Fastloader with Apache License 2.0 6 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
    AppendLuceneRequest appendLuceneRequest = new AppendLuceneRequest();

    appendLuceneRequest.indexName = request.param(INDEX_NAME);
    appendLuceneRequest.uuid = request.param(UUID);
    appendLuceneRequest.shardId = Integer.valueOf(request.param(SHARD_ID));
    appendLuceneRequest.appendSegmentDirs = request.param(APPEND);
    appendLuceneRequest.primeKey = request.param(PRIMERKEY);

    // 跳转到AppendLuceneTransportAction.doExecute()
    return channel -> client.executeLocally(AppendLuceneAction.INSTANCE, appendLuceneRequest, new RestBuilderListener<AppendLuceneResponse>(channel) {
        @Override
        public RestResponse buildResponse(AppendLuceneResponse appendLuceneResponse, XContentBuilder builder) throws Exception {
            return new BytesRestResponse(RestStatus.OK, appendLuceneResponse.toXContent(builder, ToXContent.EMPTY_PARAMS));
        }
    });
}
 
Example #16
Source File: RangerSecurityRestFilter.java    From ranger with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final NodeClient client)
		throws Exception {
	// Now only support to get user from request,
	// it should work with other elasticsearch identity authentication plugins in fact.
	UsernamePasswordToken user = UsernamePasswordToken.parseToken(request);
	if (user == null) {
		throw new ElasticsearchStatusException("Error: User is null, the request requires user authentication.",
				RestStatus.UNAUTHORIZED);
	} else {
		if (LOG.isDebugEnabled()) {
			LOG.debug("Success to parse user[{}] from request[{}].", user, request);
		}
	}
	threadContext.putTransient(UsernamePasswordToken.USERNAME, user.getUsername());

	String clientIPAddress = RequestUtils.getClientIPAddress(request);
	if (StringUtils.isNotEmpty(clientIPAddress)) {
		threadContext.putTransient(RequestUtils.CLIENT_IP_ADDRESS, clientIPAddress);
	}

	this.restHandler.handleRequest(request, channel, client);
}
 
Example #17
Source File: IndexAnomalyDetectorActionHandler.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor function.
 *
 * @param settings                ES settings
 * @param clusterService          ClusterService
 * @param client                  ES node client that executes actions on the local node
 * @param channel                 ES channel used to construct bytes / builder based outputs, and send responses
 * @param anomalyDetectionIndices anomaly detector index manager
 * @param detectorId              detector identifier
 * @param seqNo                   sequence number of last modification
 * @param primaryTerm             primary term of last modification
 * @param refreshPolicy           refresh policy
 * @param anomalyDetector         anomaly detector instance
 * @param requestTimeout          request time out configuration
 */
public IndexAnomalyDetectorActionHandler(
    Settings settings,
    ClusterService clusterService,
    NodeClient client,
    RestChannel channel,
    AnomalyDetectionIndices anomalyDetectionIndices,
    String detectorId,
    Long seqNo,
    Long primaryTerm,
    WriteRequest.RefreshPolicy refreshPolicy,
    AnomalyDetector anomalyDetector,
    TimeValue requestTimeout,
    Integer maxAnomalyDetectors,
    Integer maxAnomalyFeatures
) {
    super(client, channel);
    this.clusterService = clusterService;
    this.anomalyDetectionIndices = anomalyDetectionIndices;
    this.detectorId = detectorId;
    this.seqNo = seqNo;
    this.primaryTerm = primaryTerm;
    this.refreshPolicy = refreshPolicy;
    this.anomalyDetector = anomalyDetector;
    this.requestTimeout = requestTimeout;
    this.maxAnomalyDetectors = maxAnomalyDetectors;
    this.maxAnomalyFeatures = maxAnomalyFeatures;
}
 
Example #18
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 #19
Source File: Netty4HttpServerTransport.java    From crate with Apache License 2.0 5 votes vote down vote up
protected HttpChannelHandler(Netty4HttpServerTransport transport,
                             NodeClient nodeClient,
                             Settings settings,
                             PipelineRegistry pipelineRegistry) {
    this.transport = transport;
    this.nodeClient = nodeClient;
    this.pipelineRegistry = pipelineRegistry;
    this.nodeName = NODE_NAME_SETTING.get(settings);
    this.home = PathUtils.get(PATH_HOME_SETTING.get(settings)).normalize();
}
 
Example #20
Source File: RestFeatureStoreCaches.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
    if (request.method() == RestRequest.Method.POST) {
        return clearCache(request, client);
    } else {
        return getStats(client);
    }
}
 
Example #21
Source File: Node.java    From crate with Apache License 2.0 5 votes vote down vote up
/** Constructs a ClusterInfoService which may be mocked for tests. */
protected ClusterInfoService newClusterInfoService(Settings settings,
                                                   ClusterService clusterService,
                                                   ThreadPool threadPool,
                                                   NodeClient client) {
    return new InternalClusterInfoService(settings, clusterService, threadPool, client);
}
 
Example #22
Source File: RestSimpleFeatureStore.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    String indexName = indexName(request);
    if (request.method() == RestRequest.Method.DELETE) {
        return delete(client, type, indexName, request);
    } else if (request.method() == RestRequest.Method.HEAD || request.method() == RestRequest.Method.GET) {
        return get(client, type, indexName, request);
    } else {
        return addOrUpdate(client, type, indexName, request);
    }
}
 
Example #23
Source File: RestDataAction.java    From elasticsearch-dataformat with Apache License 2.0 5 votes vote down vote up
public RestChannelConsumer prepareRequest(final RestRequest request,
        final NodeClient client) throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    request.withContentOrSourceParamParserOrNull(
            parser -> RestSearchAction.parseSearchRequest(searchRequest,
                    request, parser,
                    size -> searchRequest.source().size(size)));

    if (request.paramAsInt("size", -1) == -1) {
        searchRequest.source().size(100);
    }

    final String file = request.param("file");

    final long limitBytes;
    String limitParamStr = request.param("limit");
    if (Strings.isNullOrEmpty(limitParamStr)) {
        limitBytes = defaultLimit;
    } else {
        if (limitParamStr.endsWith("%")) {
            limitParamStr = limitParamStr.substring(0,
                    limitParamStr.length() - 1);
        }
        limitBytes = (long) (maxMemory
                * (Float.parseFloat(limitParamStr) / 100F));
    }

    final ContentType contentType = getContentType(request);
    if (contentType == null) {
        final String msg = "Unknown content type:"
                + request.header("Content-Type");
        throw new IllegalArgumentException(msg);
    }
    final DataContent dataContent = contentType.dataContent(client,
            request);

    return channel -> client.search(searchRequest, new SearchResponseListener(
            channel, file, limitBytes, dataContent));
}
 
Example #24
Source File: RestSimpleFeatureStore.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
RestChannelConsumer getStore(NodeClient client, String indexName) {
    return (channel) -> client.admin().indices().prepareExists(indexName)
            .execute(new RestBuilderListener<IndicesExistsResponse>(channel) {
                @Override
                public RestResponse buildResponse(IndicesExistsResponse indicesExistsResponse,
                                                  XContentBuilder builder) throws Exception {
                    builder.startObject()
                            .field("exists", indicesExistsResponse.isExists())
                            .endObject()
                            .close();
                    return new BytesRestResponse(indicesExistsResponse.isExists() ? RestStatus.OK : RestStatus.NOT_FOUND,
                            builder);
                }
            });
}
 
Example #25
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 #26
Source File: RestSimpleFeatureStore.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
RestChannelConsumer addOrUpdate(NodeClient client, String type, String indexName, RestRequest request) throws IOException {
    assert SUPPORTED_TYPES.contains(type);
    String routing = request.param("routing");
    if (!request.hasContentOrSourceParam()) {
        throw new IllegalArgumentException("Missing content or source param.");
    }
    String name = request.param("name");
    AutoDetectParser parserState = new AutoDetectParser(name);
    request.applyContentParser(parserState::parse);
    StorableElement elt = parserState.element;
    if (!type.equals(elt.type())) {
        throw new IllegalArgumentException("Excepted a [" + type + "] but encountered [" + elt.type() + "]");
    }

    // Validation happens here when parsing the stored element.
    if (!elt.name().equals(name)) {
        throw new IllegalArgumentException("Name mismatch, send request with [" + elt.name() + "] but [" + name + "] used in the URL");
    }
    if (request.method() == RestRequest.Method.POST && !elt.updatable()) {
        try {
            throw new IllegalArgumentException("Element of type [" + elt.type() + "] are not updatable, " +
                    "please create a new one instead.");
        } catch (IllegalArgumentException iae) {
            return (channel) -> channel.sendResponse(new BytesRestResponse(channel, RestStatus.METHOD_NOT_ALLOWED, iae));
        }
    }
    FeatureStoreAction.FeatureStoreRequestBuilder builder = new FeatureStoreAction.FeatureStoreRequestBuilder(
        client, FeatureStoreAction.INSTANCE);
    if (request.method() == RestRequest.Method.PUT) {
        builder.request().setAction(FeatureStoreAction.FeatureStoreRequest.Action.CREATE);
    } else {
        builder.request().setAction(FeatureStoreAction.FeatureStoreRequest.Action.UPDATE);
    }
    builder.request().setStorableElement(elt);
    builder.request().setRouting(routing);
    builder.request().setStore(indexName);
    builder.request().setValidation(parserState.validation);
    return (channel) -> builder.execute(new RestStatusToXContentListener<>(channel, (r) -> r.getResponse().getLocation(routing)));
}
 
Example #27
Source File: RestSimpleFeatureStore.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
RestChannelConsumer delete(NodeClient client, String type, String indexName, RestRequest request) {
    assert SUPPORTED_TYPES.contains(type);
    String name = request.param("name");
    String id = generateId(type, name);
    String routing = request.param("routing");
    return (channel) ->  {
        RestStatusToXContentListener<DeleteResponse> restR = new RestStatusToXContentListener<>(channel, (r) -> r.getLocation(routing));
        client.prepareDelete(indexName, ES_TYPE, id)
                .setRouting(routing)
                .execute(ActionListener.wrap((deleteResponse) -> {
                        // wrap the response so we can send another request to clear the cache
                        // usually we send only one transport request from the rest layer
                        // it's still unclear which direction we should take (thick or thin REST layer?)
                        ClearCachesAction.ClearCachesNodesRequest clearCache = new ClearCachesAction.ClearCachesNodesRequest();
                        switch (type) {
                        case StoredFeature.TYPE:
                            clearCache.clearFeature(indexName, name);
                            break;
                        case StoredFeatureSet.TYPE:
                            clearCache.clearFeatureSet(indexName, name);
                            break;
                        case StoredLtrModel.TYPE:
                            clearCache.clearModel(indexName, name);
                            break;
                        }
                        client.execute(ClearCachesAction.INSTANCE, clearCache, ActionListener.wrap(
                                (r) -> restR.onResponse(deleteResponse),
                                // Is it good to fail the whole request if cache invalidation failed?
                                restR::onFailure
                        ));
                    },
                    restR::onFailure
                ));
    };
}
 
Example #28
Source File: RestSimpleFeatureStore.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
RestChannelConsumer get(NodeClient client, String type, String indexName, RestRequest request) {
    assert SUPPORTED_TYPES.contains(type);
    String name = request.param("name");
    String routing = request.param("routing");
    String id = generateId(type, name);
    return (channel) -> client.prepareGet(indexName, ES_TYPE, id)
            .setRouting(routing)
            .execute(new RestToXContentListener<GetResponse>(channel) {
                @Override
                protected RestStatus getStatus(final GetResponse response) {
                    return response.isExists() ? OK : NOT_FOUND;
                }
            });
}
 
Example #29
Source File: RestSimpleFeatureStore.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
RestChannelConsumer search(NodeClient client, String type, String indexName, RestRequest request) {
    String prefix = request.param("prefix");
    int from = request.paramAsInt("from", 0);
    int size = request.paramAsInt("size", 20);
    BoolQueryBuilder qb = boolQuery().filter(termQuery("type", type));
    if (prefix != null && !prefix.isEmpty()) {
        qb.must(matchQuery("name.prefix", prefix));
    }
    return (channel) -> client.prepareSearch(indexName)
            .setTypes(IndexFeatureStore.ES_TYPE)
            .setQuery(qb)
            .setSize(size)
            .setFrom(from)
            .execute(new RestStatusToXContentListener<>(channel));
}
 
Example #30
Source File: RestAddFeatureToSet.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    String store = indexName(request);
    String setName = request.param("name");
    String routing = request.param("routing");
    String featureQuery = null;
    List<StoredFeature> features = null;
    boolean merge = request.paramAsBoolean("merge", false);
    if (request.hasParam("query")) {
        featureQuery = request.param("query");
    }
    FeatureValidation validation = null;
    if (request.hasContentOrSourceParam()) {
        FeaturesParserState featuresParser = new FeaturesParserState();
        request.applyContentParser(featuresParser::parse);
        features = featuresParser.features;
        validation = featuresParser.validation;
    }
    if (featureQuery == null && (features == null || features.isEmpty())) {
        throw new IllegalArgumentException("features must be provided as a query for the feature store " +
                "or in the body, none provided");
    }

    if (featureQuery != null && (features != null && !features.isEmpty())) {
        throw new IllegalArgumentException("features must be provided as a query for the feature store " +
                "or directly in the body not both");
    }

    AddFeaturesToSetRequestBuilder builder = new AddFeaturesToSetRequestBuilder(client);
    builder.request().setStore(store);
    builder.request().setFeatureSet(setName);
    builder.request().setFeatureNameQuery(featureQuery);
    builder.request().setRouting(routing);
    builder.request().setFeatures(features);
    builder.request().setMerge(merge);
    builder.request().setValidation(validation);
    return (channel) -> builder.execute(new RestStatusToXContentListener<>(channel, (r) -> r.getResponse().getLocation(routing)));
}