# Match\_phrase\_prefix acts erratically when there is a filter

**URL:** <https://discuss.elastic.co/t/match-phrase-prefix-acts-erratically-when-there-is-a-filter/33395>\
**Category:** Elasticsearch\
**Created:** [October 30, 2015, 4:01pm UTC](https://discuss.elastic.co/t/match-phrase-prefix-acts-erratically-when-there-is-a-filter/33395 "2015-10-30T16:01:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paol](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@paol](https://discuss.elastic.co/u/paol)\
**Post date:** [October 30, 2015, 4:01pm UTC](https://discuss.elastic.co/t/match-phrase-prefix-acts-erratically-when-there-is-a-filter/33395/1 "2015-10-30T16:01:38Z")

</div>

I'm trying to make sense of the behaviour of this query:

```
GET /categories/category/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "classification_system_id": 1
        }
      },
      "must": {
        "match_phrase_prefix": {
          "category_id": {
            "query": "13",
            "max_expansions" : 20
          }
        }
      }
    }
  }
}

```

The term filter matches about 200 out of 26000 documents in the index.

The results of the match\_phrase\_prefix are highly unpredictable, it returns fewer results than it should for some prefixes but not others. For example if I search for prefix "13" it should find 10 but returns only 1, but for prefix "19" it returns all 11 results as it should. Other values are equally unpredictable. If the filter is removed the problem goes away, as far as I can tell.

Things improve only by bumping the max\_expansions way up. To get the expected results for 2 character prefixes requires max\_expansions around 200, and for 1 character prefixes around 1000!

Is this normal? If so, what is the proper solution? Setting max\_expansions to such high numbers appears to be a bad idea, if the documentation is to be believed.

---

<div class="post-metadata">

**Author:** ![mainec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mainec/32/5557_2.png) [@mainec](https://discuss.elastic.co/u/mainec)\
**Post date:** [November 2, 2015, 9:40am UTC](https://discuss.elastic.co/t/match-phrase-prefix-acts-erratically-when-there-is-a-filter/33395/2 "2015-11-02T09:40:19Z")

</div>

Also posting the documents you indexed (or a minimal set of them) would help reproduce your issue.

In general I think the following issue might be relevant to your question: [https://github.com/elastic/elasticsearch/issues/7485](https://github.com/elastic/elasticsearch/issues/7485)

---

<div class="post-metadata">

**Author:** ![paol](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@paol](https://discuss.elastic.co/u/paol)\
**Post date:** [November 3, 2015, 10:46am UTC](https://discuss.elastic.co/t/match-phrase-prefix-acts-erratically-when-there-is-a-filter/33395/3 "2015-11-03T10:46:54Z")

</div>

Yes, that appears to be the same problem. Thank you for the pointer.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [November 3, 2015, 1:05pm UTC](https://discuss.elastic.co/t/match-phrase-prefix-acts-erratically-when-there-is-a-filter/33395/4 "2015-11-03T13:05:02Z")

</div>

The trouble with max expansions is they that are done against the full  
terms dictionary. It'd be super expensive to do them in a filtered context.  
If you really want a prefix search you can analyze the string with edge  
ngrams. That makes for a larger index but there is no expansion to do so  
the querying is faster. You'd set the index\_analyzer with edge\_ngrams and  
the search analyzer without.

---

<div class="post-metadata">

**Author:** ![paol](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@paol](https://discuss.elastic.co/u/paol)\
**Post date:** [November 3, 2015, 2:24pm UTC](https://discuss.elastic.co/t/match-phrase-prefix-acts-erratically-when-there-is-a-filter/33395/5 "2015-11-03T14:24:31Z")

</div>

Yeah, it later occurred to me that the strange behaviour would be explained if the filter was being applied after the max\_expansions, where I had assumed it was applied before. Thanks for clarifying that.

This should probably be mentioned in the documentation of match\_phrase\_prefix, because the behaviour is unexpected if you don't know the inner workings of ES.

---

<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:** [July 5, 2017, 11:40pm UTC](https://discuss.elastic.co/t/match-phrase-prefix-acts-erratically-when-there-is-a-filter/33395/6 "2017-07-05T23:40:54Z")

</div>


