提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
Elasticsearch官网参考文档:https://www.elastic.co/guide/index.html
Elasticsearch官方下载地址:https://www.elastic.co/cn/downloads/elasticsearch
https://docs.spring.io/spring-data/elasticsearch/reference/
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
修改yml配置文件
server:
port: 8082
spring:
elasticsearch:
rest:
uris: 192.168.25.131:9200 #集群 后面加 ,
一、实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
//indexName 索引库名称 名字如果是字母那么必须是小写字母
//createIndex 是否创建索引
@Document(indexName = "student",createIndex = true)
public class Student {
@Id
@Field(store = true, type = FieldType.Keyword)
private String sId;
@Field(store = true, type = FieldType.Keyword)
private String sName;
@Field(store = true, type = FieldType.Text, analyzer = "ik_smart")
//Text可以分词 ik_smart=粗粒度分词 ik_max_word 为细粒度分词
private String sAddress;
@Field(index = false, store = true, type = FieldType.Integer)
private Integer sAge;
@Field(index = false, store = true, type = FieldType.Date, format = DateFormat.basic_date_time)
private Date sCreateTime;
@Field(type = FieldType.Keyword)
private String[] sCourseList; //数组类型 由数组中第一个非空值决定(这里数组和集合一个意思了)
@Field(type = FieldType.Keyword)
private List<String> sColorList; //集合类型 由数组中第一个非空值决定
}
二、继承ElasticsearchRepository类
public interface EsStudentRepository extends ElasticsearchRepository<McMesgPushRecDetailVO, String> {
/**
* 自定义查询方法,方法名需要遵循【自定义方法命名约定】
* @param sName
* @param sAddress
* @return
*/
List<Student> findBySNameOrSAddress(String sName, String sAddress);
}
【自定义方法命名约定】:
二、ElasticsearchRepository和ElasticsearchRestTemplate使用步骤
1.接口类
代码如下(示例):
public interface EsStudentService {
/**
* 保存到ES中
* @param Student
*/
public void save(Student student);
/**
* 根据消息内容或手机号查询ES中的结果
* @param messageContents
* @param contactContents
* @return
*/
public List<Student> findBySNameOrSAddress(String sName, String sAddress);
/**
* 批量添加或更新文档
* @param list
*/
public void saveOrUpdate(List<Student> list);
/**
* 查询
* @param param value
* @return
*/
public List<Student> query(String param,String value,Page page);
}
2.实现方法
@Service
public class EsStudentServiceImpl implements EsStudentService {
@Resource
private EsStudentRepository studentRepository;
/**
* spring容器已存在此bean实例,直接注入即可
*/
@Resource
private ElasticsearchRestTemplate elasticsearchRestTemplate;
/**
* 保存消息详情到ES中
* @param detailVO
*/
@Override
public void save(Student student) {
//ElasticsearchRepository封装好的方法,还有更多其他封装好的方法
studentRepository.save(student);
}
/**
* 从ES中查询结果
* @param sName
* @param sAddress
* @return
*/
@Override
public List<student> findBySNameOrSAddress(String messageContents, String contactContents) {
//自定义查询方法
return studentRepository.findBySNameOrSAddress(String sName, String sAddress);
}
/**
* 批量保存或更新
* @param list
*/
@Override
public void saveOrUpdate(List<Student> list){
if (!CollectionUtils.isEmpty(list)){
List<IndexQuery> indexQueries = new ArrayList<>();
for (Student student : list){
IndexQuery indexQuery = new IndexQuery();
indexQuery.setObject(student );
indexQueries.add(indexQuery);
}
elasticsearchRestTemplate.bulkIndex(indexQueries, IndexCoordinates.of("student"));
}
}
/**
* 查询 分页
* @param param
* @return
*/
@Override
public List<Student> query(String param, String value, Page page){
List<Student> list = new ArrayList<>();
BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
List<String> params = Arrays.asList(param.split(";"));
for (String queryColumn : params){
queryBuilder.must(QueryBuilders.fuzzyQuery(queryColumn, value));
}
queryBuilder.filter();
NativeSearchQuery query = new NativeSearchQueryBuilder()
//过滤
.withQuery(queryBuilder)
//分页
.withPageable(PageRequest.of(page.getPageNumber(), page.getPageSize()))
.build();
SearchHits<Student> search = elasticsearchRestTemplate.search(query, Student.class);
if(search.getTotalHits()>0){
search.forEach(userSearchHit ->{
list.add(userSearchHit.getContent());
});
}
return list;
}
}
3.接口测试
/**
* 注入
**/
@Resource
private EsStudentService esStudentService;
//ElasticsearchRepository
@RequestMapping("/esTest")
public String esTest(@RequestBody Student student){
esStudentService.save(vo);
return "res";
}
//ElasticsearchRepository
@RequestMapping("/esTest2")
public String esTest(@RequestParam String sName){
List<McMesgPushRecDetailVO> list = esStudentService.findBySNameOrSAddress(messageContents,null);
return JSONArray.toJSONString(list);
}
//ElasticsearchRestTemplate
@RequestMapping("/esTest3")
public String esTest3(@RequestParam String sName){
Page page = new Page();
page.setPageNumber(1);
page.setPageSize(10);
List<Student> list = esStudentService.query("sName",sName,page);
return JSONArray.toJSONString(list);
}
//ElasticsearchRestTemplate
@RequestMapping("/esTest4")
public String esTest4(@RequestBody Student student){
List<Student> list = new ArrayList<>();
list.add(vo);
esMcMesgPushRecDetailService.saveOrUpdate(list);
return "res";
}
总结
如果注入失败ElasticsearchRepository:文章来源:https://www.toymoban.com/news/detail-855315.html
1.@EnableElasticsearchRepositories 添加此注解扫描Repository
2.检查自定义方法是否符合约束文章来源地址https://www.toymoban.com/news/detail-855315.html
到了这里,关于使用ElasticsearchRepository和ElasticsearchRestTemplate操作Elasticsearch,Spring Boot整合Elasticsearch的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!