# Is it possible to configure filters that use boolean logic for SearchProvider?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352>\
**Category:** Elastic Search\
**Created:** [September 14, 2022, 12:26am UTC](https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352 "2022-09-14T00:26:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beratna](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Beratna](https://discuss.elastic.co/u/Beratna)\
**Post date:** [September 14, 2022, 12:26am UTC](https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352/1 "2022-09-14T00:26:53Z")

</div>

Hi there,

I'm currently using App Search with search-ui react library.

I'm looking for a way to apply boolean logic to the filters inside my searchQuery config.

Let's say I have 10 documents in my search engine.

Each document represents a coffee shop that has 2 fields: "shop\_name" and "shop\_country"

For those 10 documents: there are 5 coffee shops in the US, 3 shops in Canada, and 2 shops in Europe.

What would be the best way to query for coffee shops in North America (US + Canada)?

Let's say I'm passing this config prop into SearchProvider:

```auto
    const config = {
        searchQuery: {
            facets: buildFacetConfigFromConfig(),
            disjunctiveFacets: [],
            filters: [{ field: 'shop_country', values: ['US'] }],
        },
        apiConnect: newAppSearchAPIConnector({...}),
    }

   <SearchProvider config={config}>...</SearchProvider>

```

Is there a way to apply an "OR" condition to filters?

Or would I have to add shop\_country as a facet, and make it disjunctive? But then how can I filter for 2 values?

Or would I have to change the schema and add a new field like 'north\_america': 'true' ? and query for that specific field?

Thanks for the help in advance!!

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [September 14, 2022, 12:53pm UTC](https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352/2 "2022-09-14T12:53:21Z")

</div>

Sure is!

```auto
filters: [
      {
        field: "shop_country",
        type: "any",
        values: ["US", "Canada"]
      }
    ],

```

They key here is to use `type: "any"`.

The type definitions for this stuff can be found [here](https://github.com/elastic/search-ui/blob/38f111e336dd6715da3eb263c13f8132b04d188e/packages/search-ui/src/types/index.ts#L44).

We should make this more apparent in our documentation.

---

<div class="post-metadata">

**Author:** ![Beratna](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Beratna](https://discuss.elastic.co/u/Beratna)\
**Post date:** [September 14, 2022, 3:30pm UTC](https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352/3 "2022-09-14T15:30:55Z")

</div>

That works, thanks Jason!!

I appreciate the type definitions link 🙏

---

<div class="post-metadata">

**Author:** ![Beratna](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Beratna](https://discuss.elastic.co/u/Beratna)\
**Post date:** [September 14, 2022, 4:12pm UTC](https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352/4 "2022-09-14T16:12:09Z")

</div>

Sorry Jason, I have one more related question:

Is it possible to apply an "OR" condition BETWEEN filters?

Let's say I have an additional field called "shop\_shipping", because some US shops offer shipping to Canada.

If I try:

```auto
filters: [
      {
        field: "shop_country",
        type: "any",
        values: ["US", "Canada"]
      },

     {
       field: "shop_shipping",
       values: ["Canada"]
     }
    ],

```

If I add another filter object to the filters array, it narrows down the search, which is fine if I have documents that include 'Canada' in both shop\_country and shop\_shipping. But then I'm not sure how I could include European shops that ship to both US and Canada or just the US.

Basically, I'm trying to figure out the right schema and filters for these conditions:

"I'm in the US, which US or Canadian or European shops can SHIP to the US?"

"I'm in Canada, which US or Canadian or European shops can SHIP to Canada?"

"I'm in Europe, which shops can SHIP to Europe?"

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [September 14, 2022, 6:09pm UTC](https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352/5 "2022-09-14T18:09:30Z")

</div>

I don't think I understand... this doesn't work?

"I'm in the US, which US or Canadian or European shops can SHIP to the US?"

```auto
filters: [
      {
        field: "shop_country",
        values: ["US"]
      },

     {
       field: "shop_shipping",
        type: "any",
       values: ["Canada", "US", "Europe"]
     }
    ],

```

"I'm in Canada, which US or Canadian or European shops can SHIP to Canada?"

```auto
filters: [
      {
        field: "shop_country",
        values: ["Canada"]
      },

     {
       field: "shop_shipping",
        type: "any",
       values: ["Canada", "US", "Europe"]
     }
    ],

```

"I'm in Europe, which shops can SHIP to Europe?"

```auto
filters: [
      {
        field: "shop_country",
        values: ["Europe"]
      },

     {
       field: "shop_shipping",
        type: "any",
       values: ["Europe"]
     }
    ],

```

---

<div class="post-metadata">

**Author:** ![Beratna](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Beratna](https://discuss.elastic.co/u/Beratna)\
**Post date:** [September 14, 2022, 8:06pm UTC](https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352/6 "2022-09-14T20:06:29Z")

</div>

Thanks for your help Jason!! Sorry, I should be more clear

"I'm in the US, which US or Canadian or European shops can SHIP to the US?"

```auto
filters: [
      {
        field: "shop_country",
        values: ["US"]
      },

     {
       field: "shop_shipping",
        type: "any",
       values: ["Canada", "US", "Europe"]
     }
    ],

```

Wouldn't the config above would return all shops headquartered in US that can ship to either US or Europe or Canada?

Instead, I'm trying to return all shops headquartered ANYWHERE that can ship to the US. So this would make more sense:

```auto
filters: [
      {
        field: "shop_country",
        type: 'any'
        values: ["US", "Canada", "Europe"]
      },

     {
       field: "shop_shipping",
       values: ["US"]
     }
    ],

```

But what I can't figure out is how to arrange the schema because technically shop\_shipping could be an array:

```auto
{
   shop_name: 'Coffee Shop based in America that can also ship to Canada but not Europe',
   shop_country: 'US',
   shop_shipping: ['US', 'Canada']
}

```

Would it be better to create a new fields like this?

```auto
{
    shop_name: 'Coffee shop based in Canada that ships to North America',
    shop_country: 'Canada',
    ships_to_US: true,
    ships_to_Canada: true,
    ships_to_Europe: false,
}

```

Then write queries like this perhaps?

```auto
   const [country, setCountry] = useState('US')

   filters: [
    {
        [`ships_to_${country}`]: true
    },

   ]

```

Thanks for your help again! I'm trying to brainstorm the best way to do this based on what I can do with config queries

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [September 15, 2022, 12:16pm UTC](https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352/7 "2022-09-15T12:16:43Z")

</div>

Have you tried this out yet? I'm pretty sure this will just work.

This:

```auto
     {
       field: "shop_shipping",
       values: ["US"]
     }

```

Will match on this:

```auto
{
   shop_name: 'Coffee Shop based in America that can also ship to Canada but not Europe',
   shop_country: 'US',
   shop_shipping: ['US', 'Canada']
}

```

---

<div class="post-metadata">

**Author:** ![Beratna](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Beratna](https://discuss.elastic.co/u/Beratna)\
**Post date:** [September 15, 2022, 5:20pm UTC](https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352/8 "2022-09-15T17:20:34Z")

</div>

Wow it works!! Thanks Jason!! I thought App Search only accepted strings, booleans, numbers and geolocations 🤔

I'll be indexing docs with a shipping array from now on.

But in my current situation, I have thousands of docs like this with missing shipping data:

```auto
{
   shop_name: 'American based shop with missing shipping data',
   shop_country: 'US',
}

```

Right now, the config below excludes all of those docs:

```auto
     {
       field: "shop_shipping",
       values: ["US"]
     }

```

Is it possible to write a config that will match BOTH these docs below using a `'US'` value?

```auto
{
   shop_name: 'American shop with missing shipping data',
   shop_country: 'US',
}

{
   shop_name: 'American shop with clean shipping data',
   shop_country: 'US',
   shop_shipping: ['US', 'Canada']
}

```

Because something like this:

```auto
     {
       field: "shop_shipping",
       values: ["US"]
     },
     {
       field: "shop_country",
       values: ["US"]
     }

```

would only return docs with shipping data:

```auto
{
   shop_name: 'American shop with clean shipping data',
   shop_country: 'US',
   shop_shipping: ['US', 'Canada']
}

```

Thanks for your help Jason! I appreciate it! 🙏

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [September 15, 2022, 5:41pm UTC](https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352/9 "2022-09-15T17:41:54Z")

</div>

I don't think you can write a filter that matches on 'US' OR nothing. I think you'd have to actually have to index a value that represents, like 'None' or something.

So 'US' or 'None'. I don't know though, you might have to play around a little bit with the API and see what works!

---

<div class="post-metadata">

**Author:** ![Beratna](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Beratna](https://discuss.elastic.co/u/Beratna)\
**Post date:** [September 15, 2022, 5:50pm UTC](https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352/10 "2022-09-15T17:50:52Z")

</div>

Ahh that's unfortunate but not a big deal! I'll just re-index the old docs.

Thanks for taking your time to help me!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 13, 2022, 5:51pm UTC](https://discuss.elastic.co/t/is-it-possible-to-configure-filters-that-use-boolean-logic-for-searchprovider/314352/11 "2022-10-13T17:51:15Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
