• RestClient操作索引库


    RestClient操作索引库

    利用JavaRestClient实现创建、删除索引库,判断索引库是否存在

    环境准备:

    创建索引

    #酒店的mapping
    PUT /hotel
    {
      "mappings": {
        "properties": {
          "id":{
            "type": "keyword"
          },
          "name":{
            "type": "text",
            "analyzer": "ik_max_word",
             "copy_to": "all"
          },
          "address":{
            "type":"keyword",
            "index": false
          },
          "price":{
            "type": "integer"
          },
          "score":{
            "type": "integer"
          },
          "brand":{
            "type": "keyword",
            "copy_to": "all"
          },
          "city":{
            "type": "keyword"
          },
          "starName":{
            "type": "keyword"
          },
          "business":{  
             "type": "keyword",
             "copy_to": "all"
          },
       
          "location":{
            "type":"geo_point"
          },
          "pic":{
            "type": "keyword",
            "index": false
          },
          "all":{
             "type": "text",
            "analyzer": "ik_max_word"
          }
          
          
          
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    ES中支持两种地理坐标数据类型:

    geo_point:由纬度和经度确定的一个点

    geo_shape:有多个geo_point组成的复杂几何图形,例如一条直线

    加入elasticsearch的依赖坐标

    
        org.elasticsearch.client
        elasticsearch-rest-high-level-client
        7.12.1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、因为SpringBoot默认版本ES是7.6.2

    修改properties保证elasticsearch版本一致

    image-20220816174741896

        
            1.8
            7.12.1
        
    
    • 1
    • 2
    • 3
    • 4

    3.初始化RestHighLevelClient

    @SpringBootTest
    public class HotelIndexTest {
        private RestHighLevelClient client;
    
        @Test
        void testInit(){
            System.out.println(client);
        }
    
        @BeforeEach
        void setUP(){
            this.client=new RestHighLevelClient(RestClient.builder(
                    HttpHost.create("http://192.168.205.128:9200")
            ));
        }
        @AfterEach
        void tearDown() throws IOException {
            this.client.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    创建索引库

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

    其中的MAPPING_TEMPLATE是定义的常量用来存放

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

    删除索引库、判断索引库是否存在

    //删除索引库
    @Test
    void testDeleteHodelIndex() throws IOException {
        //创建request对象
        DeleteIndexRequest request=new DeleteIndexRequest("hotel");
    
        //发送请求
        client.indices().delete(request,RequestOptions.DEFAULT);
    }
    //判断是否存在
    
    @Test
    void testExistsIndexHotelIndex() throws IOException {
        //创建resquest对象
        GetIndexRequest request=new GetIndexRequest("hotel");
        //发送请求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists?"索引库存在":"索引库不存在");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    利用JavaRestClient实现文档的CRUD

    去数据库中查询酒店数据,导入到hotel索引库,实现酒店数据的CRUD

    1.初始化JavaRestClient

    2.利用JavaRestClient新增酒店数据

    //添加文档数据
    @Test
    void testAddDoucment() throws IOException {
    
    
        //调用数据库查询
        Hotel hotel = hotelService.getById(36934);
        //准备Request对象
        IndexRequest request=new IndexRequest("hotel").id(hotel.getId().toString());
        //准备JSON文档
        HotelDoc hotelDoc = new HotelDoc(hotel);
        //准备请求的参数
        request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
    
        client.index(request,RequestOptions.DEFAULT);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    HotelDoc实体类

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

    3.利用JavaRestClient根据id查询酒店

    //根据id查询数据
    @Test
    void testGetDocumentById() throws IOException {
        //准备Request对象
        GetRequest request=new GetRequest("hotel","36934");
        GetResponse documentFields = client.get(request, RequestOptions.DEFAULT);
        String json = documentFields.getSourceAsString();
        //反序列化
        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
        System.out.println(hotelDoc);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4.利用JavaRestClient删除酒店数据

    @Test
    void testDeleteDocument() throws IOException {
        DeleteRequest request=new DeleteRequest("hotel","36934");
    
        client.delete(request,RequestOptions.DEFAULT);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5.利用JavaRestClient修改酒店数据

    局部更新:

    //修改数据
    @Test
    void testUpdateDocument() throws IOException {
        UpdateRequest request = new UpdateRequest("hotel", "36934");
        request.doc(
                "price","666"
        );
    
        client.update(request,RequestOptions.DEFAULT);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    批量导入数据到ES

    @Test
    void testBulkRequest() throws IOException {
        //批量查询酒店数据
        List<Hotel> list = hotelService.list();
    
        //创建Request
        BulkRequest bulkRequest = new BulkRequest();
        //转换为文档类型
        for (Hotel hotel : list) {
            HotelDoc hotelDoc = new HotelDoc(hotel);
            bulkRequest.add(new IndexRequest("hotel")
                    .id(hotelDoc.getId().toString())
                    .source(JSON.toJSONString(hotelDoc),XContentType.JSON));
        }
        //添加请求
        client.bulk(bulkRequest, RequestOptions.DEFAULT);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    查询:

    GET /hotel/_search
    
    • 1

    image-20220817153155740

  • 相关阅读:
    视频监控系统/视频汇聚平台EasyCVR如何反向代理进行后端保活?
    【微信小程序】网络请求
    日志采集/分析
    sessionStorage、localStorage、cookie的缓存
    跟艾文学编程《Python基础》Anaconda 安装
    Swin-Transformer(2021-08)
    java程序设计案例教程王希军,渣本二面阿里受挫
    Pytorch之SwinTransformer图像分类
    Linux下动静态库的制作与使用
    基于TCP的Qt网络通信
  • 原文地址:https://blog.csdn.net/qq_57907966/article/details/126448092