Searching using partial words in Database

I have a database. I have already added the search feature through code (I will paste the code towards the end for the perusal). Now currently if the search term only gives the results when the exact search word matches with the original database entry.

For instance, if the database entry is “A song of ice and fire” and if we search “ice fire” we will not get the result. However, if we search “ice and fire” we will get the result.

Therefore, I wanted to tweak my code in a way where it can search individual words and give results even if certain words are matching from the search input. Please assist me. I am not very comfortable with coding and cannot understand how to implement this feature in the code.

Here is the code which I already have:

import wixData from ‘wix-data’ ;

//wait until the page is loaded
$w.onReady( function () {

//when the search button is pressed on screen
$w( ‘#searchButton1’ ).onClick((event) => {

//wait until the dataset is fully loaded or else we may be searching a partially loaded dataset
$w( “#dataset1” ).onReady(() => {

//here we call the function called filter and execute it
filter();

//here we detail what the filter function does
function filter() {

let searchInput = ($w( ‘#input1’ ).value);

//we console log the search variable for debugging purposes
console.log($w( ‘#input1’ ).value);

                            $w( "#dataset1" ).setFilter 

                                (wixData.filter() 
                                .contains( "title" , ($w( '#input1' ).value)  

//change the collectionFieldID to the ID of the column in your collection the you want to filter
)
)

                        } 
                }) 
        }) 

})

It’s better to put onClick() inside the dataset.onReady() (and you did the opposite).

  • you since you only filter on click, there’s no need to have a .filter() function.
    Just put the dataset,setFilter inside the onClick event handler.

you can delete this line:

let searchInput = ($w('#input1').value);

because you don’t use it later.

Thanks a lot.

I have simplified my codes (after multiple attempts).

However, my problem is still not resolved. I am unable to do searching in the manner as I above described. For instance, if the text says, " M/s Gloob Interior Design Pvt. Ltd. v Mode Finserver Pvt. Ltd." I will only get this result if I type any word or full sentence like “gloob” or “design”, or “gloob interior”. But I won’t get this result, if I search for “gloob design”. How can I get that kind of searching?

Request someone to respond to this, please.

We can use stopwords to filter the phrase to keywords. The output will be on a array.
Then use Dataquery.hasany() to get the filtered items from dataset

stopword - npm (npmjs.com)

For that, we have to use the above package. This package is not available and we have to request Wix for package installation.

@krgupta101 try this:

import wixData from "wix-data"
//wait until the page is loaded
$w.onReady(function () {
    //wait until the dataset is fully loaded or else we may be searching a partially loaded dataset
    $w("#dataset1").onReady(() => {
        $w("#searchButton1").onClick(event => {
            filter($w("#input1").value)
        })
    })
})

function filter(value) {
  //Creates an array of words
    let searchList = value.split(" ")
  //Search for any word inside the array
    $w("#dataset1").setFilter(wixData.filter().hasSome("title", searchList))
}


Stopword package is available for use now :blush:

I just asked myself why not using → Reg-Ex instead of third-party implementations like NPM-packages? NPM-packages can be broken time by time, right?