Elasticsearch, sorting by exact string match
One solution is to manipulate the score of the results that contain the Bob in the first name field.
For example:
POST /test/users{ "name": "Bob"}POST /test/users{ "name": "Alice"}GET /test/users/_search{ "query": { "bool": { "should": [ { "match": { "name": { "query": "Bob", "boost" : 2 } } }, { "match_all": {} } ] } }}
Would return both Bob and Alice in that order (with approximate scores of 1 and 0.2 respectively).
From the book:
Query-time boosting is the main tool that you can use to tune relevance. Any type of query accepts a boost parameter. Setting a boost of 2 doesn’t simply double the final _score; the actual boost value that is applied goes through normalization and some internal optimization. However, it does imply that a clause with a boost of 2 is twice as important as a clause with a boost of 1.
Meaning that if you also wanted "Fred" to come ahead of Bob you could just boost it with a 3 factor in the example above.