从 Spring Data Elastic Search 4.0.0 的 SearchHits 类获取 InnerHits 结果 [英] Getting InnerHits Result from SearchHits Class at Spring Data Elastic Search 4.0.0

查看:27
本文介绍了从 Spring Data Elastic Search 4.0.0 的 SearchHits 类获取 InnerHits 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在做从 spring 数据弹性搜索 3.2.X 到 4.0.0 的迁移.

我一直依赖于通过使用现已弃用的低级函数通过原始 SearchResponse 类获取内部点击结果 org.springframework.data.elasticsearch.core.ElasticsearchOperations.query(SearchQuery query, ResultsExtractor resultsExtractor);

https://docs.spring.io/spring-data/elasticsearch/docs/4.0.0.RELEASE/reference/html/#elasticsearch-migration-guide-3.2-4.0

基本上我所做的是创建自定义的 resultExtractor,它将公开低级别的 SearchResponse,以便我可以提取内部点击结果.

现在 4.0.0 版本不再存在该功能,ElasticSearchOperations 和 ElasticSearchRestTemplate 都使用名为 SearchHits 的新结果类,AFAIK 不存储内部命中信息.

除了直接使用 restHighLevelClient 之外我能想到的解决方法是扩展 ElasticSearchRestTemplate 并基本上创建 search(Query query, Classclazz、IndexCoordinates 索引),它将返回 SearchHits 和原始搜索响应(类似于我的自定义 ResultExtractor 过去所做的).

之后,由于所有 api 返回页面实例都已弃用,我现在必须依靠 SearchHitSupport.searchPageFor(SearchHits searchHits, @Nullable Pageable pageable) 来获得常用的分页功能.>

我觉得这不是一个非常简单和干净的解决方案,当然感觉更像是一种解决方法.4.0.0版本有没有更直接的方式获取innerHitsResult并将结果内容映射到页面?

解决方案

有一个 未解决的问题 关于inner_hits.返回 inner_hits 应该在 SearchHit 类中完成,而不是通过暴露内部 Elasticsearch 数据来完成.

至于分页:当您有一个 SearchHits 对象作为使用 Pageable 的查询结果时,您可以调用

SearchHitssearchHits = operations.search(query, Entity.class);SearchPage<实体>page = SearchHitSupport.searchPageFor(searchHits, query.getPageable)

SearchPage 实现了 Page 接口.

问题已修复,从下一版本(4.1)开始返回内部命中.

I am currently doing migration from spring data elastic search 3.2.X to 4.0.0.

I had been relying on obtaining inner hits result through raw SearchResponse class by utilizing the now deprecated low level function org.springframework.data.elasticsearch.core.ElasticsearchOperations.query(SearchQuery query, ResultsExtractor resultsExtractor);

https://docs.spring.io/spring-data/elasticsearch/docs/4.0.0.RELEASE/reference/html/#elasticsearch-migration-guide-3.2-4.0

Basically what I did was creating custom resultExtractor which will expose the low level SearchResponse so that I could then extract the inner hits result.

Now with version 4.0.0 that functionality is no longer there, Both ElasticSearchOperations and ElasticSearchRestTemplate utilize new result class called SearchHits which AFAIK does not store inner hits information.

The workaround I could think of besides using the restHighLevelClient directly (which of course is undesirable since I'd lose the object mapping and stuff) is to extend ElasticSearchRestTemplate and basically creating new implementation of search(Query query, Class clazz, IndexCoordinates index) which will return SearchHits and the raw search response (similar to what my custom ResultExtractor used to do).

Afterwards, since all api returning page instance has been deprecated, I would now have to rely on SearchHitSupport.searchPageFor(SearchHits searchHits, @Nullable Pageable pageable) to get the usual paging functionalities.

I felt that this is not very straightforward and clean solution and certainly felt more like a workaround. Would there be any more straightforward way to obtain innerHitsResult and map result content to page at version 4.0.0?

解决方案

There is an open issue about inner_hits. Returning the inner_hits should be done in the SearchHit<T> class and not by exposing internal Elasticsearch data.

As for the paging: When you have a SearchHits<T> object as the result of a query that use a Pageable, you can call

SearchHits<Entity> searchHits = operations.search(query, Entity.class);
SearchPage<Entity> page = SearchHitSupport.searchPageFor(searchHits, query.getPageable)

SearchPage implements the Page interface.

Edit:

The issue is fixed, inner hits are returned from the next version (4.1) on.

这篇关于从 Spring Data Elastic Search 4.0.0 的 SearchHits 类获取 InnerHits 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆