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

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. :slight_smile:

Appreciate any help
Kind Regards
Jens

Elastic version:

      "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"
      }

I'm not sure about this but may be you'd like to explore this: Filter search results | Elasticsearch Guide [7.12] | Elastic

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?

You should upgrade to 7.12.0.

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 :laughing:

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 where the upgrade is like a one click operation... :wink: