标签:pos zab termquery xtend dep .com image ide pid
(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
*cluster-name可以在ip:9200里面查看
*SpringDataElasticSearch与ES版本之间有相应的适配关系,低版本的SpringDataElasticSearch不兼容高版本的ES。
解决方案:
1.升级SpringBoot版本
2.降级ES版本。
(3)、操作ES
1.第一种方式:使用ElasticsearchRepository,类似于JPA
1)编写一个ElasticsearchRepository
1 package cn.coreqi.repository; 2 3 import cn.coreqi.entities.User; 4 import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; 5 6 /** 7 * 第一种方式,类似于JPA,编写一个ElasticsearchRepository 8 * 第一个泛型为Bean的类型 9 * 第二个泛型为Bean的主键类型 10 */ 11 public interface UserRepository extends ElasticsearchRepository<User,Integer> { 12 public User findByUserName(String UserName); 13 }
2)在Bean上标注所属索引和所属类型
1 package cn.coreqi.entities; 2 3 import org.springframework.data.elasticsearch.annotations.Document; 4 5 import java.io.Serializable; 6 7 @Document(indexName = "coreqi",type = "user") 8 public class User implements Serializable { 9 private Integer id; 10 private String userName; 11 private String passWord; 12 private Integer enabled; 13 14 public User() { 15 } 16 17 public User(Integer id, String userName, String passWord, Integer enabled) { 18 this.id = id; 19 this.userName = userName; 20 this.passWord = passWord; 21 this.enabled = enabled; 22 } 23 24 public Integer getId() { 25 return id; 26 } 27 28 public void setId(Integer id) { 29 this.id = id; 30 } 31 32 public String getUserName() { 33 return userName; 34 } 35 36 public void setUserName(String userName) { 37 this.userName = userName; 38 } 39 40 public String getPassWord() { 41 return passWord; 42 } 43 44 public void setPassWord(String passWord) { 45 this.passWord = passWord; 46 } 47 48 public Integer getEnabled() { 49 return enabled; 50 } 51 52 public void setEnabled(Integer enabled) { 53 this.enabled = enabled; 54 } 55 56 @Override 57 public String toString() { 58 return "User{" + 59 "id=" + id + 60 ", userName=‘" + userName + ‘\‘‘ + 61 ", passWord=‘" + passWord + ‘\‘‘ + 62 ", enabled=" + enabled + 63 ‘}‘; 64 } 65 }
3)调用
1 @Autowired 2 private UserRepository userRepository; 3 4 public String add() { 5 User user = new User(1,"fanqi","123456",1); 6 userRepository.index(user); 7 return "seccess"; 8 } 9 10 public User search() throws IOException { 11 User user = userRepository.findByUserName("fanqi"); 12 return user; 13 }
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 }
SpringBoot整合SpringDataElasticSearch操作ES
标签:pos zab termquery xtend dep .com image ide pid
原文地址:https://www.cnblogs.com/fanqisoft/p/10356698.html