I have a search field where user can enter multiple separate words. I am trying to filter the dynamic page using this search entry. Example: user enters "pizza tomatoes", dataset contains the following entry "yammy pizza with cheese and tomatoes". So I want this entry to be shown in filtered results.
For this I have the following code:
import wixData from "wix-data";
export function button2_click(event) {
let searchPhrase = $w("#iSearchPhrase").value;
let searchWords = searchPhrase.split(' ');
let newFilter = wixData.filter();
if (searchPhrase) {
newFilter = newFilter.hasSome('title', searchWords)
.or(newFilter.hasSome('mealDescription', searchWords));
}
$w('#mealDataset').setFilter(newFilter);
}
But it doesn't return anything.
If I try the exact phrase search via contains(), it works but as I said it has to be an exact match (example: user enters "yammy pizza", dataset contains the following entry "yammy pizza with cheese and tomatoes" and this way contains() works). But exact search is not what I am looking for.
It can search many words. It is also not case sensitive.
it does not consider word order. can search for mixed words. If the words are written in the text, lists them. Maybe this is what you are looking for. I'm trying to add a filter to this code.
import wixData from 'wix-data'; export function searchButton_onClick(event) { //assume the input comes from a component called 'searchInput' //CHANGE TO YOUR SPECIFIC COMPONENT NAME let searchValue = $w('#SearchBox').value; //split the search inputs into distinct words let searchWords = searchValue.split(' '); //build a query for 'my-collection' //CHANGE THIS TO YOUR COLLECTION NAME let query = wixData.query('Afghanistan') .descending("overview"); //add a "contains" condition to the query for each word: //assumes we search in the field 'myField' //CHANGE THIS TO YOUR FIELD NAME for (let i=0; i < searchWords.length; i++) { query = query.contains('overview', searchWords[i]) } //actually run the query: query.find().then(res => { //give the results to the table to display //assume the table is named 'resultsTable' //CHANGE TO YOUR SPECIFIC COMPONENT NAME $w('#repeater1').data = res.items; }) .catch(err => { console.log("problem in search! " + err); }); }
Hey Alex,
What you are looking for is called "fuzzy search", where non-exact search phrases are considered a match. We don't support fuzzy search for data filters, but we do support it in our Search API.
HTH,
Tova
You should keep in mind that .hasSome() is case-sensitive. So if you have "Pizza" in your collection, and the user inputs "pizza" it won't find it.
Also the .hasSome is not what you need here. You need .contains() :
searchWords.forEach(e => { const word = e.trim(); if(word.length > 0){ newFilter = newFilter.contains('title', word); } }} //the .trim() method removes unneeded spaces from start and end