三、RestClient操作索引库与文档

文章目录

三、RestClient操作索引库与文档

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

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

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

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

3.1 操作索引库

设计数据表 对应的mappings

json 复制代码
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"
      }
    }
  }
}

引入依赖

xml 复制代码
<properties>
    <java.version>1.8</java.version>
    <elasticsearch.version>7.12.1</elasticsearch.version>
    <mybatis-plus-boot.version>3.4.2</mybatis-plus-boot.version>
</properties>

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

初始化

java 复制代码
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();
    }
}

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

java 复制代码
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);
    }
    // ...........
}

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

java 复制代码
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);
    }
}

3.2 操作文档

【TODO】

结束语

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

相关推荐
我要见SA姐16 小时前
用生活化类比当钥匙,一键打开 C++ 入门大门
java·开发语言·c++
LaughingZhu6 小时前
Product Hunt 热榜 2026-12-05:SoduWork、finerd split、Otari by Mozilla AI
java·人工智能·docker·chatgpt·容器
L-影7 小时前
HashMap详解-上篇
java·开发语言·spring·hashmap
爱笑的源码基地7 小时前
什么是智能预问诊系统?一套java版预问诊成品源码
java·源码·程序·预问诊·智能问诊·预问诊源码
她说..8 小时前
幂等问题分析
java·jvm·spring boot·spring·spring cloud·intellij-idea
摇滚侠8 小时前
《On Java 中文版 基础卷》阅读笔记 集合 24
java·开发语言·笔记
小小测试开发8 小时前
rsync 源路径少一个斜杠,--delete 删的就不是你以为的那一层
java·前端·数据库·人工智能
智购科技无人售货机工厂店9 小时前
自动售货机刷脸支付集成实战:活体检测与合规门槛~YH
java·服务器·前端·科技·零售
Nazarick10 小时前
力扣LeetCode热题75题(1)
java·算法·leetcode·力扣
岳涛_daemon10 小时前
数值照旧填、请求没报错,ES 9.3 把 max_size 换成按主分片算,索引要更大才滚动
elasticsearch