Rank your Query Results in Wix Code now possible :)

It has of course been possible all the time but I managed to make a function that functions really good to rank results when querying your Data Collections in Wix Code.

Sometimes I just want to show results and use sorting but a lot of times there is some kind of keyword field or something I would like to sort after and that wouldn’t work to use sorting on a field for ranking.

Let’s say we create a new field in our Data Collection and call it Ranking. In that field we put a lot of comma separated values like golf,tennis,football and in the the record we put golf and in the third we put tennis, football.

So when we search for golf those who has that keyword should be ranked by the number of times that keyword is found inside that field. You could also redo my function and make the split use a space and then tell it to use the title field. Then it will rank all your records after the number of times your rank word occurs in the title field.

Well, play around with and I hope you have some fun with it.

// Yet another useful thing from Wix Show (www.wixshow.com)
// If you want more, support us by becoming a paying member
import wixData from 'wix-data';

export function searchWixData(dataCollection,rankField,rankWord,searchField, searchValue){
 // Backend search module made by Andreas Kviby
 // This function will search within a Data Collection for
 // the specified searchField and use contains for searchValue 
 // and then it will sort and rank the results for you after
 // rankWord within rankField
 
 // If you specify rankWord and rankField this will be used for
 // ranking the results. 
 //
 // REMEMBER
 //
 // You must have a comma separated field where you store the values
 // to rank after, like "fotball,basket,golf" and no spaces!!!
 //
 // Hope you like this function
 let dbQuery = wixData.query(dataCollection);
 dbQuery = dbQuery.contains(searchField, searchValue);
 return dbQuery 
    .find()
    .then( (results) => {
 if (results.totalCount > 0) { // We got hits
 console.log(results.totalCount + " hits returned!"); // Console output how many hits we get
 
 // Rank the results now
 if (rankWord & rankField) { // The rankWord & rankField must exist
 let rankedResult = results.items;
 var keywords = new Set(Object.keys(rankedResult).map(function (key) {
 var item = rankedResult[key];
 item.newKeywords = item[rankField].split(','); // All keywords that you want to rank efter must be comma separated
 return item.newKeywords;
                }));
 
 let startRankValue = 0;

 Object.keys(rankedResult).forEach(function (key) {
 var record = rankedResult[key];
 if (!record.rank) {record.rank = startRankValue; }
 record.newKeywords.forEach((keyword) => {
 var ranktest = keyword.indexOf(rankWord);
 if (ranktest > 0) {
 record.rank = record.rank + 1;
                        }
 
                    })
                });
 
 rankedResult.sort(function(a, b){
 var keyA = a.rank,
 keyB = b.rank;
 // Compare the 2 rank values
 if(keyA < keyB) return 1;
 if(keyA > keyB) return -1;
 return 0;
                });
 return rankedResult;
            }
 return results.items;             
        } 
 return [];
 
      });

 }

Happy coding in Wix Code!

#datacollection #search #wixshow #ranking #data #sort

3 Likes

cool