SpringBoot官方文档:
https://spring.io/projects/spring-data-elasticsearch
https://github.com/spring-projects/spring-data-elasticsearch
ES官方文档:
https://www.elastic.co/guide/en/elasticsearch/reference/7.x/index.html
1. 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2 添加配置
此处使用rest方式操作es。除此还有 spring.elasticsearch.jest 方式可选择
# es配置
spring.elasticsearch.rest.uris=http://localhost:9200
spring.elasticsearch.rest.connection-timeout=1s
spring.elasticsearch.rest.read-timeout=30s
3 创建ES document映射
3.1 document映射类
package com.example.demo.es;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import javax.persistence.Id;
/**
* @author Aaron
* @date 2020/3/8
*/
@Document(indexName = "index_test", type = "type_user", shards = 1, replicas = 1)
public class UserDO {
@Id
@Field(type = FieldType.Keyword)
private Integer id;
// ik_max_word:会将文本做最细粒度的拆分
// ik_smart:会将文本做最粗粒度的拆分
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String name;
@Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart")
private String phone;
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String address;
public UserDO() {
}
public UserDO(Integer id, String name, String phone, String address) {
this.id = id;
this.name = name;
this.phone = phone;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
3.2 数据操作repository
package com.example.demo.es;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
/**
* @author Aaron
* @date 2020/3/8
*/
public interface UserDORepository extends CrudRepository<UserDO, Long> {
List<UserDO> findByName(String name);
List<UserDO> findByPhone(String phone);
List<UserDO> findByAddress(String address);
}
配置扫描Document映射类所在包
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
/**
* @author Aaron
* @date 2020/3/8
*/
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.example.demo.es")
public class EsConfig {
}
3.3 Service、Controller 代码
package com.example.demo.es;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @author Aaron
* @date 2020/3/8
*/
@Service
public class UserEsService {
@Resource
private UserDORepository repository;
@Resource
private ElasticsearchRestTemplate elasticsearchTemplate;
public void init() {
elasticsearchTemplate.createIndex(UserDO.class);
repository.deleteAll();
UserDO user1 = new UserDO(1, "Tom", "13800138000", "广东省深圳市南山区");
UserDO user2 = new UserDO(2, "Aaron", "13800138001", "北京市朝阳区");
repository.save(user1);
repository.save(user2);
}
public void save(UserDO user) {
repository.save(user);
}
public List<UserDO> findByAddress(String address) {
return repository.findByAddress(address);
}
}
package com.example.demo.controller;
import com.example.demo.es.UserDO;
import com.example.demo.es.UserEsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @author Aaron
* @date 2020/3/8
*/
@RestController
@RequestMapping("es")
public class EsController {
@Resource
private UserEsService userEsService;
@GetMapping("init")
public String init() {
userEsService.init();
return "success";
}
@GetMapping("save")
public String save(UserDO user) {
userEsService.save(user);
return "success";
}
@GetMapping("findByAddress")
public List<UserDO> findByAddress(String address) {
return userEsService.findByAddress(address);
}
}
4 测试
4.1 启动项目
4.2 创建ES索引,初始化数据
4.3 查询数据
http://localhost:8081/es/findByAddress?address=%E6%9C%9D%E9%98%B3
done