/**
* @author BNTang
*/
public class ElasticQueryUtils {
public static NativeSearchQueryBuilder buildSearchQuery(CommonPageVO queryVO, BoolQueryBuilder queryBuilder, String index, String type) {
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder()
.withIndices(index)
.withTypes(type);
if (Objects.nonNull(queryVO)) {
int currentPage = Optional.ofNullable(queryVO.getCurrentPage()).orElse(1);
int pageSize = Optional.ofNullable(queryVO.getPageSize()).orElse(10);
// 获取当前页码数
Pageable pageable = PageRequest.of(currentPage - 1, pageSize);
searchQueryBuilder.withPageable(pageable);
}
if (Objects.nonNull(queryBuilder)) {
searchQueryBuilder.withQuery(queryBuilder);
}
Collection<FieldSort> fieldSorts = Objects.nonNull(queryVO) && queryVO instanceof CommonElasticQueryVO ? ((CommonElasticQueryVO) queryVO).getFieldSorts() : Collections.emptyList();
if (CollectionUtils.isEmpty(fieldSorts)) {
searchQueryBuilder.withSort(SortBuilders.fieldSort("_id").order(SortOrder.DESC));
} else {
fieldSorts.forEach(e -> {
searchQueryBuilder.withSort(SortBuilders.fieldSort(e.getField()).order(SortOrder.valueOf(e.getOrder())));
});
}
return searchQueryBuilder;
}
}