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

相关推荐
wuminyu6 分钟前
Java FFM处理网络读写事件源码剖析
java·linux·c语言·jvm·c++
小江的记录本17 分钟前
【AI Agent】《2026年9月 AI Agent全栈开发技术选型专项面试宝典》
java·spring boot·python·spring·ai·面试·ai编程
梦梦代码精1 小时前
《LikeShop全产品技术硬核拆解:ThinkPHP8+Vue3+UniApp架构,私有化部署与二次开发实战指南》
java·低代码·系统架构·php·开源软件
一木 之林1 小时前
C/C++ 面向对象编程(OOP)(进阶)
java·c语言·c++
摇滚侠1 小时前
《SpringBoot 3:入门与应用实战》第 9 章 使用 WebMvc 开发应用 阅读笔记 2
java·spring boot·笔记
technology_x2 小时前
液冷超充站设备厂家哪家好?2026年功率与散热对比
java·开发语言
LayZhangStrive2 小时前
Agent开发 - MCP Client使用MCP Server的几种方式(Spring AI)
java·spring ai·mcp
仓三2 小时前
从 Prompt Engineering 到 Context Engineering:2026 年 Agent 性能提升的隐藏杠杆
java·prompt·context
许彰午2 小时前
07-SqlBuilder六法
java·开发语言·低代码·架构
Jinkxs2 小时前
SkyWalking - 生产级部署:集成 Elasticsearch 作为后端存储
大数据·elasticsearch·skywalking