本文整理了Java中org.elasticsearch.client.RestHighLevelClient
类的一些代码示例,展示了RestHighLevelClient
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RestHighLevelClient
类的具体详情如下:
包路径:org.elasticsearch.client.RestHighLevelClient
类名称:RestHighLevelClient
[英]High level REST client that wraps an instance of the low level RestClient and allows to build requests and read responses. The RestClient instance is internally built based on the provided RestClientBuilder and it gets closed automatically when closing the RestHighLevelClient instance that wraps it. In case an already existing instance of a low-level REST client needs to be provided, this class can be subclassed and the #RestHighLevelClient(RestClient,CheckedConsumer,List) constructor can be used. This class can also be sub-classed to expose additional client methods that make use of endpoints added to Elasticsearch through plugins, or to add support for custom response sections, again added to Elasticsearch through plugins.
[中]高级REST客户端,它封装了低级REST客户端的一个实例,并允许生成请求和读取响应。RestClient实例是基于提供的RestClientBuilder在内部构建的,当关闭封装它的RestHighLevelClient实例时,它会自动关闭。如果需要提供一个已经存在的低级REST客户机实例,可以对该类进行子类化,并且可以使用#RestHighLevelClient(RestClient,CheckedConsumer,List)构造函数。该类还可以进行子分类,以公开其他客户端方法,这些方法利用通过插件添加到Elasticsearch的端点,或者添加对自定义响应节的支持,再次通过插件添加到Elasticsearch。
代码示例来源:origin: spring-projects/spring-data-elasticsearch
protected void buildClient() throws Exception {
Assert.hasText(hosts, "[Assertion Failed] At least one host must be set.");
ArrayList<HttpHost> httpHosts = new ArrayList<HttpHost>();
for (String host : hosts.split(COMMA)) {
URL hostUrl = new URL(host);
httpHosts.add(new HttpHost(hostUrl.getHost(), hostUrl.getPort(), hostUrl.getProtocol()));
}
client = new RestHighLevelClient(RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()])));
}
代码示例来源:origin: spring-projects/spring-data-elasticsearch
private SearchResponse doSearch(SearchRequest searchRequest, SearchQuery searchQuery) {
prepareSearch(searchRequest, searchQuery);
try {
return client.search(searchRequest);
} catch (IOException e) {
throw new ElasticsearchException("Error for search request with scroll: " + searchRequest.toString(), e);
}
}
代码示例来源:origin: apache/nifi
@Override
public DeleteOperationResponse deleteById(String index, String type, List<String> ids) throws IOException {
BulkRequest bulk = new BulkRequest();
for (int idx = 0; idx < ids.size(); idx++) {
DeleteRequest request = new DeleteRequest(index, type, ids.get(idx));
bulk.add(request);
}
BulkResponse response = highLevelClient.bulk(bulk);
DeleteOperationResponse dor = new DeleteOperationResponse(response.getTookInMillis());
return dor;
}
代码示例来源:origin: apache/flink
@Override
public RestHighLevelClient createClient(Map<String, String> clientConfig) throws IOException {
RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()]));
restClientFactory.configureRestClientBuilder(builder);
RestHighLevelClient rhlClient = new RestHighLevelClient(builder);
if (LOG.isInfoEnabled()) {
LOG.info("Pinging Elasticsearch cluster via hosts {} ...", httpHosts);
}
if (!rhlClient.ping()) {
throw new RuntimeException("There are no reachable Elasticsearch nodes!");
}
if (LOG.isInfoEnabled()) {
LOG.info("Created Elasticsearch RestHighLevelClient connected to {}", httpHosts.toString());
}
return rhlClient;
}
代码示例来源:origin: org.apache.camel/camel-elasticsearch-rest
message.setBody(restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT).getId());
} else if (operation == ElasticsearchOperation.Update) {
UpdateRequest updateRequest = message.getBody(UpdateRequest.class);
message.setBody(restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT).getId());
} else if (operation == ElasticsearchOperation.GetById) {
GetRequest getRequest = message.getBody(GetRequest.class);
message.setBody(restHighLevelClient.get(getRequest, RequestOptions.DEFAULT));
} else if (operation == ElasticsearchOperation.Bulk) {
BulkRequest bulkRequest = message.getBody(BulkRequest.class);
message.setBody(restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT).getItems());
} else if (operation == ElasticsearchOperation.BulkIndex) {
BulkRequest bulkRequest = message.getBody(BulkRequest.class);
message.setBody(restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT).getItems());
} else if (operation == ElasticsearchOperation.Delete) {
DeleteRequest deleteRequest = message.getBody(DeleteRequest.class);
message.setBody(restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT).getResult());
} else if (operation == ElasticsearchOperation.DeleteIndex) {
DeleteRequest deleteRequest = message.getBody(DeleteRequest.class);
searchRequest.source(sourceBuilder);
try {
restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
message.setBody(true);
} catch (ElasticsearchStatusException e) {
message.setBody(restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT).getHits());
} else if (operation == ElasticsearchOperation.MultiSearch) {
MultiSearchRequest searchRequest = message.getBody(MultiSearchRequest.class);
message.setBody(restHighLevelClient.msearch(searchRequest, RequestOptions.DEFAULT).getResponses());
代码示例来源:origin: dadoonet/fscrawler
try {
this.client = new RestHighLevelClient(lowLevelClient);
checkVersion();
logger.info("Elasticsearch Client for version {}.x connected to a node running version {}", compatibleVersion(), getVersion());
(request, bulkListener) -> client.bulkAsync(request, bulkListener);
代码示例来源:origin: apache/incubator-gobblin
@Override
public GetResponse get(GetRequest getRequest)
throws IOException {
return highLevelClient.get(getRequest);
}
代码示例来源:origin: spring-projects/spring-data-elasticsearch
@Override
public void destroy() throws Exception {
try {
log.info("Closing elasticSearch client");
if (client != null) {
client.close();
}
} catch (final Exception e) {
log.error("Error closing ElasticSearch client: ", e);
}
}
代码示例来源:origin: eea/eea.elasticsearch.river.rdf
searchResponse = client.search(searchRequest);
String scrollId = searchResponse.getScrollId();
SearchHit[] searchHits = searchResponse.getHits().getHits();
DeleteResponse deleteResponse = client.delete(deleteRequest);
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
scrollRequest.scroll(scroll);
searchResponse = client.searchScroll(scrollRequest);
scrollId = searchResponse.getScrollId();
searchHits = searchResponse.getHits().getHits();
DeleteResponse deleteResponse = client.delete(deleteRequest);
ClearScrollResponse clearScrollResponse = client.clearScroll(clearScrollRequest);
boolean succeeded = clearScrollResponse.isSucceeded();
} catch (IOException e) {
代码示例来源:origin: Netflix/conductor
/**
* Performs an index operation with a retry.
* @param request The index request that we want to perform.
* @param operationDescription The type of operation that we are performing.
*/
private void indexWithRetry(final IndexRequest request, final String operationDescription) {
try {
new RetryUtil<IndexResponse>().retryOnException(() -> {
try {
return elasticSearchClient.index(request);
} catch (IOException e) {
throw new RuntimeException(e);
}
}, null, null, RETRY_COUNT, operationDescription, "indexWithRetry");
} catch (Exception e) {
Monitors.error(className, "index");
logger.error("Failed to index {} for request type: {}", request.id(), request.type(), e);
}
}
代码示例来源:origin: Netflix/conductor
@Override
public void removeWorkflow(String workflowId) {
DeleteRequest request = new DeleteRequest(indexName, WORKFLOW_DOC_TYPE, workflowId);
try {
DeleteResponse response = elasticSearchClient.delete(request);
if (response.getResult() == DocWriteResponse.Result.NOT_FOUND) {
logger.error("Index removal failed - document not found by id: {}", workflowId);
}
} catch (IOException e) {
logger.error("Failed to remove workflow {} from index", workflowId, e);
Monitors.error(className, "remove");
}
}
代码示例来源:origin: dadoonet/fscrawler
@Override
public void start() throws IOException {
if (client != null) {
// The client has already been initialized. Let's skip this again
return;
}
try {
// Create an elasticsearch client
client = new RestHighLevelClient(buildRestClient(settings.getElasticsearch()));
checkVersion();
logger.info("Elasticsearch Client for version {}.x connected to a node running version {}", compatibleVersion(), getVersion());
} catch (Exception e) {
logger.warn("failed to create elasticsearch client, disabling crawler...");
throw e;
}
if (settings.getElasticsearch().getPipeline() != null) {
// Check that the pipeline exists
if (!isExistingPipeline(settings.getElasticsearch().getPipeline())) {
throw new RuntimeException("You defined pipeline:" + settings.getElasticsearch().getPipeline() +
", but it does not exist.");
}
}
BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer =
(request, bulkListener) -> client.bulkAsync(request, RequestOptions.DEFAULT, bulkListener);
bulkProcessor = BulkProcessor.builder(bulkConsumer, new DebugListener(logger))
.setBulkActions(settings.getElasticsearch().getBulkSize())
.setFlushInterval(TimeValue.timeValueMillis(settings.getElasticsearch().getFlushInterval().millis()))
.setBulkSize(new ByteSizeValue(settings.getElasticsearch().getByteSize().getBytes()))
.build();
}
代码示例来源:origin: apache/nifi
@Override
public Map<String, Object> get(String index, String type, String id) throws IOException {
GetRequest get = new GetRequest(index, type, id);
GetResponse resp = highLevelClient.get(get, new Header[]{});
return resp.getSource();
}
代码示例来源:origin: spring-projects/spring-data-elasticsearch
@Override
default void close() throws IOException {
rest().close();
}
}
代码示例来源:origin: spring-projects/spring-data-elasticsearch
@Override
public String index(IndexQuery query) {
String documentId;
IndexRequest request = prepareIndex(query);
try {
documentId = client.index(request).getId();
} catch (IOException e) {
throw new ElasticsearchException("Error while index for request: " + request.toString(), e);
}
// We should call this because we are not going through a mapper.
if (query.getObject() != null) {
setPersistentEntityId(query.getObject(), documentId);
}
return documentId;
}
代码示例来源:origin: spring-projects/spring-data-elasticsearch
@Override
public String delete(String indexName, String type, String id) {
DeleteRequest request = new DeleteRequest(indexName, type, id);
try {
return client.delete(request).getId();
} catch (IOException e) {
throw new ElasticsearchException("Error while deleting item request: " + request.toString(), e);
}
}
代码示例来源:origin: Netflix/conductor
@Inject
public ElasticSearchRestDAOV5(RestClient lowLevelRestClient, ElasticSearchConfiguration config, ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
this.elasticSearchAdminClient = lowLevelRestClient;
this.elasticSearchClient = new RestHighLevelClient(lowLevelRestClient);
this.indexName = config.getIndexName();
this.logIndexPrefix = config.getTasklogIndexName();
this.clusterHealthColor = config.getClusterHealthColor();
// Set up a workerpool for performing async operations.
int corePoolSize = 6;
int maximumPoolSize = 12;
long keepAliveTime = 1L;
this.executorService = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize,
keepAliveTime,
TimeUnit.MINUTES,
new LinkedBlockingQueue<>());
}
代码示例来源:origin: spring-projects/spring-data-elasticsearch
private long doCount(SearchRequest countRequest, QueryBuilder elasticsearchQuery) {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
if (elasticsearchQuery != null) {
sourceBuilder.query(elasticsearchQuery);
}
countRequest.source(sourceBuilder);
try {
return client.search(countRequest).getHits().getTotalHits();
} catch (IOException e) {
throw new ElasticsearchException("Error while searching for request: " + countRequest.toString(), e);
}
}
代码示例来源:origin: dadoonet/fscrawler
@Override
public void start() throws IOException {
if (client != null) {
// The client has already been initialized. Let's skip this again
return;
}
try {
// Create an elasticsearch client
client = new RestHighLevelClient(buildRestClient(settings.getElasticsearch()));
checkVersion();
logger.info("Elasticsearch Client for version {}.x connected to a node running version {}", compatibleVersion(), getVersion());
} catch (Exception e) {
logger.warn("failed to create elasticsearch client, disabling crawler...");
throw e;
}
if (settings.getElasticsearch().getPipeline() != null) {
// Check that the pipeline exists
if (!isExistingPipeline(settings.getElasticsearch().getPipeline())) {
throw new RuntimeException("You defined pipeline:" + settings.getElasticsearch().getPipeline() +
", but it does not exist.");
}
}
BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer =
(request, bulkListener) -> client.bulkAsync(request, RequestOptions.DEFAULT, bulkListener);
bulkProcessor = BulkProcessor.builder(bulkConsumer, new DebugListener(logger))
.setBulkActions(settings.getElasticsearch().getBulkSize())
.setFlushInterval(TimeValue.timeValueMillis(settings.getElasticsearch().getFlushInterval().millis()))
.setBulkSize(new ByteSizeValue(settings.getElasticsearch().getByteSize().getBytes()))
.build();
}
代码示例来源:origin: Netflix/conductor
new RetryUtil<BulkResponse>().retryOnException(() -> {
try {
return elasticSearchClient.bulk(bulkRequest);
} catch (IOException e) {
throw new RuntimeException(e);
内容来源于网络,如有侵权,请联系作者删除!