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

相关推荐
gb421528717 分钟前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
曾经的三心草28 分钟前
Python2-工具安装使用-anaconda-jupyter-PyCharm-Matplotlib
android·java·服务器
Metaphor69229 分钟前
Java 高效处理 Word 文档:查找并替换文本的全面指南
java·经验分享·word
ChinaRainbowSea43 分钟前
7. LangChain4j + 记忆缓存详细说明
java·数据库·redis·后端·缓存·langchain·ai编程
stormsha44 分钟前
飞算JavaAI炫技赛电商系统商品管理模块的架构设计与实现
java·架构·鸿蒙系统
minh_coo1 小时前
Spring框架事件驱动架构核心注解之@EventListener
java·后端·spring·架构·intellij-idea
翻滚丷大头鱼1 小时前
Java 集合Collection—List
java·开发语言
今生相伴9911 小时前
ELFK:企业级日志管理的完整解决方案——从入门到精通
运维·elk·elasticsearch
敲键盘的肥嘟嘟左卫门1 小时前
StringBuilder类的数据结构和扩容方式解读
java
索迪迈科技2 小时前
java后端工程师进修ing(研一版 || day40)
java·开发语言·学习·算法