• 三、RestClient操作索引库与文档


    三、RestClient操作索引库与文档

    ES官方提供了各种不同语言的客户端,用来操作ES。这些客户端的本质就是组装DSL语句,通过http请求发送给ES

    官方文档地址: https://www.elastic.co/guide/en/elasticsearch/client/index.html

    数据库文件:视频里展示的数据库表可以使用自己有的其他数据替代,不一定非要一致。

    自己手敲了个工程项目(包含SQL文件):测试RestClient项目文件

    3.1 操作索引库

    设计数据表对应的mappings

    PUT /movie
    {
      "mappings": {
        "properties": {
          "all":{
            "type": "text",
            "analyzer": "ik_max_word"
          },
          "movieId":{
            "type": "keyword"
          },
          "movieTitle":{
            "type": "text",
            "analyzer": "ik_max_word", 
            "copy_to": "all"
          },
          "movieIntroduction":{
            "type": "text",
            "analyzer": "ik_max_word", 
            "copy_to": "all"
          },
          "movieRating":{
            "type": "float"
          },
          "movieReleaseDate":{
            "type": "keyword", 
            "copy_to": "all"
          }
        }
      }
    }
    
    • 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

    引入依赖

    <properties>
        <java.version>1.8java.version>
        <elasticsearch.version>7.12.1elasticsearch.version>
        <mybatis-plus-boot.version>3.4.2mybatis-plus-boot.version>
    properties>
    
    <dependency>
        <groupId>org.elasticsearch.clientgroupId>
        <artifactId>elasticsearch-rest-high-level-clientartifactId>
        <version>7.12.1version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    初始化

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

    创建movie索引,CREATE_MOVIE 为上面的 mappings

    public class MovieIndexTest {
        // ...........
        
        @Test
        void testCreateMovieIndex() throws IOException {
            // 创建Request
            CreateIndexRequest request = new CreateIndexRequest("movie");
            // 准备请求数据
            request.source(CREATE_MOVIE, XContentType.JSON);
            // 发送请求
            client.indices().create(request, RequestOptions.DEFAULT);
        }
        // ...........
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    删除、获取,判断是否存在

    public class MovieIndexTest {
    	@Test
        void testDelete() throws IOException {
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("movie");
            client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
        }
        @Test
        void testExists() throws IOException {
            GetIndexRequest getIndexRequest = new GetIndexRequest("movie");
            boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
            System.out.println(exists);
        }
        @Test
        void testGet() throws IOException {
            GetIndexRequest getIndexRequest = new GetIndexRequest("movie");
            GetIndexResponse getIndexResponse = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);
            System.out.println(getIndexResponse);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3.2 操作文档

    【TODO】

    结束语

    上一篇:二、ElasticSearch中索引库与文档操作

  • 相关阅读:
    vue2 编写自己的组件库,并发布到npm
    Kotlin 开发Android app(十四):startActivity跳转Activity活动
    【Python】Pycharm中设置使用conda的虚拟环境(保姆级图文)
    Go高性能之方法接收器 - 指针vs值
    selenium常见异常以及处理方法
    大模型中的token是什么?
    最长有效括号【python版】
    手动抄表和自动抄表优缺点对比
    C++ Qt高级开发视频教程
    【ArcGIS微课1000例】0051:Geodatabase子类型操作全解
  • 原文地址:https://blog.csdn.net/Array_dear/article/details/133907778