guopengfeiheze 2020-07-13 18:36 采纳率: 50%
浏览 1221
已采纳

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条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 已采纳回答 10月27日

      悬赏问题

      • ¥15 layui upload.render 问题 有偿
      • ¥15 “with “is not allowed in strict mode
      • ¥15 ADS2020使用村田数据库仿真出错
      • ¥15 如何解决爬取网站不定时不返回数据
      • ¥20 有几个关于fpga的基础问题黑盒仿真,建立保持时间,和仿真覆盖理论。
      • ¥20 如何在NCBI上下载高通量的测序数据
      • ¥15 L型TFET的器件参数
      • ¥15 nacos启动失败,文件夹权限
      • ¥20 vb6.0窗体中的vscroll控件无法响应鼠标滚轮事件,请教方法
      • ¥15 如何在linux服务器做视频静态资源访问接口