#community-help

Sorting Search Results Based on Weights

TLDR Pantelis wanted to sort search results by weight. Kishore Nallan suggested using "prioritize_exact_match: false" and "text_match_type: 'max_weight'" to achieve the desired result.

Powered by Struct AI

1

14
3mo
Solved
Join the chat
Jun 26, 2023 (3 months ago)
Pantelis
Photo of md5-cf30cea0b09568543ab6a67633468f77
Pantelis
12:33 PM
Hello, i have successfully created my schema and started to index some documents, so far so good. However i would like to sort my search results based on weights, so i came across query by weights. Say that i have those two fields: title, city. I want to give a greater weight to the tile than city so that i give priority to the titles. I am using query_by_weights=2,1. I would expect that results with the keyword in title would come first but then i saw that results are sorted by default with text_match. What is the purpose of the weights if they dont override sorting? How can i achieve what i want?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:35 PM
👋 can you post an example query, an example record that you were expecting first, and the record that actually shows up?
Pantelis
Photo of md5-cf30cea0b09568543ab6a67633468f77
Pantelis
12:45 PM
sure i have the following structure:
Documents: {title: 'Eat Croissant in Nice', food: ['Socca', 'Ratatouille'}
{title: 'Stroll through Montmartre's cobblestone streets', food: ['Baguette', Croissant]

query_by=title,food
query_by_weights=2,1

the second one comes first when i search for 'Croissant'
12:51
Pantelis
12:51 PM
where i want the fields with the most weights to come up first, in my case Eat Croissant in Nice
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:58 PM
Can you try setting:

prioritize_exact_match: false

The exact match with the word croissant on the food field is giving it a higher text match score. Weight is used to influence the ranking when match score is the same.
Pantelis
Photo of md5-cf30cea0b09568543ab6a67633468f77
Pantelis
12:59 PM
is there a way to sort the results based on the weight of the fields?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:59 PM
There is also the text_match_type parameter which can help with the behavior you want.

Here's what the docs say:

In a multi-field matching context, this parameter determines how the representative text match score of a record is calculated. 

Possible values: `max_score` (default) or `max_weight`. 

In the default `max_score` mode, the best text match score across all fields are used as the representative score of this record. Field weights are used as tie breakers when 2 records share the same text match score.

In the `max_weight` mode, the text match score of the highest weighted field is used as the representative text relevancy score of the record.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
01:00
Kishore Nallan
01:00 PM
So try sending:

text_match_type: 'max_weight'
Pantelis
Photo of md5-cf30cea0b09568543ab6a67633468f77
Pantelis
01:00 PM
nice let me check!
01:04
Pantelis
01:04 PM
Nope it still doesnt make any difference, the result with food still comes first instead of the result that has the keyword in title
01:05
Pantelis
01:05 PM
Image 1 for
01:06
Pantelis
01:06 PM
prioritize_exact_match worked though
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
01:07 PM
It's max_weight
Pantelis
Photo of md5-cf30cea0b09568543ab6a67633468f77
Pantelis
01:07 PM
you are right! thank you so much, worked as well

1