分类
Java 大数据 笔记 编程

Spring Data Elasticsearch兼容问题

使用Spring Data Elasticsearch查询Elasticsearch数据计数

NativeSearchQueryBuilder searchQueryBuilder1 = new NativeSearchQueryBuilder()
                    .withIndices(BURY_POINT_DURATION)
                    .withQuery(boolQueryBuilder)
                    .withFields("eventId", "eventType", "eventName");
SearchQuery searchQuery1 = searchQueryBuilder1.build();
Long clickTotalCount = elasticsearchRestTemplate.count(searchQuery1);

执行错误:Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: org.elasticsearch.search.SearchHits.getTotalHits()J

包引用版本:

Spring Data Elasticsearch 3.2.9.RELEASE
Elasticsearch 7.5.2

Spring Data Elasticsearch 3.x 只兼容到Elasticsearch 6.x,Elasticsearch 7.x需要使用Spring Data Elasticsearch 4.x

更换Spring Data版本代价太大,于是直接使用Elasticsearch Java Client查询。

CountRequest countRequest = new CountRequest(BURY_POINT_DURATION);
countRequest.query(boolQueryBuilder);
CountResponse countResponse = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
Long clickTotalCount = countResponse.getCount();

发表评论

电子邮件地址不会被公开。