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

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日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵