Elasticsearch-Rest-Client整合SpringBoot

本文档详细介绍了如何在本地安装Elasticsearch,并使用Elasticsearch-Rest-Client进行Java集成。首先,从官网下载并解压Elasticsearch,通过命令行启动服务。接着,创建一个名为service_search的模块,引入与Elasticsearch版本匹配的高阶REST客户端依赖。配置文件包括服务端口、应用名称和Nacos服务地址。启动类启用Spring Boot和Discovery Client。在配置类中,定义了一个RestHighLevelClient Bean,用于连接Elasticsearch的本地实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.安装ElasticSearch

1.官网下载ElasticSearch,解压即可。(下载地址:Download Elasticsearch | Elastic

2.进入安装目录下bin目录中,使用命令 ./elasticsearch 启动,启动输出信息结尾出现started 关键字,表示启动成功,使用./elasticsearch -d 命令使服务后台启动,程序默认端口为9200。

2.Elasticsearch-Rest-Client

Elasticsearch-Rest-Client:官方RestClient,封装了ES操作,API层次分明,上手简单

第一步:创建模块

创建模块service_search

 

第二步:依赖

引入依赖,注意版本要和elasticsearch版本一样。

<properties>
        <elasticsearch.version>7.8.0</elasticsearch.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
    </dependencies>

第三步:配置文件

# 服务端口
server.port=8007
# 服务名
spring.application.name=service-search
​
#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
​
​
# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

第四步:启动类

package com.wxj.search;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
​
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableDiscoveryClient
public class SearchApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(SearchApplication.class,args);
    }
}

第五步:配置类

package com.wxj.search.config;
​
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
​
@Configuration
public class ElasticSearchConfig {
​
    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }
​
    @Bean
    public RestHighLevelClient esRestClient(){
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http")));
        return client;
    }
}

根据引用,Maven坐标org.elasticsearch.client:elasticsearch-rest-high-level-client:6.8.3是指elasticsearch-rest-high-level-client6.8.3版本。这个库提供了与Elasticsearch进行REST通信的API。它可以用于在Java项目中与Elasticsearch进行交互。 根据引用,ElasticSearchConfig是一个配置类,它用于配置Elasticsearch客户端。这个类使用host和port属性来指定Elasticsearch的主机和端口。在elasticsearchClient()方法中,它创建一个RestHighLevelClient对象并返回。 根据引用,findAll()方法是一个测试方法,用于查询所有的产品。在这个方法中,它使用elasticsearch-rest-high-level-client版本为7.6.2的API来执行查询操作。请注意,如果在POM文件中同时引用了不同版本的elasticsearch-rest-high-level-client,可能会导致不兼容的情况。 至于文件 G:\maven\repository\org\elasticsearch\client\elasticsearch-rest-high-level-client\6.4.3\jackson-dataformat-yaml-2.8.10.jar,系统找不到指定的文件。这可能是因为该文件不存在或路径不正确。请确保该文件存在,并且指定的路径是正确的。 综上所述,G:\maven\repository\org\elasticsearch\client\elasticsearch-rest-high-level-client\6.4.3\jackson-dataformat-yaml-2.8.10.jar文件不存在或路径不正确。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [elasticsearch-rest-high-level-client-6.8.3-API文档-中文版.zip](https://download.csdn.net/download/qq_36462452/86090017)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot整合ElasticSearch-SpringData](https://blog.csdn.net/优快云877425287/article/details/119972048)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lycirsp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值