Database search for multiple words anywhere in the referenced propertyName

#database #filter #search

Is it possible to use a filter function to check a database ‘title’ for multiple words from user input (search box)? For example, if the user search input is “grace and peace” I’m trying to return results from a database that would include the propertyName “grace leads to peace”

The basic search code is below. I want to use the “contain” function because unlike “hasSome”, it is not case sensitive…

This is taken directly from the " Search a Database " tutorial. If you go to the example site , the search will work if you type in “perfect”, but it will not work if you type in “perfect wedding”

import wixData from "wix-data";

$w.onReady(() => {

});

let lastFilterTitle;
let debounceTimer;
export function iTitle_keyPress(event, $w) {
  if (debounceTimer) {
    clearTimeout(debounceTimer);
    debounceTimer = undefined;
  }
  debounceTimer = setTimeout(() => {
    filter($w('#iTitle').value);  
  }, 500);
}

function filter(title, continent) {
  if (lastFilterTitle !== title) {
    let newFilter = wixData.filter();
    if (title)
      newFilter = newFilter.contains('articleTitle', title);
    if (continent)
      newFilter = newFilter.contains('continent', continent);
    $w('#dataset1').setFilter(newFilter);   
    lastFilterTitle = title; 
  }
}

If I understand your question, you wish to retrieve all the items with a title that contains at least one of the words (in your example: “grace” or “and” or “peace”). Is that what you want?

Thanks for the quick response J.D! Yes, essentially. Or, I guess more ideally, it would return results that had only those words.

So in my example, if someone searches “grace peace” it would retrieve all of the items with those two words. But if they search “grace and peace” it would not, because “and” is not in the title of the item.

Does that make sense? I’m having a hard time with the language to describe my challenge.

Here’s a couple screenshots to help explain from the example website.

I’m trying to adjust so that “Australia” will still show in the repeater if “City Treasure” is the input.

@communications19033 so it needs to contain all the words but not necessarily in the same order. right?
So you can try:

///...in the filter function   
const titleWords = title.split(' ').map(e => e.trim()).filter(e => e);
let newFilter = wixData.filter();
titleWords.forEach(word => {
newFilter = newFilter.contains('articleTitle', word);
})
//....
 $w('#dataset1').setFilter(newFilter); 

You can do the same for ‘continent’.

@jonatandor35 Thanks!! That worked. Here’s the code without ‘continent’ search.

import wixData from "wix-data";

$w.onReady(() => {

});

let debounceTimer;
export function iTitle_keyPress(event, $w) {
  if (debounceTimer) {
    clearTimeout(debounceTimer);
    debounceTimer = undefined;
  }
  debounceTimer = setTimeout(() => {
    filter($w('#iTitle').value);  
  }, 500);
}

function filter(title) {
    const titleWords = title.split(' ').map(e => e.trim()).filter(e => e);
    let newFilter = wixData.filter();
    titleWords.forEach(word => {
    newFilter = newFilter.contains('articleTitle', word);
    })
    $w('#dataset1').setFilter(newFilter);   
  }