Adding score to SearchHit via REST Template

I'm using a NativeSearchQuery with setTrackScores(true). This query is passed to the ElasticsearchRestTemplate#search(Query, Clazz) (inherited from AbstractElasticsearchTemplate#(Query, Clazz) The Clazz here is a typical data object (bean). It includes a Transient annotation for score, as the persistence backend is not concerned with or stores this field..

The score field is also concerned with surfacing the score provided in the query to each SearchHit returned by the ElasticsearchRestTemplate#search(query, clazz) .

Looping through the hits, I see a score value, e.g., hit.getScore(),

SearchHits<Type> searchHits = restTemplate(query, Type.class);
for (SearchHit hit : searchHits) {
  log.debug("Score: {}", hit.getScore());

but, the score value is not mapped to the score field is null in hit.getContent():

SearchHit: Object{ properties... , score=null}

I have a sense this is something I'm overlooking, but need some pointers from some experienced users/devs.

How do I surface the score of the results in a List of SearchHits<Type> from a NativeSearchQuery?

Many thanks!

I think this is related to calling and collecting the List<Type>. Here getContent sees score as null because the value is Transient in the entity.

I'll update as I find out more.

OK... Closer. I have a score on the search result, but...

 List<Type> hits = -> {typeSearchHit.getContent().setScore(typeSearchHit.getScore()); return typeSearchHit.getContent();}).stream().collect(Collectors.toList());

Yes, there's probably a better way to express that, but I needed the brute force to get the score set on the Type under iteration. Ignoring that, I have new error...

Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://localhost:9200], URI [/type/_search?typed_keys=true&max_concurrent_shard_requests=5&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 400 Bad Request]
  "error": {
        "reason":"No mapping found for [score] in order to sort on",
    "reason":"all shards failed",
           "reason":"No mapping found for [score] in order to sort on",

I honestly don't know how to solve this. I'm resorting to Google-fu.

Note type or Type is just my obfuscation.

That was easy. I didn't have it mapped.

This has revealed I have a different problem that is not Elastic, but something in my understanding of the entity + elastic behaviors.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.