# Using sort with elastic request is not returning score in the response

**URL:** <https://discuss.elastic.co/t/using-sort-with-elastic-request-is-not-returning-score-in-the-response/227890>\
**Category:** Elasticsearch\
**Created:** [April 14, 2020, 10:39am UTC](https://discuss.elastic.co/t/using-sort-with-elastic-request-is-not-returning-score-in-the-response/227890 "2020-04-14T10:39:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![guruprasadkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guruprasadkk/32/66271_2.png) [@guruprasadkk](https://discuss.elastic.co/u/guruprasadkk)\
**Post date:** [April 14, 2020, 10:39am UTC](https://discuss.elastic.co/t/using-sort-with-elastic-request-is-not-returning-score-in-the-response/227890/1 "2020-04-14T10:39:10Z")

</div>

I am using elasticsearch-rest-high-level-client 7.6.2 version to interact with elastic server from my java application.  
Now I am planning to use sorting capability in elastic. When I added the sort in the elastic request. I am not getting the scores back in response.  
I understood that it is expected behaviour and suggestion is to use

`SearchRequestBuilder#setTrackScores(true)`

But I am using RestHighLevelClient. with this how can I create the instance of SearchRequestBuilder?  
Is there any other alternate solution for this problem?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 14, 2020, 1:12pm UTC](https://discuss.elastic.co/t/using-sort-with-elastic-request-is-not-returning-score-in-the-response/227890/2 "2020-04-14T13:12:22Z")

</div>

Wondering if this works?

```
SearchRequest sr = new SearchRequest();
sr.setTrackScores(true);

```

But I wonder if this is supposed to work (on a mobile now so I can't test it):

```
new SearchSourceBuilder().setTrackScores(true);
```

---

<div class="post-metadata">

**Author:** ![guruprasadkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guruprasadkk/32/66271_2.png) [@guruprasadkk](https://discuss.elastic.co/u/guruprasadkk)\
**Post date:** [April 15, 2020, 5:47am UTC](https://discuss.elastic.co/t/using-sort-with-elastic-request-is-not-returning-score-in-the-response/227890/3 "2020-04-15T05:47:35Z")

</div>

None of these works because method setTrackScores is part of the class SearchRequestBuilder

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 15, 2020, 8:36am UTC](https://discuss.elastic.co/t/using-sort-with-elastic-request-is-not-returning-score-in-the-response/227890/4 "2020-04-15T08:36:46Z")

</div>

Try something like this:

```auto
SearchResponse response = client.search(new SearchRequest("test").source(
        new SearchSourceBuilder().query(
                QueryBuilders.matchQuery("foo", "bar")
        ).trackScores(true)
), RequestOptions.DEFAULT);

```

---

<div class="post-metadata">

**Author:** ![guruprasadkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guruprasadkk/32/66271_2.png) [@guruprasadkk](https://discuss.elastic.co/u/guruprasadkk)\
**Post date:** [April 16, 2020, 10:17am UTC](https://discuss.elastic.co/t/using-sort-with-elastic-request-is-not-returning-score-in-the-response/227890/5 "2020-04-16T10:17:03Z")

</div>

Thanks. This worked.

---

<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:** [May 14, 2020, 10:17am UTC](https://discuss.elastic.co/t/using-sort-with-elastic-request-is-not-returning-score-in-the-response/227890/6 "2020-05-14T10:17:06Z")

</div>

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