Spring Data ElasticSearch Is it possible to get the "took" field from the response

269 Views Asked by At

When I query from elasticsearch, I get a response:

{
"took": 16,
"timed_out": false,
"_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
},

Is it possible to get the "took" value using spring data elasticsearch?

EDIT:

This is how we query to the elasticsearch:

SearchHits<SearchPo> findByAllAttributes(String searchText) {

return elasticsearchRestTemplate.search(buildHeaderSearchQuery(searchText),
    SearchPo.class, IndexCoordinates.of("search");
1

There are 1 best solutions below

4
Val On

You cannot retrieve that information using ElasticsearchRestTemplate. As you can see from the code, the tookinformation is available inside SearchResponse but SearchDocumentResponseBuilder.from(SearchResponse) doesn't make it available and only returns SearchHits.

What you could do is sub-class ElasticsearchRestTemplate and overload (not override!) the search method in order to return the raw SearchResponse so you can then call SearchResponse.getTook() from your code.

Edit (by P.J.Meisch): Create an issue in Github to have this added to the SearchHits<T> and feel free to contribute a pull request for that.