guopengfeiheze 2020-07-13 18:36 采纳率: 42.9%
浏览 1484
已采纳

java 进行ES查询时查询不到数据

我在java 中使用Java High Level REST Client进行ES的聚合查询,但是实际运行过程中无法查询到结果,并且调试时发现有错误:
代码如下:

    public Map<String, Object> testQueryByPage(ElasticsearchConfig elasticsearchConfig, String[] indices,
                                               String sortField,
                                String fieldKey, String fieldVal, int from, int size) throws IOException
    {
        BoolQueryBuilder boolQuery = new BoolQueryBuilder();
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery(sortField).gte(null); // "count"
        boolQuery.filter(rangeQuery);

        MatchQueryBuilder matchQuery = new MatchQueryBuilder(fieldKey, fieldVal);
        boolQuery.must(matchQuery);

        if (client == null) {
            initClient(elasticsearchConfig);
        }

        SearchResponse searchResponse = client
            .search(
            new SearchRequest(
                indices)
                    .source(new SearchSourceBuilder().query(boolQuery).from(from).size(size)
                        .trackTotalHits(true)),
            RequestOptions.DEFAULT);

        // System.out.println(response.getHits().getTotalHits());
        // System.out.println(response.toString());
        // closeClient();

        SearchHits hits = searchResponse.getHits();
        TotalHits totalHits = hits.getTotalHits();
        long totalSize = totalHits.value;
        SearchHit[] searchHits = hits.getHits();

        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        Map<String, Object> sourceMap = null;
        for (SearchHit searchHit : searchHits) {
            sourceMap = searchHit.getSourceAsMap();
            resultList.add(sourceMap);
        }
        closeClient();
        logger.info("单条件分页查询时,查询出的总条数为:【{}】", totalSize);
        resultMap.put(BobfintechContant.DATA_LIST, resultList);
        resultMap.put(BobfintechContant.DATA_SIZE, totalSize);
        return resultMap;
    }

        private void initClient(ElasticsearchConfig elasticsearchConfig)
    {
              client = new RestHighLevelClient(RestClient.builder(httpHosts));
        }

实际运行中发现用下面的代码查询到的总条数为1(这是正确的)

 TotalHits totalHits = hits.getTotalHits();
 long totalSize = totalHits.value;

但是使用下面的代码获取查询结果时

SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();

显示如下错误:

org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.

这个错误就在调试时把鼠标定位在searchHits上出现的,如果不调试的话,直接返回的是空的数据,请问大佬们,这个问题是什么原因?

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2020-07-14 14:18
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 10月27日

悬赏问题

  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了
  • ¥15 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询