三、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中索引库与文档操作

相关推荐
vHelios18 分钟前
【电商项目】商品搜索开发复盘(1):根据需求拆解搜索接口的设计逻辑
java·微服务·es
极创信息39 分钟前
国产化信创适配认证高频术语:信创适配、软件自主可控、国产化率、代码溯源率、代码自主率、代码开源率是什么?
java·python·struts·eclipse·开源·php·hibernate
Data_Journal40 分钟前
什么是 CAPTCHA,它是如何工作的?
java·大数据·服务器·前端·数据库
mqiqe1 小时前
响应式流中的错误处理:Project Reactor 异常治理全体系
java·架构
我命由我123451 小时前
Android 开发问题:TopAppBar 和 topAppBarColors API is experimental...
android·java·java-ee·kotlin·android studio·android jetpack·android-studio
AC赳赳老秦1 小时前
语义采集进阶实战:利用 OpenClaw AI 语义识别自动提取网页核心信息,无需手动编写选择器
java·运维·服务器·python·信息可视化·deepseek·openclaw
AI人工智能+电脑小能手1 小时前
大白话说Java设计模式-23-桥接模式(源码剖析篇)
java·设计模式·jdbc·桥接模式·源码分析·awt·java logging
李高钢1 小时前
C# WPF Prism 进阶(二):区域(Region)与模块化(Module)
java·前端·数据库
long3161 小时前
枚举(Enums)
java·开发语言·数据库
墨雨晨曦882 小时前
2026/08/15 spring AI学习总结
java·tomcat