# Deduce filters from search string

**URL:** <https://discuss.elastic.co/t/deduce-filters-from-search-string/265353>\
**Category:** Elasticsearch\
**Created:** [February 24, 2021, 2:09pm UTC](https://discuss.elastic.co/t/deduce-filters-from-search-string/265353 "2021-02-24T14:09:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian87](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian87/32/84527_2.png) [@Christian87](https://discuss.elastic.co/u/Christian87)\
**Post date:** [February 24, 2021, 2:09pm UTC](https://discuss.elastic.co/t/deduce-filters-from-search-string/265353/1 "2021-02-24T14:09:43Z")

</div>

Hi,

let's say I store a catalogue of household appliances in Elasticsearch. Documents in my index can represent dish washers, washing machines, freezers and similar devices.

Dish washers have a text field "type" which can contain one of the following strings:

- integrated
- semi-integrated
- freestanding
- table top

If a search string contains any of these terms I want to automatically apply a filter to my query.  
For example if the search string contains 'table top' I want to only select documents where type is exactly 'table top'.

Dish washer type is only one of many filters I want to apply automatically. If a user was searching for a cooker, the type could be something like 'gas cooker' or 'induction cooker'. Of course there can be more than one filter per device. A cooker could also be 'with grill' or 'without grill'. That information would be in a dedicated field 'grill' and I would like to apply a filter on that field as well if the search string contains 'with grill' or 'without grill'.

Pseudo code for what I want to achieve in Elasticsearch:

- search string = 'semi-integrated dish washer' should only return documents where type = "semi-integrated"'
- search string = 'gas cooker with grill' should only return documents where type = "gas cooker" and grill = 'with grill'

I could try to solve this outside of a Elasticsearch for example in plain Java code, but then I could not take advantage of Elasticsearch features like synonyms, so: What is the best way to achieve this in Elasticsearch?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [February 24, 2021, 2:39pm UTC](https://discuss.elastic.co/t/deduce-filters-from-search-string/265353/2 "2021-02-24T14:39:39Z")

</div>

This is something I'd call "category snapping" - detecting category names in the text of simple query input and rewriting the query to be a more structured query of the form

```
bool
    must
            term
                     categoryKeyword:integrated
            query_string
                     freeTextField: Bosch dishwasher 

```

That's a very domain-specific set of pattern-matching rules you need to pull together and [querqy](https://github.com/querqy/querqy-elasticsearch) might be a useful framework for managing this.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [February 24, 2021, 2:56pm UTC](https://discuss.elastic.co/t/deduce-filters-from-search-string/265353/3 "2021-02-24T14:56:59Z")

</div>

One other approach which relies less on manual rule curation is to use a statistical approach to suggesting relevant filters or facets based on analysing the top matches. Check out the comments below [this youtube video](https://www.youtube.com/watch?v=W8DJYfAKKLA&t=41s) for a way of selecting relevant filters to suggest to your searchers.

---

<div class="post-metadata">

**Author:** ![Christian87](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian87/32/84527_2.png) [@Christian87](https://discuss.elastic.co/u/Christian87)\
**Post date:** [February 24, 2021, 3:12pm UTC](https://discuss.elastic.co/t/deduce-filters-from-search-string/265353/4 "2021-02-24T15:12:31Z")

</div>

Thanks a lot, Mark!

querqy looks very promising and I will try to implement my use case with it. I will also check out the youtube video tomorrow. 🙂

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [February 24, 2021, 3:15pm UTC](https://discuss.elastic.co/t/deduce-filters-from-search-string/265353/5 "2021-02-24T15:15:40Z")

</div>

No worries.

> I will also check out the youtube video tomorrow

The video might be useful background on the business problem but their proposed elasticsearch solution is quite inefficient, hence my comments below the video.

---

<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:** [March 24, 2021, 3:15pm UTC](https://discuss.elastic.co/t/deduce-filters-from-search-string/265353/6 "2021-03-24T15:15:44Z")

</div>

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