ElasticSearch6.2.3学习第一篇,transport的方法早晚都要gg,所以还是赶紧把restclient的方法学会
初始化
A RestHighLevelClient instance needs a REST low-level client builder to be built as follows:
高级的实例需要低级的帮着构建
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(“localhost”, 9200, “http”),
new HttpHost(“localhost”, 9201, “http”)));
The high-level client will internally create the low-level client used to perform requests based on the provided builder, and manage its lifecycle.
上面那句话里面内部其实就依靠低级的rest创建了高级的client
The high-level client instance needs to be closed when no longer needed so that all the resources used by it get properly released, as well as the underlying http client instance and its threads. This can be done through the close method, which will close the internal RestClient instance.
高级用完了也需要关,不然占用的资源和线程太浪费东西了,然后用close方法就可以了,内部开启的也会关掉
client.close();
In the rest of this documentation about the Java High Level Client, the RestHighLevelClient instance will be referenced as client.
下面讲的高级的rest client的实例都会用client代表
支持的APIs
The Java High Level REST Client supports the following APIs:
高级的封装好了下面的api
Indices APIs(关于索引的APIs)
Create Index API(创建索引)
A CreateIndexRequest requires an index argument:
创建索引需要索引名字这个参数,下面创建了一个twitter的索引
CreateIndexRequest request = new CreateIndexRequest(“twitter”);
Index settings 索引可以设置一些东西。比如分片的数量,以及副本的数量
Each index created can have specific settings associated with it.
request.settings(Settings.builder()
.put(“index.number_of_shards”, 3)
.put(“index.number_of_replicas”, 2)
);
Index mappings 索引的映射,就想数据库的每个表的列都有字段名一样,索引的mapping就是希望建索引的人能够按照一定规矩去建立索引
An index may be created with mappings for its document types
设定每个字段的类型很重要
request.mapping(“tweet”,
” {\n” +
” \”tweet\”: {\n” +
” \”properties\”: {\n” +
” \”message\”: {\n” +
” \”type\”: \”text\”\n” +
” }\n” +
” }\n” +
” }\n” +
” }”,
XContentType.JSON);
The mapping for this type, provided as a JSON string
上面定义了message的类型,并最后输出成json
Index aliases索引的别名
Aliases can be set at index creation time
request.alias(
new Alias(“twitter_alias”)
);
Optional arguments 可选的参数
The following arguments can optionally be provided:
request.timeout(TimeValue.timeValueMinutes(2));
request.timeout(“2m”);
Timeout to wait for the all the nodes to acknowledge the index creation as a TimeValue
Timeout to wait for the all the nodes to acknowledge the index creation as a String
设置:将所有节点确认索引创建的超时设为两分钟,一个使用TimeValue值,第二个使用String值
request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
request.masterNodeTimeout(“1m”);
Timeout to connect to the master node as a TimeValue
Timeout to connect to the master node as a String
关于主节点的超时时间,1,2同上
request.waitForActiveShards(2);
request.waitForActiveShards(ActiveShardCount.DEFAULT);
The number of active shard copies to wait for before the create index API returns a response, as an int.
The number of active shard copies to wait for before the create index API returns a response, as an ActiveShardCount.
在创建索引API返回响应之前等待的活动分片副本的数量
Synchronous Execution 同步执行
CreateIndexResponse createIndexResponse = client.indices().create(request);
Asynchronous Execution 异步执行
The asynchronous execution of a create index request requires both the CreateIndexRequest instance and an ActionListener instance to be passed to the asynchronous method:
异步执行创建索引需要一个ActionListener实例作为参数传进去
client.indices().createAsync(request, listener);
当执行完成的时候,CreateIndexRequest会
The CreateIndexRequest to execute and the ActionListener to use when the execution completes
The asynchronous method does not block and returns immediately. Once it is completed the ActionListener is called back using the onResponse method if the execution successfully completed or using the onFailure method if it failed.
异步方法不会阻止并立即返回。完成后,如果执行成功完成,则使用onResponse方法回调操作侦听器;如果执行失败,则使用onffailure方法回调操作侦听器。
A typical listener for CreateIndexResponse looks like:
ActionListener listener = new ActionListener() {
//Called when the execution is successfully completed. The response is provided as an argument 成功完成执行时调用。响应作为参数提供
@Override
public void onResponse(CreateIndexResponse createIndexResponse) {
}
//Called in case of failure. The raised exception is provided as an argument 在失败的情况下调用。引发的异常作为参数提供
@Override
public void onFailure(Exception e) {
}
};
Create Index Response 索引回应的创建使用
The returned CreateIndexResponse allows to retrieve information about the executed operation as follows:
返回的CreateIndexResponse允许检索有关执行操作的信息,如下所示:
boolean acknowledged = createIndexResponse.isAcknowledged();
boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
Indicates whether all of the nodes have acknowledged the request
指示是否所有节点都已确认请求
Indicates whether the requisite number of shard copies were started for each shard in the index before timing out
指示是否在超时之前为索引中的每个分片启动了必需的分片副本数
Delete Index API
Delete Index Request(与创建类似)
A DeleteIndexRequest requires an index argument:
DeleteIndexRequest request = new DeleteIndexRequest(“posts”);
Optional arguments(类似)
The following arguments can optionally be provided:
request.timeout(TimeValue.timeValueMinutes(2));
request.timeout(“2m”);
Timeout to wait for the all the nodes to acknowledge the index deletion as a TimeValue
Timeout to wait for the all the nodes to acknowledge the index deletion as a String
request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
request.masterNodeTimeout(“1m”);
Timeout to connect to the master node as a TimeValue
Timeout to connect to the master node as a String
request.indicesOptions(IndicesOptions.lenientExpandOpen());
Setting IndicesOptions controls how unavailable indices are resolved and how wildcard expressions are expanded
设置IndicesOptions去控制不能用的索引如何解决处理,上面用lenientExpandOpen进行通用的处理
Synchronous Execution
DeleteIndexResponse deleteIndexResponse = client.indices().delete(request);
Asynchronous Execution
The asynchronous execution of a delete index request requires both the DeleteIndexRequest instance and an ActionListener instance to be passed to the asynchronous method:
client.indices().deleteAsync(request, listener);
The DeleteIndexRequest to execute and the ActionListener to use when the execution completes
The asynchronous method does not block and returns immediately. Once it is completed the ActionListener is called back using the onResponse method if the execution successfully completed or using the onFailure method if it failed.
A typical listener for DeleteIndexResponse looks like:
ActionListener listener = new ActionListener() {
@Override
public void onResponse(DeleteIndexResponse deleteIndexResponse) {
}
@Override
public void onFailure(Exception e) {
}
};
Called when the execution is successfully completed. The response is provided as an argument
Called in case of failure. The raised exception is provided as an argument
Delete Index Response
The returned DeleteIndexResponse allows to retrieve information about the executed operation as follows:
boolean acknowledged = deleteIndexResponse.isAcknowledged();
Indicates whether all of the nodes have acknowledged the request
If the index was not found, an ElasticsearchException will be thrown:
try {
DeleteIndexRequest request = new DeleteIndexRequest(“does_not_exist”);
client.indices().delete(request);
} catch (ElasticsearchException exception) {
if (exception.status() == RestStatus.NOT_FOUND) {
//Do something if the index to be deleted was not found
}
}