Search code examples
elasticsearchsearchkick

Searchkick: I would like the results to display the exact match first before listing out the others with sorting?


I would like the results to display the exact match first before listing out the others and that data should be sorted by a-z or z-a (asc/desc)?

1. Maxwell B
2. Maxwell A
3. D Maxwell
4. C Maxwell

Search text: Maxwell if I search the text and apply order by asc then result is: C Maxwell D Maxwell Maxwell A Maxwell B

But I have searched the Maxwell so first should display Maxwell (matched result) then it should list out the others, What I want is: Maxwell A Maxwell B C Maxwell D Maxwell


Solution

  • In the example you gave, while searching for 'Maxwell' both 'C Maxwell' and 'Maxwell A' are of equal similarity - none of them is a full exact match, but both contains the searched token (Maxwell) fully.

    I also don't think sorting can help, as sorting is done on the text, and still there is no preference for any one of them more then the other, cause both contain 'Maxwell' and other text parts.

    What query are you running exactly? One way you can change the order of the result is to modify the score of the results. you can look for an example in https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html where you can specify a function_score with a match for 'Maxwell' , which might help.