How to query database to find string entry containing *whole word* within the string, not just a fragment

Before you respond to this question with ’ use .eq ', please read and understand the whole problem. This is NOT about the wix search bar. It is about the wix database query method.

Consider the case where I am searching a database that contains a text field. As an example only, let’s say there are only seven entries in the database and the text field contains the following:
1: “This is a pathetic sentence”
2: “This is the way you pat the cat”
3: “This entry also has the word pat in it”
4: “This one does not”
5: “Pat the puppy”
6: “The puppy’s name is pat”
7: “The puppy’s name with a full stop is pat.”

My search word is “pat”
I want to be able to find entries that contain the whole word “pat”. Ie. I do not want the query to return entries that contain ‘pat’ as a fragment of one of the words. You can liken this to the ‘whole word’ search that can be done in any text editor and many other database tools. Ie, a correct query result for a ‘whole word’ = “pat” would return entries 2, 3, 5, 6, 7

.eq(‘pat’) will not return any entries - you can’t use .eq to solve this problem
.contains will return entries 2 ,3, 5, 6, 7 but it will also return 1 because ‘pat’ is a fragment of ‘pathetic’
So, neither of these approaches work for searching for a ‘whole word’.

Putting spaces around the search term Eg " pat " will return 2, 3 only but will miss 5, 6 and 7

The only solution I have come up with is horrible - upon creating the database entry, create an extra field with the same entry but with spaces added at the beginning and the end of the field and all non-alphanumeric characters replaced with a space… This will allow the ‘pat’ search term to find 5 and 7.

A .containsWholeWord query method for the wix function would solve this problem.

Does anyone else have a better solution than burning database space and more importantly comms bandwidth?

Hello psav_wix,

i just tried to construct something what could fit your wishes, take a look here…
https://russian-dima.wixsite.com/meinewebsite/search-box

It is still not perfect working, but with some code-upgrades and additional functions, your issue should be solved.

You have just to work further on this code…

import wixData from 'wix-data';

$w.onReady(function () {
 //TODO: write your page related code here...

});

export function button1_click(event) {SEARCH()}

function SEARCH (parameter) {
 var wordCounter = 0
 let myText = $w('#TXTinput').text
    $w('#TXToutput').text = ""
 
 var searchValue = $w('#input1').value//.toLowerCase();
 var searchWordLength = searchValue.length
 var searchWords = myText.split(' ');
 var searchWords1 = []
 var searchWords2 = []
 var searchWords3 = []

    $w('#wordLength').value=searchWordLength
 
    console.log(searchWords)
 
 for (let i=0; i < searchWords.length; i++) {
 let mySearchWord=searchWords[i]

            console.log(mySearchWord)

 if (mySearchWord.slice(0,searchWordLength)==searchValue || mySearchWord.slice(0,searchWordLength)==searchValue.toLowerCase()) {
 if (mySearchWord.charCodeAt(searchWordLength+1) >=65 && mySearchWord.charCodeAt(searchWordLength+1) <=90) {}
 else if (mySearchWord.charCodeAt(searchWordLength+1) >=98 && mySearchWord.charCodeAt(searchWordLength+1) <=122) {}
 else if (mySearchWord.charCodeAt(searchWordLength+1) >=192 && mySearchWord.charCodeAt(searchWordLength+1) <=255) {}
 else {searchWords1.push(mySearchWord), wordCounter++}
            }

 if (mySearchWord.slice(1,searchWordLength+1)==searchValue || mySearchWord.slice(1,searchWordLength+1)==searchValue.toLowerCase()) {console.log("YES")
                console.log(mySearchWord.charCodeAt(1))
 if (mySearchWord.charCodeAt(0) >=65 && mySearchWord.charCodeAt(0) <=90) {console.log("1")}
 else if (mySearchWord.charCodeAt(0) >=98 && mySearchWord.charCodeAt(0) <=122) {console.log("2")}
 else if (mySearchWord.charCodeAt(0) >=192 && mySearchWord.charCodeAt(0) <=255) {console.log("3")}   
 else {console.log("FOUND"), searchWords2.push(mySearchWord), wordCounter++}
            }
        }
        searchWords3 = searchWords1 + searchWords2
        console.log("---- 1 ----")  
        console.log(searchWords1)
        console.log("---- 2 ----")  
        console.log(searchWords2)
        console.log("---- 3 ----")
        console.log(searchWords3)
 
        $w('#OUTPUTfoundterms').value=wordCounter
        $w('#TXTresults').text=searchWords3
        $w('#TXToutput').text = searchWords.toString()
}

Following problem still exists and has to be fixed (when searching for “puppy” it does not find all relevant values) —> has to be fixed in code.

Lower-Casesearching, does not show all results yet (code has to be expanded).

The “Result-Output” shows the raw-result (that means, it shows the founded terms in their splitted form.) —> code has to be expanded, for showing all “search-term” in editet form.

Take a look to the console-log (press F-12 in your google-chrome-browser and go to console)

Sorry, I don’t see how this works for a wix database query.

.contains will return entries 2 ,3, 5, 6, 7 but it will also return 1 because ‘pat’ is a fragment of ‘pathetic’
Just take a look why my code do not count —> “pathetic”, like it would be done by filtering with “contains”. How i did it?

So your solution could be a combination of “contains” (you will get all the needed values / search-terms, then you cut-out —> “pathetic” like i did it in my code.

I think that should work.

Thanks - I think I now understand - you are retrieving all the results with a query and then post-processing the items returned.

I had thought of doing this already but I discounted it because a large database the initial query could return MANY more items than required. In large databases, this will result in poor performance because many more items are being returned by the initial query.

However, I now realise and wonder if doing this post-filtering in a back-end module would help since that code would be running on servers with lots of bandwidth and processing power. Then just return the post-processed results to the front-end.

It would be better if wix provided a proper ‘whole word’ query though. :slight_smile:

Thanks for your effort!!

No problem you are welcome.
I just wanted to show, that there is always a solution for a given problem.
You have just to work on it.

Ok, i did not knew your whole issue, i just saw a fragment of it (how to get the whole word and not just a fragment). :wink:

Good luck, i am sure you will find your solution (you are not stupid). :blush: