Hello,
I created a search bar that relies on a database.
Each word in this search bar is separated individually (Mot01, Mot02, Mot03).
When I run the search, I put this code (which works perfectly) :
wixData.query("ARTICLES")
.or(wixData.query("ARTICLES").contains("ContenuBrut", Mot01))
.or(wixData.query("ARTICLES").contains("ContenuBrut", Mot02))
.or(wixData.query("ARTICLES").contains("ContenuBrut", Mot03))
.find()
.then( (result) => {
But now I’m looking for a way to sort search results by the number of coincidences?
For example, if a result coincides with 2 words (“Mot01” and “Mot03”) :
This one :
.or(wixData.query("ARTICLES").contains("ContenuBrut", Mot01))
and this one :
.or(wixData.query("ARTICLES").contains("ContenuBrut", Mot03))
This result will appear before the one that has only one coincidence (just “Mot01”, for example)
Thanks a lot for your help ! 
This was an interesting functionality to implement! 
In order to accomplish it we can use a read-only Dataset connected to the Data Collection you want to find search results from (in this case “ARTICLES”).
By using a dataset we can take advantage of Wix Data Filters to create 3 cases to get all unique results (containing 1, 2, or 3 of the search terms).
Please note that you may need to do additional work to paginate the results if the dataset is large. Additionally, the search results must be distinct or you will get repeated results (this can be sanitized against).
Example Code
import wixData from 'wix-data';
$w.onReady(function () {
//Case 1 - Contains exactly three terms
const allTerms = wixData.filter().contains("ContenuBrut", Mot01).and(wixData.filter().contains("ContenuBrut", Mot03)).and(wixData.filter().contains("ContenuBrut", Mot02));
//Case 2 - Contains exactly two terms
const twoTerms = wixData.filter().contains("ContenuBrut", Mot01).and(wixData.filter().contains("ContenuBrut", Mot02)).or(wixData.filter().contains("ContenuBrut", Mot01).and(wixData.filter().contains("ContenuBrut", Mot03))).or(wixData.filter().contains("ContenuBrut", Mot03).and(wixData.filter().contains("ContenuBrut", Mot02))).and(wixData.filter().not(allTerms));
//Case 3 - Contains exactly one term
const oneTerm = wixData.filter().contains("ContenuBrut", Mot01).or(wixData.filter().contains("ContenuBrut", Mot03)).or(wixData.filter().contains("ContenuBrut", Mot02)).and(wixData.filter().not(allTerms.or(twoTerms)));
//Used to store sorted search results
let sortedResults = [];
$w("#dataset1").onReady(async () => {
try {
//Sets Each Filter then concatenates results to "sortedResults"
await $w("#dataset1").setFilter(allTerms);
sortedResults = [...sortedResults, ...await getSearchResults()];
await $w("#dataset1").setFilter(twoTerms);
sortedResults = [...sortedResults, ...await getSearchResults()];
await $w("#dataset1").setFilter(oneTerm);
sortedResults = [...sortedResults, ...await getSearchResults()];
//TODO: Handle displaying results (or doing whatever you need with the correctly sorted data)
console.log(sortedResults);
} catch (error) {
console.error("Error occured while filtering or fetching data: ", error)
}
});
});
//Returns items from dataset based on current filter
async function getSearchResults() {
const count = $w("#dataset1").getTotalCount()
return await $w("#dataset1").getItems(0, count).then((results) => results.items);
}
It is also possible to accomplish something similar to this without a dataset using WixData.query() with similar logic (using sets of objects to get all distinct search results in the order you prefer).
1 Like
Thank you very much Thomas for your return! I work with query, and I’m not an expert, so it’s hard to transpose your code without the dataset.
Could you tell me how we transpose it to the query?
Thank you so much again ! 