API 文档
RestClient
Elasticsearch 会在7.0之后的版本废弃TransportClient,在8.0之后的版本移除TransportClient (文档)。因此,使用RestClient来进行相关的操作。
We plan on deprecating the
TransportClient
in Elasticsearch 7.0 and removing it completely in 8.0. Instead, you should be using the Java High Level REST Client, which executes HTTP requests rather than serialized Java requests. The migration guidedescribes all the steps needed to migrate.
构建RestClient
maven依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.3.2</version>
</dependency>
定义常量
public class EsConsts {
public static final String INDEX_NAME = "books";
public static final String TYPE = "books";
public static final Integer PORT = 9200;
public static final String HOST_NAME = "localhost";
}
创建RestHighLevelClient
public class RestClientHelper {
private static RestHighLevelClient client;
private RestClientHelper(){
client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(EsConsts.HOST_NAME, EsConsts.PORT, "http")));
}
public static RestHighLevelClient getClient(){
if(client == null){
synchronized (RestClientHelper.class){
if(client == null){
RestClientHelper restClientHelper = new RestClientHelper();
}
}
}
return client;
}
}
索引相关操作
官方文档
创建索引
//创建索引
public void createIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest(EsConsts.INDEX_NAME);
buildSetting(request);
buildIndexMapping(request);
client.indices().create(request);
}
//设置分片
public void buildSetting(CreateIndexRequest request){
request.settings(Settings.builder().put("index.number_of_shards",3)
.put("index.number_of_replicas",2));
}
//设置index的mapping
public void buildIndexMapping(CreateIndexRequest request){
Map<String, Object> jsonMap = new HashMap<>();
Map<String, Object> number = new HashMap<>();
number.put("type", "text");
Map<String, Object> price = new HashMap<>();
price.put("type", "float" );
Map<String, Object> title = new HashMap<>();
title.put("type", "text");
Map<String, Object> province = new HashMap<>();
province.put("type", "text");
Map<String, Object> publishTime = new HashMap<>();
publishTime.put("type", "date");
Map<String, Object> properties = new HashMap<>();
properties.put("number", number);
properties.put("price", price);
properties.put("title", title);
properties.put("province", province);
properties.put("publishTime", publishTime);
Map<String, Object> book = new HashMap<>();
book.put("properties", properties);
jsonMap.put("books", book);
request.mapping(EsConsts.TYPE, jsonMap);
}
判断存在并删除
public void deleteIndex() throws IOException {
GetIndexRequest getIndexRequest = new GetIndexRequest();
getIndexRequest.indices(EsConsts.INDEX_NAME);
if(client.indices().exists(getIndexRequest)) {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(EsConsts.INDEX_NAME);
client.indices().delete(deleteIndexRequest);
}
}
Document操作相关
官方文档
定义对象
public class Book {
private String number;
private Double price;
private String title;
private String province;
private Date publishTime;
}
添加数据
public void putData(Book book) throws IOException {
IndexRequest indexRequest = new IndexRequest(EsConsts.INDEX_NAME, EsConsts.TYPE, book.getNumber());
ObjectMapper mapper = new ObjectMapper();
byte[] json = mapper.writeValueAsBytes(book);
indexRequest.source(json, XContentType.JSON);
client.index(indexRequest);
}
获取数据
public Book getData(String id) throws IOException {
GetRequest getRequest = new GetRequest(EsConsts.INDEX_NAME, EsConsts.TYPE, id);
GetResponse getResponse = client.get(getRequest);
byte[] sourceAsBytes = getResponse.getSourceAsBytes();
ObjectMapper mapper = new ObjectMapper();
Book book = mapper.readValue(sourceAsBytes, Book.class);
return book;
}
更新数据
public void updateData(Book book) throws IOException {
UpdateRequest updateRequest = new UpdateRequest(EsConsts.INDEX_NAME, EsConsts.TYPE, book.getNumber());
updateRequest.doc(createIndexRequest(book));
GetResult getResult =
client.update(updateRequest).getGetResult();
}
private IndexRequest createIndexRequest(Book book) throws JsonProcessingException {
IndexRequest indexRequest = new IndexRequest(EsConsts.INDEX_NAME, EsConsts.TYPE, book.getNumber());
ObjectMapper mapper = new ObjectMapper();
byte[] json = mapper.writeValueAsBytes(book);
indexRequest.source(json, XContentType.JSON);
return indexRequest;
}
注:es的更新数据,不论是直接用script方式,还是updaterequest.doc方式,貌似都是在原来已有的数据上合并(涉及到的字段更新,update中未涉及到的字段保持不变);如果需要全量覆盖,直接用添加数据请求。
删除数据
public String deleteData(String id) throws IOException{
DeleteRequest deleteRequest = new DeleteRequest(EsConsts.INDEX_NAME, EsConsts.TYPE,
id);
DeleteResponse deleteResponse = client.delete(deleteRequest);
return deleteResponse.getResult().toString();
}