# Problem in phrase\_prefix with filter

**URL:** <https://discuss.elastic.co/t/problem-in-phrase-prefix-with-filter/297538>\
**Category:** Elasticsearch\
**Tags:** eql-elastic-query-language\
**Created:** [February 17, 2022, 7:17pm UTC](https://discuss.elastic.co/t/problem-in-phrase-prefix-with-filter/297538 "2022-02-17T19:17:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kalavero](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kalavero/32/101963_2.png) [@Kalavero](https://discuss.elastic.co/u/Kalavero)\
**Post date:** [February 17, 2022, 7:17pm UTC](https://discuss.elastic.co/t/problem-in-phrase-prefix-with-filter/297538/1 "2022-02-17T19:17:17Z")

</div>

I have a query that I use for a search\_as\_you\_type field that is not returning the expected value.

The query looks something like this:

```auto
GET users/_search
{
  "_source": ["first_name","full_name","last_name","marketplace.enrolled"], 
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "multi_match": {
          "query": "John S",
          "max_expansions": 50, 
          "type": "phrase_prefix",
          "fields": [
            "full_name",
            "full_name.search_as_you_type"
          ]}
        }
      ],
      "filter": [
        {
        "bool": {
          "should": [
            {
              "term": {
                "marketplace.enrolled": {
                  "value": "true"
                }
              }
            }
          ]
        }
        }
      ]
    }
  },
  "size": 500
}

```

Result:

```auto
{
  "took" : 50,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  }
}

```

My problem is that this query returns no results even though there are several "John S" in the database with the ´"marketplace.enrolled": "true"´.

After some debugging, I have found out the problem is that the ´phrase\_prefix´ is getting the first part "John" and then getting the first 50 results and then applying the filter in a way that the first 50 results do not necessarily have the ´"marketplace.enrolled": "true"´ so in that word with 50 "John" none of them has a surname starting with "S".

My question is, how can I improve this query so that the filter is applied before I get the first 50 results?

I've been crazy to solve this for the past week so any help would be very appreciated.

PS: I know that the max\_expansions would help a bit with that but would not solve the main issue I am having.

---

<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 17, 2022, 7:17pm UTC](https://discuss.elastic.co/t/problem-in-phrase-prefix-with-filter/297538/2 "2022-03-17T19:17:19Z")

</div>

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