Database Search Against Multiple Fields

I have implemented a search against a database of magazines. Each magazine may have up to four articles of interest. I am trying to retrieve all magazines with the word or phrase entered into a search input box. It ONLY works against the first field. What Am I Missing here?

Url: https://jds-cvoa.wixsite.com/cvoa/copy-of-regional-newsletters

Wix code I am using:

import wixData from ‘wix-data’;

// For full API documentation, including code examples, visit Velo API Reference - Wix.com
$w.onReady(function () {
//TODO: write your page related code here…
});

export function searchButton_onclick() {
wixData.query(‘CVOA-Magazines’)
.contains(‘article1’ || ‘article2’ || ‘article3’ || ‘article4’,$w(‘#input1’).value)
.find()
.then(res=>{
$w(‘#table1’).rows = res.items;
});
}

Thanks,
JD

1 Like

Hi,
I recommend checking out this video tutorial . Moreover, you can check this thread for filtering repeater using multiple user inputs.

Good luck,
Tal.

Thanks Tal,
I already looked at that video but I don’t need the filter. Then I found Nayeli tutorial which was way more easier. https://youtu.be/ZdIB0Q7WXP4

After I got it working I modified the .contains to use an OR to see if the search value was contained in the other three database fields.

I just looked at the other thread. Should I be using multiple if statements?

Thanks
JD

Hey,
Can you please share the code you were asking about?

Tal

Scroll up… it is in my initial post. :slight_smile: HERE is is again:

import wixData from ‘wix-data’;
// For full API documentation, including code examples, visit http://wix.to/94BuAAs
$w.onReady(function () {
//TODO: write your page related code here…
});
export function searchButton_onclick() {
wixData.query(‘CVOA-Magazines’)
.contains(‘article1’ || ‘article2’ || ‘article3’ || ‘article4’,$w(’ #input1 ‘).value)
.find()
.then(res=>{
$w(’ #table1 ').rows = res.items;
});
}

Following her original code for the contains was:
.contains(‘article1’,$w(’ #input1 ').value)
And that works as expected. I added the OR condition to check the other fields. It only returns items with the match in ‘article1’.

Hi JD,

I’m developing a site with a different topic but similar data structure.
Did you solve your problem with multiple fields search? I’m desperate to make it run.

phip

Yes, you use the .or operator… here is the final code:

export function input1_keyPress(event, $w) {
if (event.key === “Enter”)
wixData.query(‘CVOA-Magazines’)
.contains(‘title’, $w(‘#input1’).value)
.or(wixData.query(‘CVOA-Magazines’).contains(‘article1’, $w(‘#input1’).value))
.or(wixData.query(‘CVOA-Magazines’).contains(‘article2’, $w(‘#input1’).value))
.or(wixData.query(‘CVOA-Magazines’).contains(‘article3’, $w(‘#input1’).value))
.or(wixData.query(‘CVOA-Magazines’).contains(‘article4’, $w(‘#input1’).value))

.find()
.then(res => {
$w(‘#repeater1’).data = res.items;
});
}

This code works perfectly! but I am trying to perfect this. If you write more than one word it doesn’t show any results unless the string be entered exactly as the data phrase is wrote, so I would like to make this can combine words with no matter the order are entered and so find combinations… EG


can some one help?

@cwvega76 , thanks for sharing! :blush:
It was helpful for me with a repeater filtering multiple fields:

let lastSearchValue;
function filter(searchValue) {
    if(searchValue !== lastSearchValue) {
        $w('#contactsDataset').setFilter(
            wixData.filter().contains('title', searchValue)
            .or(wixData.filter().contains('position', searchValue))
            .or(wixData.filter().contains('department', searchValue))
        );
        lastSearchValue = searchValue;
    }
}