SpringBoot整合Elasticsearch开发

Posted 菜鸟学习JAVA开发

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合Elasticsearch开发相关的知识,希望对你有一定的参考价值。

注意事项:

       在使用SpringBoot+Elasticsearch开发时,首要要安装Elasticsarch服务器。如果没有安装的可用查阅一下我前面发布的一篇"Elasticsearch7.3 的安装"的文章

1、首先使用IDEA创建一个maven项目。首先选择创建一个空的maven工程

然后点击下一步,添写maven工程的名称,maven工程的存放地址以及版本号

点击finish完成创建maven工程,随后可用把maven源缓存自己的,打开IDEA编辑器file>>settings>>build,execution,deployment>>build tools>>maven

 2、在pom.xml中引入spring-boot、spring-boot-web和elasticsearc的依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.mshangqs</groupId>
    <artifactId>springboot-es</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.5.10</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--elasticsearch相关依赖包-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.3.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

3、在application.properties文件中添加elasticsearch的配置

server.port=8088

# 配置elasticsearch服务器地址 127.0.0.1:9200,127.0.0.1:9300(多个可利用逗号进行分割)
chengbkj.elasticsearch.hosts = 127.0.0.1:9200

4、利用SpringBoot的自动管理进行连接

@Configuration
public class ElasticsearchConfig 

    @Value("$chengbkj.elasticsearch.hosts")
    public String hostList;

    @Bean
    public RestHighLevelClient restHighLevelClient()

        String[] hostArr = hostList.split(",");
        //1、创建一个httpHosts数组
        HttpHost[] httpHosts = new HttpHost[hostArr.length];

        for (int i = 0; i < hostArr.length; i++) 
            String item = hostArr[i];
            httpHosts[i] = new HttpHost(item.split(":")[0],Integer.parseInt(item.split(":")[1]),"http");
        

        return new RestHighLevelClient(RestClient.builder(httpHosts));
    


4、创建一个测试文件,并进行测试是否可用进行连接

package cn.mshangqs;


import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@SpringBootTest(classes = EsApplication.class)
@RunWith(SpringRunner.class)
public class ElApplicationTest

    @Autowired
    RestHighLevelClient restHighLevelClient;

    @Test
    public void testClient() throws IOException 
        //1 构建请求
        GetRequest getRequest = new GetRequest("book", "1");
        //2 执行
        GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        //3 获取请求结果
        System.out.println(getResponse.getId());
        System.out.println(getResponse.getVersion());
        System.out.println(getResponse.getSourceAsString());
    


查看控制台打印的结果:

 对比elasticsearch服务器中的数据是否一致

 

以上是关于SpringBoot整合Elasticsearch开发的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot 整合 Elasticsearch 实现海量级数据搜索

Springboot 2.5.x整合ElasticSearch 7.1x

SpringBoot检索篇Ⅳ --- 整合ElasticSearch

SpringBoot整合ElasticSearch7.x及实战

[ ElasticSearch ] SpringBoot整合ElasticSearch

[ ElasticSearch ] SpringBoot整合ElasticSearch