写在前面
接一
代码
继续整理了后续的实现类,注释已经比较清楚了,不在此赘述
主要是基于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;
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; }
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); }
}
|