# Slow wildcard query with fast term filters

**URL:** <https://discuss.elastic.co/t/slow-wildcard-query-with-fast-term-filters/133073>\
**Category:** Elasticsearch\
**Created:** [May 24, 2018, 5:36am UTC](https://discuss.elastic.co/t/slow-wildcard-query-with-fast-term-filters/133073 "2018-05-24T05:36:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman\_Margolis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_margolis/32/50393_2.png) [@Roman\_Margolis](https://discuss.elastic.co/u/Roman_Margolis)\
**Post date:** [May 24, 2018, 5:36am UTC](https://discuss.elastic.co/t/slow-wildcard-query-with-fast-term-filters/133073/1 "2018-05-24T05:36:33Z")

</div>

Hi, i have a simple query that looks like this:

```
{
   "query": {
        "bool": {
            "filter": {
                 "bool": {
                       "must": [
                              { "term": { "a": "term1" }},
                              { "term": { "a": "term2" }},
                              { "term": { "a": "term3" }},
                              { "term": { "a": "term4" }},
                              { "term": { "a": "term5" }},
                              { "wildcard": { "b": "*a rather*long*expression of*various*words" }}
                        ]
                 }
            }
       }
   }
}

```

The query takes about 500 ms to execute on an index with 3 mil documents with 4 even shards on ES 5.3.2.  
The query returns no results for these particular terms and wildcard expression, which is fine.  
If I remove the wildcard query, the query returns in 2 ms, also with no results (again, this is fine).  
I tried placing the wildcard query in different positions inside the bool, to no avail.

I thought that the wildcard filter, combined with the term filters, would execute efficiently, but it looks like the wildcard filter scans the entire index regardless of the term filters.

Is there any way to make this work efficiently?

Thanks,  
Roman Margolis

---

<div class="post-metadata">

**Author:** ![Roman\_Margolis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_margolis/32/50393_2.png) [@Roman\_Margolis](https://discuss.elastic.co/u/Roman_Margolis)\
**Post date:** [May 24, 2018, 6:46am UTC](https://discuss.elastic.co/t/slow-wildcard-query-with-fast-term-filters/133073/2 "2018-05-24T06:46:06Z")

</div>

I think I can answer my own question. From various sources, i gather that wildcard filter always scans the entire index, regardless what other filters are present, because they are cacheable.

So it appears there's no way to do what i want efficiently, out of the box. However, a rather simple solution can be built: a script wildcard filter (or wildcard plugin). Script filters are executed later in the query pipeline (I think), after the more cheap term filters, and operate on the already filtered bitset.

What do you think?

---

<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:** [June 21, 2018, 6:46am UTC](https://discuss.elastic.co/t/slow-wildcard-query-with-fast-term-filters/133073/3 "2018-06-21T06:46:09Z")

</div>

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