Hi there everyone!
I have been trying to achieve a feature that would make search of a databse collection much more useful for site owners, that is, ranking or sorting query results by relevance/matching to the original input from the user in a search bar.
To further explain what I am trying to achieve, I propose the example:
Lets say that a user has searched for “red fast car” in my site.
Currently, if a user did this, all results that contain the word red, the word fast or the word car would pop up in no specific order , giving the user results, but not exactly what he/she is looking for. Let’s say that 13 of my database collection items contain the word “red” or the word “car” , two of them contain two words: “red” and “car” and one contains the three words: “red”, “fast” and “car.”
What I am trying to do is arrange these items so that the item that contains all three words (the most words matching the query, not necesarilly all), appears first, followed by those who contain 2 words, and lastly all other items that have 1 word macthing the query.
I would really appreciate any help I can get with this matter, I have been trying for some time, but no luck whatsoever. I really think this could be an upgrade to any wix site owner’s UX and provide a great solution for everyone.
Thanks in advance!
Sincerely,
NicolasGPO
Hey Nicolas,
Sounds like an interesting approach and a nice feature to offer your users. You can use the following API functions:
wix-data.WixDataQuery.hasAll() will provide the results for all of the terms that are in provided list. Just do successive queries first with all three in the list, then with only two, and finally with just one. The hasAll() function is an easy way to do the .or() function in a query.
wix-data.WixDataQuery.hasSome() is another option when you’re just interested in some of the entries being found.
I hope this helps.
Yisrael
Thank You, I will try your solution!
HI Yisrael,
I have been trying to achieve this for some time now, however I cannot figure out how to display the result of the .hasAll() query before the result of the .hasSome() query of the same database inside the same repeater , in order to “rank” results by relevance to the search term.
I am trying to perform two queries , one that returns the results that match the search term exactly with .hasAll() and then one that returns items that match partially using the .hasSome() query.
Do you have any suggestions?
Thanks in advance!
Sincerely,
Nicolas
Hey Nicolas,
First filter for the hasSome() results. Then you can loop through these results for “hasAll” something like this:
const search_tags = ['red', 'green', 'blue'];
let items = [
{ color: 'red green blue', rank: 100, },
{ color: 'red blue', rank: 100 },
{ color: 'red green', rank: 100 },
{ color: 'green blue', rank: 100 },
{ color: 'red green purple blue', rank: 100 },
{ color: 'red green yellow grey blue', rank: 100 },
{ color: 'red black green blue', rank: 100 },
];
items.forEach(function (item) {
let color = item.color;
let res = search_tags.every(i => color.includes(i));
if (res) {
item.rank = 0;
}
});
// sort the items
// set the repeater data
console.log(items);
The forEach loop goes through all of my items, and the search_tags.every loops through the search strings to see if the field contains all of the items. If so, I set the rank to 0 (which would be the highest rank).
After the “hasAll” ranking has been executed you should sort the results by rank and then set the repeater data.
You can adapt this to your results to give you to ranking for “hasAll” items. You can create a more comprehensive ranking, but it won’t necessarily be trivial.
Good luck,
Yisrael
Hi @yisrael-wix
Is it possible to give example on how to do queries with all 3 in the list, followed by 2, then 1? Based on the example NicolasGPO’s post? Thanks.
Hi @nicolasgpo ,
You manage to find the solution?