learn and grow up

spring-data-es抽象service集成简单增删查改之二

字数统计: 310阅读时长: 1 min
2020/02/22 Share

写在前面

接一

代码

继续整理了后续的实现类,注释已经比较清楚了,不在此赘述

主要是基于ElasticsearchRepository来实现简单的增删改查的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
* 公共查询的抽象service,基于spring-data-es,提供简单的增删查改
* @author yyt
* @param <M>
* @param <S>
*/
public abstract class EsAbstractDocService<M extends ElasticsearchRepository<S,String>,S extends BaseDoc> implements EsDocService<S> {

@Autowired
private M elasticsearchRepository;

@Override
public S saveOrUpdate(S s){
prepareSave(s);
elasticsearchRepository.save(s);
return s;
}

/**
* 根据业务主键判断是否已经有业务数据,有的话则填充es的id
* @param s
*/
protected abstract void prepareSave(S s);

@Override
public S delete(String id){
if(StringUtils.isEmpty(id)){
return null;
}
Optional<S> os=elasticsearchRepository.findById(id);
S s=os.get();
s.setDeleted(BaseConstant.FILED_DELETED);
s.setUpdatedTime(System.currentTimeMillis());
elasticsearchRepository.save(s);
return s;
}



@Override
public <E extends BaseDto> Iterable<S> search(E e){
QueryBuilder qbs=buildCommonQuery(e);

buildOwnQuery(qbs,e);
return elasticsearchRepository.search(qbs);
}

@Override
public <E extends BaseDto> Map<String,Object> searchByPage(E dto){
QueryBuilder qbs=buildCommonQuery(dto);

buildOwnQuery(qbs,dto);
SearchQuery query = buildCommonPageSearch(qbs,dto);
Page<S> rep= elasticsearchRepository.search(query);
Map<String,Object> rem=new HashMap(2);

rem.put("total",rep.getTotalElements());
rem.put("data",rep.getContent());
return rem;
}

@Override
public Iterable<S> search(QueryBuilder var1){
return elasticsearchRepository.search(var1);
}


}
CATALOG
  1. 1. 写在前面
  2. 2. 代码