(1)、添加starter依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
(2)、配置相关属性
spring.data.elasticsearch.cluster-name=docker-cluster
spring.data.elasticsearch.cluster-nodes=192.168.205.128:9300
#设置连接超时时间
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上标注所属索引和所属类型
package cn.coreqi.entities; import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.io.Serializable; @Document(indexName = "coreqi",type = "user") //文档
public class EsUser implements Serializable {
@Id //主键
private String id; //ES中id不能定义为Long
private String username;
private String password;
private Integer enabled; public EsUser() {
} public EsUser(String id, String username, String password, Integer enabled) {
this.id = id;
this.username = username;
this.password = password;
this.enabled = enabled;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Integer getEnabled() {
return enabled;
} public void setEnabled(Integer enabled) {
this.enabled = enabled;
} @Override
public String toString() {
return "EsUser{" +
"id='" + id + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", enabled=" + enabled +
'}';
}
}
2)编写一个ElasticsearchRepository
package cn.coreqi.repository; import cn.coreqi.entities.EsUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; /**
* 第一种方式,类似于JPA,编写一个ElasticsearchRepository
* 第一个泛型为Bean的类型
* 第二个泛型为Bean的主键类型
*/
public interface EsUserRepository extends ElasticsearchRepository<EsUser,String> {
Page<EsUser> findEsUsersByUsername(String username, Pageable pageable);
}
3)测试
package cn.coreqi.repository;
import static org.assertj.core.api.Assertions.assertThat;
import cn.coreqi.entities.EsUser;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class EsUserRepositoryTests { @Autowired
private EsUserRepository esUserRepository; @Before
public void initRepositoryData(){
//清除ES中数据以免影响测试结果
esUserRepository.deleteAll();
esUserRepository.save(new EsUser("1","fanqi","admin",1));
esUserRepository.save(new EsUser("2","fanqi","root",1));
esUserRepository.save(new EsUser("3","fanqi","sa",1));
} @Test
public void contextLoads() {
Pageable pageable = PageRequest.of(0,20);
Page<EsUser> result = esUserRepository.findEsUsersByUsername("fanqi",pageable);
assertThat(result.getTotalElements()).isEqualTo(3);
System.out.println("-----开始遍历结果数据-----");
for (EsUser user:result.getContent()){
System.out.println(user.toString());
}
} }
4)调用
@Autowired
private EsUserRepository esUserRepository;
@RequestMapping("/esusers")
public List<EsUser> list(@RequestParam("username") String username,
@RequestParam(value = "pageIndex",defaultValue = "0") int pageIndex,
@RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
Pageable pageable = PageRequest.of(pageIndex,pageSize);
Page<EsUser> result = esUserRepository.findEsUsersByUsername("fanqi",pageable);
return result.getContent();
}
2.第二种方式:使用ElasticsearchTemplate
1)调用
@Autowired
private ElasticsearchOperations elasticsearchOperations; public String add2() {
User user = new User(1,"fanqi","123456",1);
IndexQuery indexQuery = new IndexQueryBuilder().withObject(user).withIndexName("coreqi").withType("user").build();
elasticsearchOperations.index(indexQuery);
return "seccess";
} public User search2() throws IOException { CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria()
.and(new Criteria("userName").is("fanqi")));
return elasticsearchOperations.queryForObject(criteriaQuery,User.class); // SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.termQuery("userName","fanqi")).withIndices("coreqi").withTypes("user").build();
// List<User> list = elasticsearchOperations.queryForList(searchQuery, User.class);
// for(User s : list){
// System.out.println(s.toString());
// }
}