SpringBoot整合SpringDataElasticSearch操作ES
(1)、添加starter依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-data-elasticsearch</artifactId> 4 </dependency>
(2)、配置相关属性
1 spring.data.elasticsearch.cluster-name=docker-cluster 2 spring.data.elasticsearch.cluster-nodes=192.168.205.128:9300 3 #设置连接超时时间 4 spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s
*cluster-name可以在ip:9200里面查看
*SpringDataElasticSearch与ES版本之间有相应的适配关系,低版本的SpringDataElasticSearch不兼容高版本的ES。
解决方案:
1.升级SpringBoot版本
2.降级ES版本。
(3)、操作ES
1.第一种方式:使用ElasticsearchRepository,类似于JPA
1)在Bean上标注所属索引和所属类型
1 package cn.coreqi.entities; 2 3 import org.springframework.data.annotation.Id; 4 import org.springframework.data.elasticsearch.annotations.Document; 5 import java.io.Serializable; 6 7 @Document(indexName = "coreqi",type = "user") //文档 8 public class EsUser implements Serializable { 9 @Id //主键 10 private String id; //ES中id不能定义为Long 11 private String username; 12 private String password; 13 private Integer enabled; 14 15 public EsUser() { 16 } 17 18 public EsUser(String id, String username, String password, Integer enabled) { 19 this.id = id; 20 this.username = username; 21 this.password = password; 22 this.enabled = enabled; 23 } 24 25 public String getId() { 26 return id; 27 } 28 29 public void setId(String id) { 30 this.id = id; 31 } 32 33 public String getUsername() { 34 return username; 35 } 36 37 public void setUsername(String username) { 38 this.username = username; 39 } 40 41 public String getPassword() { 42 return password; 43 } 44 45 public void setPassword(String password) { 46 this.password = password; 47 } 48 49 public Integer getEnabled() { 50 return enabled; 51 } 52 53 public void setEnabled(Integer enabled) { 54 this.enabled = enabled; 55 } 56 57 @Override 58 public String toString() { 59 return "EsUser{" + 60 "id='" + id + '\'' + 61 ", username='" + username + '\'' + 62 ", password='" + password + '\'' + 63 ", enabled=" + enabled + 64 '}'; 65 } 66 }
2)编写一个ElasticsearchRepository
1 package cn.coreqi.repository; 2 3 import cn.coreqi.entities.EsUser; 4 import org.springframework.data.domain.Page; 5 import org.springframework.data.domain.Pageable; 6 import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 7 8 /** 9 * 第一种方式,类似于JPA,编写一个ElasticsearchRepository 10 * 第一个泛型为Bean的类型 11 * 第二个泛型为Bean的主键类型 12 */ 13 public interface EsUserRepository extends ElasticsearchRepository<EsUser,String> { 14 Page<EsUser> findEsUsersByUsername(String username, Pageable pageable); 15 }
3)测试
1 package cn.coreqi.repository; 2 import static org.assertj.core.api.Assertions.assertThat; 3 import cn.coreqi.entities.EsUser; 4 import org.junit.Before; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.boot.test.context.SpringBootTest; 9 import org.springframework.data.domain.Page; 10 import org.springframework.data.domain.PageRequest; 11 import org.springframework.data.domain.Pageable; 12 import org.springframework.test.context.junit4.SpringRunner; 13 14 @RunWith(SpringRunner.class) 15 @SpringBootTest 16 public class EsUserRepositoryTests { 17 18 @Autowired 19 private EsUserRepository esUserRepository; 20 21 @Before 22 public void initRepositoryData(){ 23 //清除ES中数据以免影响测试结果 24 esUserRepository.deleteAll(); 25 esUserRepository.save(new EsUser("1","fanqi","admin",1)); 26 esUserRepository.save(new EsUser("2","fanqi","root",1)); 27 esUserRepository.save(new EsUser("3","fanqi","sa",1)); 28 } 29 30 @Test 31 public void contextLoads() { 32 Pageable pageable = PageRequest.of(0,20); 33 Page<EsUser> result = esUserRepository.findEsUsersByUsername("fanqi",pageable); 34 assertThat(result.getTotalElements()).isEqualTo(3); 35 System.out.println("-----开始遍历结果数据-----"); 36 for (EsUser user:result.getContent()){ 37 System.out.println(user.toString()); 38 } 39 } 40 41 }
4)调用
1 @Autowired 2 private EsUserRepository esUserRepository; 3 @RequestMapping("/esusers") 4 public List<EsUser> list(@RequestParam("username") String username, 5 @RequestParam(value = "pageIndex",defaultValue = "0") int pageIndex, 6 @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){ 7 Pageable pageable = PageRequest.of(pageIndex,pageSize); 8 Page<EsUser> result = esUserRepository.findEsUsersByUsername("fanqi",pageable); 9 return result.getContent(); 10 }
2.第二种方式:使用ElasticsearchTemplate
1)调用
1 @Autowired 2 private ElasticsearchOperations elasticsearchOperations; 3 4 public String add2() { 5 User user = new User(1,"fanqi","123456",1); 6 IndexQuery indexQuery = new IndexQueryBuilder().withObject(user).withIndexName("coreqi").withType("user").build(); 7 elasticsearchOperations.index(indexQuery); 8 return "seccess"; 9 } 10 11 public User search2() throws IOException { 12 13 CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria() 14 .and(new Criteria("userName").is("fanqi"))); 15 return elasticsearchOperations.queryForObject(criteriaQuery,User.class); 16 17 // SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.termQuery("userName","fanqi")).withIndices("coreqi").withTypes("user").build(); 18 // List<User> list = elasticsearchOperations.queryForList(searchQuery, User.class); 19 // for(User s : list){ 20 // System.out.println(s.toString()); 21 // } 22 }