SpringBoot配置elasticsearch rest template

Posted by zsh on May 18, 2021

最近项目中用到了Elasticsearch,需要在SpringBoot项目上配置,网上找了一圈发现都是使用ElasticsearchTemplate操作,官方最新的推荐是使用 ElasticsearchRestTemplate,基于HTTP协议与es交互。于是各种查资料,踩坑,在这里把一步步配置的过程记录一下。

  1. 首先创建一个SpringBoot项目,添加最基本的依赖和es的依赖(SpringBoot版本为2.3.3)
1
2
3
4
5
<dependency>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <groupId>org.springframework.boot</groupId>
</dependency>
  1. 创建es配置Bean类
1
2
3
4
5
6
7
8
@Configuration
public class ESConfig {
    @Bean
    public ElasticsearchRestTemplate elasticsearchRestTemplate(RestHighLevelClient client) {
        return new ElasticsearchRestTemplate(client);
    }
}
  1. 在配置文件添加es的配置
1
2
3
4
spring:
  elasticsearch:
    rest:
      uris: http://ip:9200
  1. 创建es的实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Document(indexName = "test-user", createIndex = false)
public class UserForSearch {
    @Id
    private int id;
    @Field(type = FieldType.Keyword)
    private String nickname;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "UserForSearch{" +
            "id=" + id +
            ", nickname='" + nickname + '\'' +
            '}';
    }
}
  1. 使用ElasticsearchTemplate添加,查询数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Component
class EsService {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    public void testEs() {
        elasticsearchRestTemplate.delete("1", UserForSearch.class);
        UserForSearch userForSearch = elasticsearchRestTemplate.get("1", UserForSearch.class);
        System.out.println(userForSearch);

        userForSearch = new UserForSearch();
        userForSearch.setId(1);
        userForSearch.setNickname("name");
        elasticsearchRestTemplate.save(userForSearch);
        userForSearch = elasticsearchRestTemplate.get("1", UserForSearch.class);

        System.out.println(userForSearch);
    }
}

运行结果如下

1
2
null
UserForSearch{id=1, nickname='name'}

第一次查询没有数据,输出null,然后调用save方法保存一条数据,接着查询并打印输出,刚才添加的数据成功显示。这样SpringBoot和es的配置就完成了,后面的 使用Repository也都大同小异,查找一下资料即可。