写在前面
最近项目中用到了大量而是es进行数据的整合和查询,在项目完成后将自己花点时间整理了下组件,然后放上来,后面也可以继续用到~
代码
直接上代码,太忙了,先整理了base类,以下为代码,都有注释,就不在此赘述了
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| import lombok.Data; import org.springframework.data.elasticsearch.annotations.Document;
@Data public class BaseDoc { private Long createdTime; private Long updatedTime; private Integer deleted; private String version; private String createdBy; private String updatedBy;
public String findPrimaryKey(){ throw new UnsupportedOperationException(); }
}
@Data public class BaseDto { private Integer deleted; private String version;
private Long startTime; private Long endTime;
private Long createBeginTime; private Long createEndTime; @NotNull(message = "startIndex不能为空") private Integer startIndex; @NotNull(message = "size不能为空") private Integer size; private SortBy sortBy; }
public class BaseConstant {
public static final Integer FILED_NO_DELETED=0;
public static final Integer FILED_DELETED=1;
}
@Data public class SortBy { private Byte field; private Byte type; }
public enum SortFieldEnum {
SortFieldEnum0(new Byte("1"),"updatedTime"), SortFieldEnum3(new Byte("2"),"createTime"), ;
SortFieldEnum(byte field,String name){ this.field=field; this.name=name; }
private Byte field; private String name;
public String getName() { return name; }
public Byte getField() { return field; } public static SortFieldEnum getVal(Byte field){ SortFieldEnum[] enums=values(); for(SortFieldEnum item:enums){ if (item.getField().equals(field)) { return item; } } return null; }
public static SortFieldEnum getSortField(Byte field){ return SortFieldEnum.getVal(field); } }
import org.elasticsearch.search.sort.SortOrder;
public enum SortTypeEnum { SortTypeEnum0(new Byte("0"), SortOrder.ASC), SortTypeEnum1(new Byte("1"),SortOrder.DESC);
SortTypeEnum(byte field, SortOrder type){ this.field=field; this.type=type; }
private Byte field; private SortOrder type;
public Byte getField(){ return field; }
public SortOrder getType() { return type; }
public static SortTypeEnum getVal(Byte field){ SortTypeEnum[] enums=values(); for(SortTypeEnum item:enums){ if (item.getField().equals(field)) { return item; } } return null; }
public static SortTypeEnum getSortType(Byte field){ return SortTypeEnum.getVal(field); } }
@FunctionalInterface public interface ScrollDataCallBack { Object handleData(List<Map<String, Object>> datas); }
|
以下为逻辑类
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
@NoRepositoryBean public interface EsDocService<T extends BaseDoc> {
T saveOrUpdate(T t);
T delete(String id);
Iterable<T> search(QueryBuilder var1);
<E extends BaseDto> Map<String,Object> searchByPage(E t);
<E extends BaseDto> Iterable<T> search(E t);
default <E extends BaseDto> QueryBuilder buildCommonQuery(E s){ BoolQueryBuilder qbs = QueryBuilders.boolQuery(); qbs.must(QueryBuilders.termQuery("deleted", BaseConstant.FILED_NO_DELETED)); String version= s.getVersion(); if(StringUtils.isNotBlank(version)){ qbs.must(QueryBuilders.termQuery("version", version)); }
if(s.getStartTime()!=null){ qbs.must(QueryBuilders.rangeQuery("updatedTime").gte(s.getStartTime())); } if(s.getEndTime()!=null){ qbs.must(QueryBuilders.rangeQuery("updatedTime").lte(s.getEndTime())); }
if(s.getCreateBeginTime()!=null){ qbs.must(QueryBuilders.rangeQuery("createTime").gte(s.getCreateBeginTime())); } if(s.getCreateEndTime()!=null){ qbs.must(QueryBuilders.rangeQuery("createTime").lte(s.getCreateEndTime())); }
return qbs; }
<E extends BaseDto> void buildOwnQuery(QueryBuilder qbs, E s);
default <E extends BaseDto> SearchQuery buildCommonPageSearch(QueryBuilder qbs, E dto){ Pageable pageable= PageRequest.of(dto.getStartIndex(),dto.getSize()); FieldSortBuilder fsb = SortBuilders.fieldSort(SortFieldEnum.getSortField(dto.getSortBy().getField()).getName()) .order(SortTypeEnum.getSortType(dto.getSortBy().getType()).getType()); SearchQuery query = new NativeSearchQueryBuilder().withQuery(qbs).withSort(fsb) .withPageable(pageable).build(); return query; } }
|