1、初始化RestClient
1、引入es的RestHighLevelClient的依赖
<!--RestHighLevelClient--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> </dependency>
2、因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:
3、初始化
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder( HttpHost.create("http://192.168.133.129:9200") //此处填写你的es地址和端口 ));
2、创建索引库
//创建Request对象 CreateIndexRequest request = new CreateIndexRequest("hotel"); //准备请求的参数,DSL语句 request.source(MAPPING_TEMPLATE, XContentType.JSON); //发送请求 client.indices().create(request, RequestOptions.DEFAULT);
如有报错,可见隔壁大哥的提示org.elasticsearch.common.compress.NotXContentException: Compressor detection can only be called on s-CSDN博客
@Test void testHotelIsExist() throws IOException { //创建Request对象 GetIndexRequest request = new GetIndexRequest("hotel"); //发送请求 boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists); } @Test void testHotelDelete() throws IOException { //创建Request对象 DeleteIndexRequest request = new DeleteIndexRequest("hotel"); client.indices().delete(request,RequestOptions.DEFAULT); }
删除和判空索引库操作大同小异
索引库操作的基本步骤:
初始化RestHighLevelClient
创建XxxIndexRequest。XXX是CREATE、Get、Delete
准备DSL(CREATE时需要)
发送请求。调用RestHighLevelClient#indices().xxx()方法 xxx是create、exists、delete
3、利用JavaRestClient实现文档的CRUD
去数据库查询酒店数据,导入到hotel索引库,实现酒店数据的CRUD, 基本步骤如下:
1.初始化JavaRestClient
@BeforeEach void setUp() { this.client = new RestHighLevelClient(RestClient.builder( HttpHost.create("http://192.168.133.129:9200") )); }
2.利用JavaRestClient新增酒店数据
@Test void testAddDocument() throws IOException { Hotel hotel = iHotelService.getById(36934L);//通过mybatisplus获取酒店数据 HotelDoc hotelDoc = new HotelDoc(hotel);//转换成文档接收的格式 //1、准备Request对象 IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString()); //2、准备Json文档 request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);//转换成JSON并发送 //3、发送 client.index(request,RequestOptions.DEFAULT); }
3.利用JavaRestClient根据id查询酒店数据
@Test void testGetDocument() throws IOException { GetRequest request = new GetRequest("hotel").id("36934"); GetResponse response = client.get(request, RequestOptions.DEFAULT); String hotel = response.getSourceAsString(); System.out.println(hotel); }
4.利用JavaRestClient删除酒店数据
@Test void testDeleteDocument() throws IOException { DeleteRequest request = new DeleteRequest("hotel").id("36934"); client.delete(request,RequestOptions.DEFAULT); }
5.利用JavaRestClient修改酒店数据
@Test void testUpdateDocument() throws IOException { UpdateRequest request = new UpdateRequest("hotel","36934"); request.doc( "price","666", "starName","一百钻" ); client.update(request,RequestOptions.DEFAULT); }
文档操作的基本步骤:
初始化RestHighLevelClient
创建XxxRequest。XXX是Index、Get、Update、Delete
准备参数(Index和Update时需要)
发送请求。调用RestHighLevelClient#.xxx()方法,xxx是index、get、update、delete
6.批量插入酒店数据
@Test void testBulkRequest() throws IOException { List<Hotel> list = iHotelService.list(); BulkRequest request = new BulkRequest(); for (Hotel hotel : list) { HotelDoc hotelDoc = new HotelDoc(hotel); request.add( new IndexRequest("hotel").id(hotelDoc.getId().toString()).source(JSON.toJSONString(hotelDoc),XContentType.JSON) ); } client.bulk(request,RequestOptions.DEFAULT); }