Let’s say we have a textfield with tags inside. For example we have one row with the value of “menu, navbar, commerce”.
Now when the user searches for menu or navbar or commerce or all of them or just two of them. How can we use query setFilter on dataset to accomplish this?
The function hasSome seem like a good choice but it seem that the collection field tag can only contain one word not three comma separated words.
The function hasAll seem like it can search for all cvalues in a field but it still seem to only work when you have one word in the field.
So please, how can the user write “menu navbar” in a textbox and I filter in fields which contains…
Row 1: “menu”
Row 2: “navbar”
Row 3 “menu, navbar”
Row 4: “navbar, menu”
Row 5: “commerce”
All first four records needs to be found and not the fifth, ideas?
my search uses .contains instead of .hasAll or .hasSome. If I understand you correctly, I think my code does the same thing you’re asking.
Uses ‘contains’ condition for any word in the input.
This will make sure we only find items that have word1 AND word2 AND … AND word(n), regardless of the order they appear in.
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('#searchInput').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('Videos')
.descending("created");
//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('tags', 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('#resultsTable').rows = res.items;
})
.catch(err =>
{
console.log("problem in search! " + err);
});
}
Code originally by Ziv on this thread.