Getting InnerHits Result from SearchHits Class at Spring Data Elastic Search 4.0.0

Viewed 4663

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?

1 Answers

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.

Related