在 “spring-data-elasticsearch” 4.0.4.RELEASE 中,如何将 SearchHits 转换为 Page?

huangapple go评论63阅读模式
英文:

In "spring-data-elasticsearch" 4.0.4.RELEASE, how to convert SearchHits to Page?

问题

spring-data-elasticsearch 4.0.4.RELEASE 中,ElasticsearchRestTemplatequeryForPage() 方法已被弃用,需要使用 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 content = (List) SearchHitSupport.unwrapSearchHits(searchHits);
return new PageImpl<>(content, query.getPageable(), searchHits.getTotalHits());


<details>
<summary>英文:</summary>

From:&lt;br/&gt;
`SearchHits&lt;Parcel&gt; 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>



huangapple
  • 本文由 发表于 2020年10月14日 11:59:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64346398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定