# How to sort primarily on one score, and secondarily on another score

**URL:** <https://discuss.elastic.co/t/how-to-sort-primarily-on-one-score-and-secondarily-on-another-score/269564>\
**Category:** Elasticsearch\
**Created:** [April 8, 2021, 8:09am UTC](https://discuss.elastic.co/t/how-to-sort-primarily-on-one-score-and-secondarily-on-another-score/269564 "2021-04-08T08:09:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JF2018](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@JF2018](https://discuss.elastic.co/u/JF2018)\
**Post date:** [April 8, 2021, 8:09am UTC](https://discuss.elastic.co/t/how-to-sort-primarily-on-one-score-and-secondarily-on-another-score/269564/1 "2021-04-08T08:09:11Z")

</div>

Hi

I have n terms ie:  
"Cheese","Robot" and "Cake"

I wish to do a primary sorting strictly on number of these terms matched, and secondarily on the total score of the three terms.

I know a way to sort by number of hits, ie. to use constant score, and score each termQuery as 1.  
I also know that I can sort by scoring, ie. by having a bool query, where I add all three term queries to the should clause.

But when it comes to combining these, I get in trouble. I Hope someone in the community can help me on this problem, and give me some hint to how I can primary sort on one score, and secondarily sort on another score. 🙂

Appreciate any help  
Kind Regards  
Jens

Elastic version:

```auto
      "version" : {
        "number" : "7.5.2",
        "build_flavor" : "default",
        "build_type" : "deb",
        "build_hash" : "8bec50e1e0ad29dad5653712cf3bb580cd1afcdf",
        "build_date" : "2020-01-15T12:11:52.313576Z",
        "build_snapshot" : false,
        "lucene_version" : "8.3.0",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
      }

```

---

<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 8, 2021, 9:38am UTC](https://discuss.elastic.co/t/how-to-sort-primarily-on-one-score-and-secondarily-on-another-score/269564/2 "2021-04-08T09:38:10Z")

</div>

I'm not sure about this but may be you'd like to explore this: [Filter search results | Elasticsearch Guide [7.12] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/filter-search-results.html#rescore)

> I wish to do a primary sorting strictly on number of these terms matched, and secondarily on the total score of the three terms.

That said, can't you do that with a bool query with multiple should clauses, one for each term which you boost by 10 for example and the last one for the whole text which you boost by 1 (default) or by 0.1?

> [@JF2018](#):
>
> ```auto
> "number" : "7.5.2",
> 
> ```

You should upgrade to 7.12.0.

---

<div class="post-metadata">

**Author:** ![JF2018](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@JF2018](https://discuss.elastic.co/u/JF2018)\
**Post date:** [April 8, 2021, 10:01am UTC](https://discuss.elastic.co/t/how-to-sort-primarily-on-one-score-and-secondarily-on-another-score/269564/3 "2021-04-08T10:01:57Z")

</div>

Rescoring is interesting, it might be something like that I should use. Only problem with that solution is, that I potentially have a lot of docs matching equally well on the first scoring, which means my window has to be quite high.

The boosting is also a good idea, simply boosting the clauses with constant score so much, that it will always dominate the other part. That might be a feasible solution as well.

Thank you so much for your help/ideas!

P.S.  
With regard to the 7.12.0, we have just gone from ES.1.7 to 7.5.2 a couple of months ago, so hoped that would stop people begging me to update - but I see that will never go away 😆

---

<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 8, 2021, 10:19am UTC](https://discuss.elastic.co/t/how-to-sort-primarily-on-one-score-and-secondarily-on-another-score/269564/4 "2021-04-08T10:19:11Z")

</div>

> [@JF2018](#):
>
> but I see that will never go away 😆

Haha! Sure.

Actually it has been probably a nightmare to go from 1.7 to 7.x. The cool news is that moving from 7.5 to 7.12 should be super easy. Specifically if you are running on [cloud.elastic.co](http://cloud.elastic.co) where the upgrade is like a one click operation... 😉

---

<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 6, 2021, 10:19am UTC](https://discuss.elastic.co/t/how-to-sort-primarily-on-one-score-and-secondarily-on-another-score/269564/5 "2021-05-06T10:19:40Z")

</div>

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