How to make multiple queries for multiple words

Hi

I am making a search for a database that show the results in a repeater. It works but i have this problem: when a user types in a search that has multiple words into the input element ex: "clinical study "

I would like the search to display results for word1 OR word2 OR word 3 – for example, for the search “clinical study” I want results that contain the word “clinical” OR the word “study”. Right now it gives me only results that contain both words.

Here is my code:

import wixData from 'wix-data';
export function ititle_keyPress(event, $w) {
$w("#dataset1").setFilter(wixData.filter()
)

const filterValue = $w("#SearchBox").value
const byTitle = wixData.filter().contains("title", filterValue)
const byTag1 = wixData.filter().contains("tag1", filterValue)
const byTag2 = wixData.filter().contains("tag2", filterValue)
$w("#dataset1").setFilter(byTitle.or(byTag1.or(byTag2)))
}

Thanks!

You will need to parse your search string so that you will have an array of words. You can use:

let words = str.split(" ");

You can then use the array of words in your query.

I would also recommend using one tags field with multiple values instead of having multiple tag fields (tag1, tag2).

Hi @yisrael-wix thanks so much for responding
i tried implemented your code and it works!
Thanks so much
But now i have this new problem:
I would like to Search for articles with both clinical and study in the same row.

This is my new code

import wixData from 'wix-data';
export function ititle_keyPress(event, $w) {
$w("#dataset1").setFilter(wixData.filter()
)
let str = 'Searchbox';
let words = str.split(" ");
console.log('SearchBox');
const filterValue = ("words").value
const byTitle = wixData.filter().contains("title", filterValue)
const byTag1 = wixData.filter().contains("tag1", filterValue)
const byTag2 = wixData.filter().contains("tag2", filterValue)
$w("#dataset1").setFilter(byTitle.or(byTag1.or(byTag2)))
}

Thanks so much!

@harborlightco Your code has many errors and I’m not sure how it worked at all…

For example, in this short segment of code:

let str = 'Searchbox';
let words = str.split(" ");

The variable str is now equal to the string “Searchbox”, and words ends up with the word “Searchbox” as it’s only item in the array of results. If you want the value of the Searchbox element, you should write:

let filterValue = $w("#SearchBox").value;

This line of code only prints the string “SearchBox” to the console.
console.log(‘SearchBox’);

This line of code doesn’t really do anything.

const filterValue = ("words").value

The result is that filterValue is undefined.

To get the array of words from your Searchbox, do this:

let str = $w("#Searchbox");
let words = str.split(" ");

Each word in the array would be accessed by its index number. So…

let first = words[0];    // get the first word
let second = words[1];   // get the second word

You will need to familiarize yourself with basic coding concepts to accomplish what you want. There are a wealth of Javascript coding sites which will help you learn Javascript from basic to advanced - Javascript.info is a good one. The Corvid Resources page provides tutorials, examples, and articles on getting the most out of Corvid.

You may also want to check out the WixArena - it’s a hub where you can look for Corvid (and other) experts for hire.