英文:
In "spring-data-elasticsearch" 4.0.4.RELEASE, how to convert SearchHits to Page?
问题
在 spring-data-elasticsearch
4.0.4.RELEASE 中,ElasticsearchRestTemplate
的 queryForPage()
方法已被弃用,需要使用 search()
方法代替,该方法返回 SearchHits
而不是 Page
。
那么,如何将 SearchHits
转换为 Page
,或者如何以 Page
的方式获取结果?
我已通过谷歌搜索,并查阅了官方文档,还检查了源代码,仍然不知道最佳方法,需要任何帮助?
英文:
In spring-data-elasticsearch
4.0.4.RELEASE, ElasticsearchRestTemplate
's queryForPage()
methods are deprecated, need to use search()
instead, which return SearchHits
instead of Page
.
So, how to convert SearchHits
to Page
, or how to still get results as Page
?
I have searched via google, and checked official doc, and checked source code, still don't know the best way, any help?
答案1
得分: 4
search()
方法中要传入的 Query
参数可以设置一个 Pageable
用于请求。要将返回的 SearchHits<T>
对象包装在 SearchPage<T>
中,只需执行以下操作:
SearchPage<T> searchPage = SearchHitSupport.searchPageFor(searchHits, query.getPageable());
英文:
The Query
parameter to be passed into the search()
method can have a Pageable
set for the request. To wrap the returned SearchHits<T>
object in a SearchPage<T>
you just can do
SearchPage<T> searchPage = SearchHitSupport.searchPageFor(searchHits, query.getPageable());
答案2
得分: 0
从:
`SearchHits<Parcel> searchHits = operations.search(query, xxx.class);`
然后:
List
return new PageImpl<>(content, query.getPageable(), searchHits.getTotalHits());
<details>
<summary>英文:</summary>
From:<br/>
`SearchHits<Parcel> searchHits = operations.search(query, xxx.class);`
You then:
List<xxx> content = (List<xxx>) SearchHitSupport.unwrapSearchHits(searchHits);
return new PageImpl<>(content, query.getPageable(), searchHits.getTotalHits());
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论