Wix site search

Hello, I recently started adding code to my site and don’t have much experience yet. I created a searchbar for my product database so people can find what they want. The problem is, that when someone only types one of the keywords, the accessory product shows up. However, when that exact keyword is written in a sentence, no product will appear. Does anyone here have a solution for this problem please?
This is my code for this portion:
searchBar = My searchbar
dataset1 = The dataset that I use
Zoekwoorden1 = The database I use, where my keywords are stored
keywordsProbleem = The words where the products are linked with

import {local} from ‘wix-storage’ ;
import wixData from ‘wix-data’ ;

$w.onReady( function () {
var sameWord = local.getItem( “searchWord” );

$w( "#searchBar" ).value = sameWord; 
$w( "#searchBar" ).placeholder = sameWord; 
$w( '#dataset1' ).onReady( **function**  () { 
    search(); 
}); 

});

export function searchButton_click() {
search();
}

function search() {
wixData.query( ‘Zoekwoorden1’ )
.contains( ‘keywordsProbleem’ , $w( “#searchBar” ).value)
.find()
.then(res => {
$w( ‘#repeater1’ ).data = res.items;
});

}

Hello.

You can use split() Javascript method to divide the search bar input into separate words and query your collection for each of the words. See sample code below:

export function searchButton_click(event) {
 let dataQuery = wixData.query("Zoekwoorden1")
    $w("#searchBar").value.split(" ").forEach(part =>
        dataQuery = dataQuery.contains("keywordsProbleem", part)
    )
    dataQuery.find().then(res => {
        $w('#repeater1').data = res.items;
    })
}

Good luck!

Thank you so much!

Oh and where should I put this code? (sorry for the newbie questions):grinning:

@olivierwerkman It should replace your search button’s onClick event handler . I just modified your search button code.

@samuele Thank you so much!

@samuele Unfortunately it still doesn’t work, but I appreciate your help!