# Adding score to SearchHit via REST Template

**URL:** <https://discuss.elastic.co/t/adding-score-to-searchhit-via-rest-template/323154>\
**Category:** Elasticsearch\
**Created:** [January 13, 2023, 6:05pm UTC](https://discuss.elastic.co/t/adding-score-to-searchhit-via-rest-template/323154 "2023-01-13T18:05:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![timothystone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timothystone/32/112287_2.png) [@timothystone](https://discuss.elastic.co/u/timothystone)\
**Post date:** [January 13, 2023, 6:05pm UTC](https://discuss.elastic.co/t/adding-score-to-searchhit-via-rest-template/323154/1 "2023-01-13T18:05:53Z")

</div>

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()`,

```java
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()`:

```bash
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!

---

<div class="post-metadata">

**Author:** ![timothystone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timothystone/32/112287_2.png) [@timothystone](https://discuss.elastic.co/u/timothystone)\
**Post date:** [January 14, 2023, 6:43pm UTC](https://discuss.elastic.co/t/adding-score-to-searchhit-via-rest-template/323154/2 "2023-01-14T18:43:09Z")

</div>

I think this is related to calling `searchHits.map(SearchHit::getContent).stream` 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.

---

<div class="post-metadata">

**Author:** ![timothystone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timothystone/32/112287_2.png) [@timothystone](https://discuss.elastic.co/u/timothystone)\
**Post date:** [January 15, 2023, 1:16am UTC](https://discuss.elastic.co/t/adding-score-to-searchhit-via-rest-template/323154/3 "2023-01-15T01:16:40Z")

</div>

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

```java
 List<Type> hits = searchHits.map(typeSearchHit -> {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...

```shell
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": {
    "root_cause":[
      {
        "type":"query_shard_exception",
        "reason":"No mapping found for [score] in order to sort on",
        "index_uuid":"a3PUxitKTBy72gDFLBKkzA",
        "index":"type"
      }
    ],
    "type":"search_phase_execution_exception",
    "reason":"all shards failed",
     "phase":"query",
     "grouped":true,
     "failed_shards":[
       {
         "shard":0,
         "index":"type",
         "node":"-ps3vmwyRfWdy_Ry3_7AGw",
         "reason":{
           "type":"query_shard_exception",
           "reason":"No mapping found for [score] in order to sort on",
           "index_uuid":"a3PUxitKTBy72gDFLBKkzA",
           "index":"type"
         }
       }
     ]
   },
   "status":400
}

```

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

**Note** `type` or `Type` is just my obfuscation.

---

<div class="post-metadata">

**Author:** ![timothystone](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timothystone/32/112287_2.png) [@timothystone](https://discuss.elastic.co/u/timothystone)\
**Post date:** [January 15, 2023, 3:07am UTC](https://discuss.elastic.co/t/adding-score-to-searchhit-via-rest-template/323154/4 "2023-01-15T03:07:04Z")

</div>

> [@timothystone](#):
>
> `No mapping found for [score] in order to sort on`

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 12, 2023, 3:07am UTC](https://discuss.elastic.co/t/adding-score-to-searchhit-via-rest-template/323154/5 "2023-02-12T03:07:51Z")

</div>

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