combination of .hasSome and .contains

hasSome(string, array) : some values in the array is equal to string
I want : some values in array is part of the string

Is there a function that combines dataset.setFilter().hasSome() and .setFilter().contains()

say my value the string is “my name is bryan” and an array = [‘name’, ‘bryan’].

It won’t work for the following line:
dataset.setFilter().hasSome(‘my_field’, array);

In this case ‘my_field’ will return “my name is bryan”

Hi Bryan,

Welcome to the Wix Code forums.

I’m not sure what your problem is…

In your database collection, you have an item that has a field with the value “my name is bryan”.
The array does in fact have some of the value (name and bryan).
Therefore, the filter returns the item with the field value of “my name is bryan”

This is exactly what should happen. What do you want to happen?

Yisrael

yes you are right

Hi Bryan & Yisrael,
I have been trying to replicate this. But what I found was that “hasSome” looks for an exact match for the values in array = [‘name’,‘bryan’] for a given propertyName field; which in this case returns ‘my name is bryan’. This means that ‘my name is bryan’ would NOT be returned. However, if the propertyName field returned ‘name’ or ‘bryan’ on their own, then that would be returned. This is what the original question was referring too. Could either one of you shed some light on that please?

Hi All,

I was looking to do the same sort of thing. I’ve ended up doing this. It constructs an series of “contains” filters, strung together as “or” queries. The input string is split into an array delimited by spaces. It will basically search all of the search fields for all of the words in the search.

The if “f” is true at the start is a bit messy but it is there to basically make the first query constructed from scratch rather than as an “or” off an empty query, as an empty query searches all.

I hope that is helps someone else. Happy to see it become more elegant.

$w("#voucherDataset").onReady(() => {
    //split string into array separated by spaces
    var stringArray = $w("#searchInput").value.split(" ", 10);
    var fieldArray = ["voucherCode", "issueeFname", "issueeLname", "issueeEmail"];

    let filter = wixData.filter();
    let f = true;
    fieldArray.forEach((field) => {
        stringArray.forEach((word) => {
            if (f === true) {
                filter = wixData.filter().contains(field, word);
                f = false;
            } else {
                filter = filter.or(wixData.filter().contains(field, word));
            }
        })
    });
    $w("#voucherDataset").setFilter(filter);
});

1 Like