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

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 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?