一、创建连接
!ps:RestHighLevelClient是官方指定的连接API。另外一个是TransportClient,但是TransportClient这个是已经废弃不用的,所以会在ES8.0之后完全移除,也就是说8.0之后就无法使用了
RestHighLevelClient的maven依赖为(使用前必须添加相应依赖)
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.9.2
org.elasticsearch
elasticsearch
7.9.2
注意:以上的依赖版本可以根据你使用的ES的版本来定,向下兼容,但是无法向上兼容
创建连接
static String ip = "localhost";
static int port = 9200;
static RestHighLevelClient restHighLevelClient = null;
static TransportClient client = null;
static RestHighLevelClient initClient(){
//这里的RestClient.builder(new HttpHost(ip,port),.........)支持多个httphost连接,也就是支持连接多个elasticsearch
restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost(ip,port)));
}
static Connection getInstance(){
synchronized (ElasticSearchFactory.class){
if (restHighLevelClient == null){
restHighLevelClient = initClient();
}
}
return connection;
}
RestHighLevelClient 支持对ES的增删改查、批量操作
新增:
public static void creatIndex(String json){
IndexRequest indexRequest = new IndexRequest("test_index","String");
indexRequest.source(json, XContentType.JSON);//这里的json为存入ES的文档内容
try {
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);//RequestOptions在新方法中必须选择
} catch (IOException e) {
e.printStackTrace();
}
}
查询:
查询支持直接查询和条件查询,由于ES是根据索引、文档类型和文档ID确定一个唯一的文档的,所以直接查询必须要有索引和类型还有ID
public static Map getIndex(String indexName,String type,String id){
GetRequest request = new GetRequest(indexName);
//SearchRequest searchRequest = new SearchRequest(indexName);
try {
GetResponse search = restHighLevelClient.get(request, RequestOptions.DEFAULT);
Map source = search.getSource();
return source;
} catch (IOException e) {
e.printStackTrace();
}
return new HashMap<>();
}
``
条件查询:
条件查询支持所有的ES的查询规则:![使用ES查询语句须知]{https://www.jianshu.com/p/cfbac15e0b7a}
public static Map searchIndex(String indexName){
//GetRequest request = new GetRequest(indexName);
//SearchRequest searchRequest = new SearchRequest(indexName);
QueryBuilder queryBuilders = QueryBuilders.boolQuery().filter(
QueryBuilders.termQuery("queryKey","吱吱吱ZZZ")
);
SearchRequest searchRequest = new SearchRequest();
//searchRequest.indices("posts");这里可以设计GEET或者POST
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));支持按评分排序,评分是指分词之后取得的文档相似度评分
searchSourceBuilder.query(QueryBuilders.boolQuery().filter(QueryBuilders.termQuery("queryKey.keyword","吱吱吱ZZZ")));
searchRequest.source(searchSourceBuilder);
try {
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = search.getHits().getHits();
for (SearchHit searchHit : hits){
Map sourceAsMap = searchHit.getSourceAsMap();
System.out.println("source : {}" + sourceAsMap.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
return new HashMap<>();
}
修改:
ps:ES中其实并不存在真正意义上的修改,其实是进行的文档覆盖,先将原文档查出来标记删除,然后在原文档的基础上添加上修改形成一份新文档,存入ES中,之后再将打上删除标记的文档删除。
public static void updateIndex(String indexName, String type, String id, String json){
UpdateRequest request = new UpdateRequest(indexName, type, id);
request.doc(json, XContentType.JSON);
try {
UpdateResponse updateResponse = restHighLevelClient.update(request,RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
删除:
public static void deleteIndex(String indexName, String type, String id){
DeleteRequest request = new DeleteRequest(indexName,type,id);
try {
restHighLevelClient.delete(request,RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
bulk操作,bulk操作相当于批处理,能够在一次操作中处理一批数据,同时能够同时处理,增删改操作
public static void bulkIndex(){
BulkRequest request = new BulkRequest();
String jsonString="{\n" +
"\t\"databaseId\": \"謝謝謝謝謝謝\",\n" +
"\t\"databaseType\": \"嘻嘻嘻\",\n" +
"\t\"queryKey\":\"吱吱吱ZZZ\"\n" +
"}\n";
UUID uuid = UUID.randomUUID();
request.add(new IndexRequest("test_index", "String", uuid.toString())
.source(jsonString,XContentType.JSON));
String updateJson="{\n" +
" \"queryKey\":\"吱吱吱ZZZ\"\n" +
"}";
request.add(new UpdateRequest("test_index", "String", uuid.toString())
.doc(updateJson,XContentType.JSON));
request.add(new DeleteRequest("test_index", "String", uuid.toString()));
try {
restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}