ES好处和作用就不多说了,但是查询让人烦躁,看了无数大佬作品,几乎没有封装的方法。一个表一个查询方法,让人苦不堪言
public <T> Page<T> getList(T t,Pageable page){
Class<?> classType = t.getClass();
Field[] fields = classType.getDeclaredFields();
for(Field declaredfield:fields){
declaredfield.setAccessible(true);
}
BoolQueryBuilder query = QueryBuilders.boolQuery();
for(Field f:fields){
Type fieldType =f.getGenericType();
String field = f.toString().substring(f.toString().lastIndexOf(".")+1);
Object val = f.get(t);
if(!ObjectUtils.isEmpty(val)){
setQuery(fieldType.getTypeName,field,val.toString(),query);;
}
}
NativeSearchQuery queryParas = new NativeSearchQueryBuilder(query).withPageable(page).build();
queryParas.setTrackTotalHits(true