设为首页 加入收藏

TOP

Elasticsearch rest-high-level-client 基本操作(一)
2023-07-25 21:27:30 】 浏览:58
Tags:Elasticsearch rest-high-level-client

Elasticsearch rest-high-level-client 基本操作

本篇主要讲解一下 rest-high-level-client 去操作 Elasticsearch , 虽然这个客户端在后续版本中会慢慢淘汰,但是目前大部分公司中使用Elasticsearch 版本都是6.x 所以这个客户端还是有一定的了解

image-20221025135725969

前置准备

  • 准备一个SpringBoot环境 2.2.11 版本
  • 准备一个Elasticsearch 环境 我这里是8.x版本
  • 引入依赖 elasticsearch-rest-high-level-client 7.4.2

1.配置依赖

注意: 我使用的是 springboot 2.2.11 版本 , 它内部的 elasticsearch 和 elasticsearch-rest-client 都是 6.8.13 需要注意

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
       <!-- 引入 elasticsearch 7.4.2  -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.4.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-api</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

      <!-- 排除 elasticsearch-rest-client , 也可不排除 为了把maven冲突解决   -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.4.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-client</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>elasticsearch</artifactId>
                    <groupId>org.elasticsearch</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 不引入会导致可能 使用 springboot的 elasticsearch-rest-client 6.8.13 -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.4.2</version>
        </dependency>

        <!-- elasticsearch 依赖 2.x 的 log4j -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
            <!--  排除掉 log4j-api 因为springbootstarter 中引入了loging模块 -->
            <exclusions>
                <exclusion>
                    <artifactId>log4j-api</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- junit 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

2.构建 RestHighLevelClient

highlevelclient 是 高级客户端 需要通过它去操作 Elasticsearch , 它底层也是要依赖 rest-client 低级客户端

@Slf4j
public class TestEsClient {

    private RestHighLevelClient client = null;
    private ObjectMapper objectMapper = new ObjectMapper();
		
    //构建 RestHighLevelClient
    @Be
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇SpringCloud(一) - Dubbo + Zooke.. 下一篇28基于java的简单酒店数据管理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目