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服务器中的数据是否一致

 

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

菜鸟学习JAVA开发

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

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

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

打赏作者

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

抵扣说明:

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

余额充值