SpringBoot整合ElasticSearch(elasticsearch-rest-high-level-client)

一、引入maven坐标

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.4.2</version>
</dependency>

二、指定版本

 <properties>
        <elasticsearch.version>7.4.2</elasticsearch.version>
    </properties>

有人会说引坐标的时候不是已经指定了吗,但是我们可以通过dependencies看到版本号为6.8.6

因为springboot提供的默认版本号就是这个所以需要我们手动修改

三、编写配置类

让spring帮我们创建对象

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//给容器中注入一个 RestHighLevelClient
@Configuration
public class GulimallElasticSearchConfig {
        @Value("${hostname}")

        private String hostname;
    @Value("${port}")
        private Integer port;
    @Value("${scheme}")
        private String scheme;

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getScheme() {
        return scheme;
    }

    public void setScheme(String scheme) {
        this.scheme = scheme;
    }

    @Bean
         public RestHighLevelClient    esRestClient(){
             RestHighLevelClient client = new RestHighLevelClient(
                     RestClient.builder(
                             new HttpHost(hostname,port,scheme)
                     )
             );
             return client;
         }

}

 四、编写配置文件

将ip和端口号通过配置文件进行获取,以后可以不用操作代码,在application中写入

hostname=192.168.116.170
port =9200
scheme=http

通过下面方式获取,在配置类中已经写好

  @Value("${hostname}")
        private String hostname;
    @Value("${port}")
        private Integer port;
    @Value("${scheme}")
        private String scheme;

五、测试

import org.junit.jupiter.api.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;
@RunWith(SpringRunner.class)
@SpringBootTest
class GulimallSearchApplicationTests {
    @Autowired
        private RestHighLevelClient client;
    @Test
    void indexData() throws IOException {
        IndexRequest indexRequest = new IndexRequest("user");
        System.out.println(client);
        User user = new User();
        user.setAge(18);
        user.setName("jjs");
        user.setSex("男");
        String JsonString  = JSON.toJSONString(user);
        indexRequest.source(JsonString, XContentType.JSON);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值