SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch(index);
if (StringUtils.isNotEmpty(type)) {
searchRequestBuilder.setTypes(type.split(","));
}
searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);
// 需要显示的字段,逗号分隔(缺省为全部字段)
if (StringUtils.isNotEmpty(fields)) {
searchRequestBuilder.setFetchSource(fields.split(","), null);
}
//排序字段
if(CollectionUtils.isNotEmpty(sortList)){
for (EsSort esSort : sortList){
searchRequestBuilder.addSort(esSort.getFieldName(),esSort.getSortOrder());
}
}
// 查询字段
if(CollectionUtils.isNotEmpty(matchList)){
for (EsMatchSearch esMatchSearch : matchList){
if (matchPhrase == Boolean.TRUE) {
boolQuery.must(QueryBuilders.matchPhraseQuery(esMatchSearch.getFieldName(), esMatchSearch.getValue()));
} else {
boolQuery.must(QueryBuilders.matchQuery(esMatchSearch.getFieldName(), esMatchSearch.getValue()));
}
}
}
// 范围查询字段
if(CollectionUtils.isNotEmpty(rangeList)){
for (EsRangeSearch esRangeSearch : rangeList){
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(esRangeSearch.getFieldName());
if(StringUtils.isNotBlank(esRangeSearch.getFrom())){
rangeQueryBuilder.from(esRangeSearch.getFrom());
}
if(StringUtils.isNotBlank(esRangeSearch.getTo())){
rangeQueryBuilder.to(esRangeSearch.getTo());
}
rangeQueryBuilder.includeLower(true)
.includeUpper(true);
boolQuery.must(rangeQueryBuilder);
}
}
searchRequestBuilder.setQuery(boolQuery);
// 分页应用
searchRequestBuilder.setFrom(currentPage).setSize(pageSize);
// 设置是否按查询匹配度排序
searchRequestBuilder.setExplain(true);
// 执行搜索,返回搜索响应信息
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
long totalHits = searchResponse.getHits().totalHits;
long length = searchResponse.getHits().getHits().length;
logger.debug("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length);用查询条件直接在es里查是正常的,通过代码的话结果集里总是少一条记录。求问有可能是什么原因呢?
添加回答
举报
0/150
提交
取消