Sort by score? $search results are poor

My search results are pretty crappy.

For instance, there are exact matches for the text I’m searching, but the exact match document is near the end of the results.

Is this an issue with my search index or is there a way to sort $search results that I’m unaware of?

1 Like

I think that you can simply add a decreasing $sort stage on the field searchScore.

Your exact matches probably have an higher searchScore and would end up the first documents. The order is probably the natural order of the collection since it is the most efficient.

Strangely, my results all have the same exact score, even the exact match…

I must be missing something

below are the results for searching “sazcvxbh3db”

sazcvxbh1db 1.51680588722229
sazcvxbh2db 1.51680588722229
sazcvxbh3db 1.51680588722229 <= exact match
sazcvxbh4db 1.51680588722229
sazcvxbhddb 1.51680588722229
sazcvxbhgdb 1.51680588722229
sazcvxbhtdb 1.51680588722229
sazcvxbr1ci 1.51680588722229
sazcvxbr2ci 1.51680588722229
sazcvxbr3ci 1.51680588722229
sazcvxbr4ci 1.51680588722229
sazcvxbrdci 1.51680588722229
sazcvxbrgci 1.51680588722229
sazcvxbrta1 1.51680588722229
sazcvxbrtci 1.51680588722229
sazcvxbv1db 1.51680588722229

The search:

        $search: {
            index: "autocomplete",
            autocomplete: {
                query: args?.searchText,
                path: "name",
            },
        },

and the index

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": [
        {
          "foldDiacritics": false,
          "maxGrams": 7,
          "minGrams": 3,
          "tokenization": "edgeGram",
          "type": "autocomplete"
        }
      ]
    }
  }
}

Adding fuzzy params to my $search doesn’t change anything…

The autocomplete works but I don’t get why the exact match doesn’t have a higher score

My guess, and I could be totally wrong, is that you’re using autocomplete and your max grams is set to 7. The first 7 chars all match your search query. Hence them having the same score.

1 Like

Improving exact match for this query is something that we are working on at the moment.

However, this is correct before and after that change.:

My guess, and I could be totally wrong, is that you’re using autocomplete and your max grams is set to 7. The first 7 chars all match your search query.

This is an exact match of the first. You should probably wrap this is a compound query with the autocomplete operator as one should clause, and the text operator as another clause.

2 Likes

I’m seeing the same issues, my exact match is ranking #7 in this example
https://prnt.sc/1s1nv1c