我在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上出现的,如果不调试的话,直接返回的是空的数据,请问大佬们,这个问题是什么原因?