当前位置:   article > 正文

RestClient操作索引库_resclient

resclient

RestClient是es官方提供的一套用于通过代码操作es的api,es官方提供了不同语言的客户端,这些客户端本质上就是组装的DSL语句,通过http请求发送给es


步骤一:数据库中导入表和数据


 步骤二:分析数据结构

在kibana中设置


 步骤三:初始化JavaRestClient

使用es需要引入对应的依赖,注意依赖版本要和es版本一致

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

 因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:

<properties>
    <java.version>1.8</java.version>
    <elasticsearch.version>7.12.1</elasticsearch.version>
</properties>

创建一个测试类,先初始化RestHighLevelClient,连接到es,代码如下 

  1. import java.io.IOException;
  2. public class HotelIndexTest {
  3. private RestHighLevelClient client;
  4. //声明对象,并做对象初始化连接到es,提高代码复用性
  5. @BeforeEach
  6. void setUp(){
  7. this.client = new RestHighLevelClient(RestClient.builder(
  8. HttpHost.create("http://192.168.61.130:9200")
  9. ));
  10. }
  11. @AfterEach
  12. void tearDown() throws IOException {
  13. this.client.close();
  14. }
  15. }

 


步骤4:创建索引库

成功后创建一个新的索引库,这里的DSL语句需要我们手动编写或者自己引入,我先新建一个类,写好DSL语句,在Dev Tools写一个,然后复制到代码里去,如下

  1. public class HotelConstants {
  2. public static final String MAPPING_TEMPLATE = "{\n" +
  3. " \"mappings\": {\n" +
  4. " \"properties\": {\n" +
  5. " \"id\": {\n" +
  6. " \"type\": \"keyword\"\n" +
  7. " },\n" +
  8. " \"name\":{\n" +
  9. " \"type\": \"text\",\n" +
  10. " \"analyzer\": \"ik_max_word\",\n" +
  11. " \"copy_to\": \"{all}\"\n" +
  12. " },\n" +
  13. " \"address\": {\n" +
  14. " \"type\": \"keyword\",\n" +
  15. " \"index\": false\n" +
  16. " },\n" +
  17. " \"price\": {\n" +
  18. " \"type\": \"integer\"\n" +
  19. " },\n" +
  20. " \"score\": {\n" +
  21. " \"type\": \"integer\"\n" +
  22. " },\n" +
  23. " \"brand\": {\n" +
  24. " \"type\": \"keyword\",\n" +
  25. " \"copy_to\": \"{all}\"\n" +
  26. " },\n" +
  27. " \"city\": {\n" +
  28. " \"type\": \"keyword\"\n" +
  29. " },\n" +
  30. " \"starName\": {\n" +
  31. " \"type\": \"keyword\"\n" +
  32. " },\n" +
  33. " \"business\": {\n" +
  34. " \"type\": \"keyword\",\n" +
  35. " \"copy_to\": \"{all}\"\n" +
  36. " },\n" +
  37. " \"location\": {\n" +
  38. " \"type\": \"geo_point\"\n" +
  39. " },\n" +
  40. " \"pic\": {\n" +
  41. " \"type\": \"keyword\",\n" +
  42. " \"index\": false\n" +
  43. " },\n" +
  44. " \"all\":{\n" +
  45. " \"type\": \"text\",\n" +
  46. " \"analyzer\": \"ik_max_word\"\n" +
  47. " }\n" +
  48. " }\n" +
  49. " }\n" +
  50. "}";
  51. }

然后写新增索引库的方法,并将刚才的DSL语句引入到下面代码中,如下 

  1. @Test
  2. void createHotelIndex() throws IOException {
  3. //1,创建Request对象
  4. CreateIndexRequest request = new CreateIndexRequest("hotel");
  5. //2,准备请求的参数:DSL语句
  6. request.source(MAPPING_TEMPLATE, XContentType.JSON);
  7. //3,发送请求
  8. client.indices().create(request, RequestOptions.DEFAULT);
  9. }

 


步骤五:删除索引库、判断索引库是否存在

  1. //删除索引库
  2. @Test
  3. void testDeleteHotelIndex() throws IOException {
  4. //创建删除对象
  5. DeleteIndexRequest request = new DeleteIndexRequest("hotel");
  6. //删除请求
  7. client.indices().delete(request,RequestOptions.DEFAULT);
  8. }
  1. //判断索引是否存在
  2. @Test
  3. void testExistsHotelIndex() throws IOException {
  4. //查看索引库
  5. GetIndexRequest getIndexRequest = new GetIndexRequest("hotel");
  6. //判断索引库是否存在
  7. boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
  8. System.out.println(exists ? "索引库存在":"索引库不存在" );//打印true或者false判断索引库是否存在
  9. }

 

文档的基本操作

新增文档:通常情况新增的数据都是从数据库导入的,所以需要先连接数据库并查到数据,并且根据数据库的字段创建对应的索引库,然后才是导入数据,先创建pojo对象,这里取名Hotel,

  1. @Data
  2. @TableName("tb_hotel")
  3. public class Hotel {
  4. @TableId(type = IdType.INPUT)
  5. private Long id;
  6. private String name;
  7. private String address;
  8. private Integer price;
  9. private Integer score;
  10. private String brand;
  11. private String city;
  12. private String starName;
  13. private String business;
  14. private String longitude; //经度
  15. private String latitude; //纬度
  16. private String pic;
  17. }

这个数据库表有一个地理位置,用经纬度表示,是两个字段,而在es中经纬度是将这两个字段拼接起来的一个字段,格式不同所以要做转换,所以要再写一个pojo对象这里叫做HotelDoc

  1. @Data
  2. @NoArgsConstructor
  3. public class HotelDoc {
  4. private Long id;
  5. private String name;
  6. private String address;
  7. private Integer price;
  8. private Integer score;
  9. private String brand;
  10. private String city;
  11. private String starName;
  12. private String business;
  13. private String location;
  14. private String pic;
  15. public HotelDoc(Hotel hotel) {
  16. this.id = hotel.getId();
  17. this.name = hotel.getName();
  18. this.address = hotel.getAddress();
  19. this.price = hotel.getPrice();
  20. this.score = hotel.getScore();
  21. this.brand = hotel.getBrand();
  22. this.city = hotel.getCity();
  23. this.starName = hotel.getStarName();
  24. this.business = hotel.getBusiness();
  25. this.location = hotel.getLatitude() + ", " + hotel.getLongitude();//这里将数据库的两个字段拼为一个
  26. this.pic = hotel.getPic();
  27. }
  28. }

 新建一个测试类,通过mybatisplus查询数据库内的内容上传到es中

 

  1. @SpringBootTest
  2. public class HotelDocumentTest {
  3. @Autowired
  4. private IHotelService hotelService;
  5. private RestHighLevelClient client;
  6. @Test
  7. void testAddDocument() throws IOException {
  8. //根据id查询数据
  9. Hotel hotel = hotelService.getById(61083L);
  10. //因为没有经纬度,设置了一个新实体类混合经纬度
  11. HotelDoc hotelDoc = new HotelDoc(hotel);
  12. //1.准备Request对象
  13. IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
  14. //2.准备Json文档
  15. request.source(JSON.toJSON(hotelDoc),XContentType.JSON);
  16. //3.发送请求
  17. client.index(request,RequestOptions.DEFAULT);
  18. }
  19. //声明对象,并做对象初始化连接到es,提高代码复用性
  20. @BeforeEach
  21. void setUp(){
  22. this.client = new RestHighLevelClient(RestClient.builder(
  23. HttpHost.create("http://192.168.61.130:9200")
  24. ));
  25. }
  26. @AfterEach
  27. void tearDown() throws IOException {
  28. this.client.close();
  29. }
  30. }

 


 查询操作

  1. @Test
  2. void testGetDocumentById() throws IOException {
  3. //准备request
  4. GetRequest request = new GetRequest("hotel", "61083");
  5. //发送请求得到响应
  6. GetResponse response = client.get(request, RequestOptions.DEFAULT);
  7. //解析响应结果
  8. String json = response.getSourceAsString();
  9. //反序列化json
  10. HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
  11. System.err.println(hotelDoc);
  12. }

 


 删除操作

  1. @Test
  2. void deleteDocumentest() throws IOException {
  3. //1、准备request
  4. DeleteRequest request= new DeleteRequest("hotel", "61083");
  5. //2、发送请求
  6. client.delete(request,RequestOptions.DEFAULT);
  7. }

 


修改操作

方式一:全量更新。再次写入id一样的文档,就会删除旧文档,添加新文档

方式二:局部更新。只更新部分字段,我们演示方式二

  1. @Test
  2. void testUpdateDocumentById() throws IOException {
  3. //1,新建request对象
  4. UpdateRequest request = new UpdateRequest("hotel", "61083");
  5. //2.准备参数
  6. request.doc(
  7. "price","1000",
  8. "starName","四钻"
  9. );
  10. //3,发送请求
  11. client.update(request,RequestOptions.DEFAULT);
  12. }

批量数据导入操作

  1. void BulkRequest() throws IOException {
  2. //1、创建request
  3. BulkRequest bulkRequest = new BulkRequest();
  4. //批量查询数据库数据
  5. List<Hotel> list = hotelService.list();
  6. //遍历是因为数据库数据较多
  7. for (Hotel hotel : list) {
  8. //转换为文档类型
  9. HotelDoc hotelDoc = new HotelDoc(hotel);
  10. //2、准备参数,添加多个新增的Request
  11. bulkRequest.add(new IndexRequest("hotel").
  12. id(hotelDoc.getId().toString()).
  13. source(JSON.toJSONString(hotelDoc),XContentType.JSON));
  14. }//这里就是一个链式编程,直接调用了对象的方法,而不像之前是分步写
  15. //不理解遍历的,也可以 自己一条条数据的添加进去,如下,就是很麻烦
  16. /*bulkRequest.add(new IndexRequest("hotel").
  17. id("1").
  18. source(JSON.toJSONString(hotelDoc),XContentType.JSON));
  19. bulkRequest.add(new IndexRequest("hotel").
  20. id("2").
  21. source(JSON.toJSONString(hotelDoc),XContentType.JSON));
  22. bulkRequest.add(new IndexRequest("hotel").
  23. id("3").
  24. source(JSON.toJSONString(hotelDoc),XContentType.JSON));*/
  25. //3、发送请求
  26. client.bulk(bulkRequest,RequestOptions.DEFAULT);
  27. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/659177
推荐阅读
相关标签
  

闽ICP备14008679号