<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
匹配引入的es版本和下载的版本一致
5.6.12版本过低,改成高的(7.6.2),所以依赖改为此版本
看自动配置
所以可见:有两种整合elasticsearch方式:
一:RestClient
二:SpringDataElasticsearch
以RestClient方式
导入依赖
spring-boot-starter-data-elasticsearch里面包含了
看官方文档
学习——文档——Elasticsearch Clients——Java REST Client——HighLevel——
看文档学习
自定义高级客户端:
@Configuration
public class ElConfiguration {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient restHighLevelClient=new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));
return restHighLevelClient;
}
}
测试索引:
package com.wh;
import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
class EldemoApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
//创建索引
@Test
void create() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("wh-index02");
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
//测试索引是否存在
@Test
void get() throws IOException {
GetIndexRequest getIndexRequest = new GetIndexRequest("wh-index02");
boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
//测试删除索引
@Test
void delete() throws IOException {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("wh-index");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
//判断是否删除OK了
System.out.println(delete.isAcknowledged());
}
//新建文档
@Test
void docCre() throws IOException {
Book book = new Book(23, "水壶");
// //创建请求
IndexRequest request = new IndexRequest("wh");
//规则 put /index02/_doc/1
request.id("2");
request.timeout(TimeValue.timeValueSeconds(1));
request.timeout("1s");
//将数据放入请求 json
request.source(JSON.toJSONString(book), XContentType.JSON);
//客户端发送请求
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
System.out.println(response.status());
}
//获取文档 判断是否存在 get /index/doc/1
@Test
void docExit() throws IOException {
GetRequest request = new GetRequest("wh", "1");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
//获取文档 get /index/doc/1
@Test
void docGet() throws IOException {
GetRequest request = new GetRequest("wh", "1");
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(response);//打印全部内容,和命令式一样
System.out.println(response.getSourceAsString());//打印文档内容
//{"_index":"wh","_type":"_doc","_id":"1","_version":2,"_seq_no":1,"_primary_term":1,"found":true,"_source":{"id":22,"title":"红楼"}}
//{"id":22,"title":"红楼"}
}
//更新文档
@Test
void upDoc() throws IOException {
UpdateRequest request = new UpdateRequest("wh", "1");
request.timeout("1s");
Book book = new Book(23, "红楼梦");
request.doc(JSON.toJSONString(book), XContentType.JSON);
UpdateResponse update = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(update.status());
System.out.println(update.toString());
System.out.println(update);
}
//删除文档
@Test
void deDoc() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("wh", "2");
deleteRequest.timeout("1s");
DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.status());
System.out.println(delete);
}
// 特殊 真正项目会大批量插入
@Test
void piliang() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("1s");
List<Book> list = new ArrayList<>();
list.add(new Book(1, "三国11111"));
list.add(new Book(2, "三国22222"));
list.add(new Book(3, "三国33333"));
list.add(new Book(4, "三国44444"));
list.add(new Book(5, "三国55555"));
for (int i = 0; i < list.size(); i++) {
//批量更新,删除就在这里修改对应请求即可
// bulkRequest.add(new IndexRequest("wh-test").id("" + (i+1)).source(JSON.toJSONString(list.get(i)), XContentType.JSON));
bulkRequest.add(new UpdateRequest("wh-test","" + (i+1)).doc(JSON.toJSONString(list.get(i)), XContentType.JSON));
}
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.status());
}
}
结果:
以SpringDataElasticsearch方式
导依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>