# How to exclude facets in filters or get conditional filters/conditionalFacets

**URL:** https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044
**Category:** Elastic Search
**Tags:** elastic-app-search
**Created:** [October 31, 2019, 1:19pm UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044 "2019-10-31T13:19:05Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Swapnil\_Ghorpade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swapnil_ghorpade/32/56943_2.png) [@Swapnil\_Ghorpade](https://discuss.elastic.co/u/Swapnil_Ghorpade)
#### Post date: [October 31, 2019, 1:19pm UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/1 "2019-10-31T13:19:05Z")

</div>

**App structure**  
Engine : national-parks-example  
schema: [title, id, state, description].

Facet shows filters based on state field values.

I have following scenario  
There are different users who can access documents from engine.

1. User's will be able to access documents excluding some states (excluding restricted states for each type of users) .

2. Show respected state facet/filters to users  
Example : Consider there are 2 users

User-1 : restricted states ['California']  
User-2 : restricted states ['Texas', 'Florida']

```
* If User-1 logged in he can see all states documents except 'California'
  * Facets will show all states in filters except 'California'
* For User-2 : show documents excluding state 'Texas' and 'Florida'
        * Facets will show all states in filters except 'Texas', 'Florida'

```

---

<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: [October 31, 2019, 5:05pm UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/2 "2019-10-31T17:05:08Z")

</div>

@Swapnil_Ghorpade You can achieve this with Signed Search Keys in App Search. There's a little more information on this post: [Is there any way to implement document level security in app search?](https://discuss.elastic.co/t/is-there-any-way-to-implement-document-level-security-in-app-search/201154/3).

---

<div class="post-metadata">

### Author: ![Swapnil\_Ghorpade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swapnil_ghorpade/32/56943_2.png) [@Swapnil\_Ghorpade](https://discuss.elastic.co/u/Swapnil_Ghorpade)
#### Post date: [November 1, 2019, 6:01am UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/3 "2019-11-01T06:01:38Z")

</div>

@JasonStoltz Thanks for the quick response.

What I want is ,  
consider I have states array : ['Texas', 'Florida'] and I want to show all documents from engine excluding these two states.  
so the result will return all documents where field state not equal to 'Texas' or 'Florida'

---

<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: [November 1, 2019, 10:35am UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/4 "2019-11-01T10:35:32Z")

</div>

@Swapnil_Ghorpade To exclude two states from a search, you would use a "none" filter: [https://swiftype.com/documentation/app-search/api/search/filters#combining-filters](https://swiftype.com/documentation/app-search/api/search/filters#combining-filters)

```auto
"filters": {
    "none": [
      { "title": "Texas" },
      { "title": "Florida" }
    ]
  }

```

And again, if you're trying to restrict that at a user level, then you would generate a signed search key for that user with the above filter applied.

---

<div class="post-metadata">

### Author: ![Swapnil\_Ghorpade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swapnil_ghorpade/32/56943_2.png) [@Swapnil\_Ghorpade](https://discuss.elastic.co/u/Swapnil_Ghorpade)
#### Post date: [November 1, 2019, 11:02am UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/5 "2019-11-01T11:02:36Z")

</div>

@JasonStoltz thanks looks like this will help me.

I tried this but is shows error PFA  
I Have to show state facet in filters too.

[https://codesandbox.io/s/national-parks-example-kdyms](https://codesandbox.io/s/national-parks-example-kdyms)

 ![filter](https://us1.discourse-cdn.com/elastic/original/3X/f/4/f43961546ad850e6cbf1caebbae98b8056e12377.png)

---

<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: [November 1, 2019, 11:29am UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/6 "2019-11-01T11:29:47Z")

</div>

I didn't realize you were using this with Search UI.

Query configuration looks a little bit different in Search UI: [https://github.com/elastic/search-ui/blob/master/ADVANCED.md#query-config](https://github.com/elastic/search-ui/blob/master/ADVANCED.md#query-config)

The `filters` property is an Array of [https://github.com/elastic/search-ui/blob/master/packages/react-search-ui/src/types/Filter.js](https://github.com/elastic/search-ui/blob/master/packages/react-search-ui/src/types/Filter.js).

So it would look like this:

```auto
searchQuery: {
    filters: [
      { field: "states", values: ["Texas", "California"], type: "none" }
    ]
}

```

That would apply the filter globally, for all users, on every query. This would not apply it at the user level, like you mentioned in your original post.

\*\* NOTE: I realized just now that this doesn't work correctly when you have the disjunctive facet flag set for states. I will need to enter a bug for this. If you want to see it working you'll need to remove states from "disjunctiveFacets" for now \*\*

---

<div class="post-metadata">

### Author: ![Swapnil\_Ghorpade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swapnil_ghorpade/32/56943_2.png) [@Swapnil\_Ghorpade](https://discuss.elastic.co/u/Swapnil_Ghorpade)
#### Post date: [November 1, 2019, 11:49am UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/7 "2019-11-01T11:49:40Z")

</div>

@JasonStoltz thanks , yes now its showing result according to filters config.

If I remove state from disjunctiveFacets, its just removing that state from facets

if I add filters (none) in searchQuery and remove state from disjunctiveFacets then its showing correct UI but after that not able to filter result on facet selection.

Do you it ? PFA

 ![filterIssue](https://us1.discourse-cdn.com/elastic/original/3X/c/5/c5999d6d274de9c10a04a89282f6a54af2b1cbab.png)

---

<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: [November 1, 2019, 12:15pm UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/8 "2019-11-01T12:15:28Z")

</div>

Yeah looks like a bug.

FWIW, you won't need to do that in Search UI if you are going to used Signed Search keys, it will just be part of the key.

---

<div class="post-metadata">

### Author: ![Swapnil\_Ghorpade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swapnil_ghorpade/32/56943_2.png) [@Swapnil\_Ghorpade](https://discuss.elastic.co/u/Swapnil_Ghorpade)
#### Post date: [November 1, 2019, 12:43pm UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/9 "2019-11-01T12:43:40Z")

</div>

@JasonStoltz yeah its like a bug.

Right now don't want user level restriction. Its just a document field level restriction as done through filters config (using type none). Thanks its working !

Facets are not working when using these filters in searchQuery. could you please suggest me the way to work this ?

---

<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: [November 1, 2019, 1:29pm UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/10 "2019-11-01T13:29:03Z")

</div>

@Swapnil_Ghorpade

Luckily, we have a hook in the AppSearchAPIConnector which lets us modify requests and responses directly.

[https://github.com/elastic/search-ui/blob/master/ADVANCED.md#api-config](https://github.com/elastic/search-ui/blob/master/ADVANCED.md#api-config)

Something like this should work for you for now, as a workaround:

```auto
beforeSearchCall: async (existingSearchOptions, next) => {
      const existingFilters = existingSearchOptions.filters || { none: [] };

      // Make sure none filter is applied to query
      const filters = {
        ...existingFilters,
        none: (existingFilters.none || [])
          .concat({
            states: "Texas"
          })
          .concat({
            states: "Florida"
          })
      };

      const results = await next({
        ...existingSearchOptions,
        filters
      });

      // Make sure none values are filtered out of facet results
      results.facets.states[0].data = results.facets.states[0].data.filter(
        d => !["Texas", "Florida"].includes(d.value)
      );
      return results;
    }
  });

```

---

<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: [November 1, 2019, 1:36pm UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/11 "2019-11-01T13:36:05Z")

</div>

@Swapnil_Ghorpade You could also just use a signed search key as a workaround. That would ensure that all API requests are filtered.

---

<div class="post-metadata">

### Author: ![Swapnil\_Ghorpade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swapnil_ghorpade/32/56943_2.png) [@Swapnil\_Ghorpade](https://discuss.elastic.co/u/Swapnil_Ghorpade)
#### Post date: [November 2, 2019, 9:41am UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/12 "2019-11-02T09:41:34Z")

</div>

@JasonStoltz Thank you. AppSearchAPIConnector works for 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: [November 30, 2019, 9:41am UTC](https://discuss.elastic.co/t/how-to-exclude-facets-in-filters-or-get-conditional-filters-conditionalfacets/206044/13 "2019-11-30T09:41:39Z")

</div>

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