Search code examples
elasticsearchreactivesearch

Static filters in ReactiveSearch


In my application I'm using the excellent ReactiveSearch.

There are some constants which need to be filtered on every query. For example, to pass a list of ids which would always be filtered and then the user's filtering applied over the top.

There is a similar question here, but this is focused more on removing fields.

How can a pass an array of IDs to be filtered against a specific field in ElasticSearch? I'm happy to use any kind of hacky approach, such as making a custom component to house the IDs, and then hiding it with CSS, but if there is a cleaner approach please let me know.


Solution

  • One of the ways of solving this is using the prop defaultQuery in your result component. For example, ReactiveList supports this prop (docs). You can use this to run any query of your choice that would get applied along with other queries. For example:

    <ReactiveList
      ...
      defaultQuery={() => ({
        bool: {
          must_not: {
            term: {
              "original_title.raw": "The Last Guardian"
            }
          }
        }
      })}
    />
    

    This would filter out all the original_title.raw fields matching The Last Guardian. See demo.