问答详情
源自:-

es6.2.4,使用bool查询查出的结果,SearchResponse的Hits[]总是比totalHits少一条记录

searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);
// 查询字段
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);

https://img1.mukewang.com/5c3ef81c0001431e08140078.jpg


用查询条件直接在es里查是正常的,通过代码的话结果集里总是少一条记录。求问有可能是什么原因呢?

提问者:zxkfeel 2019-01-16 17:24

个回答

  • 爱学习的小大风
    2020-10-15 11:09:11

    和哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈和!