Problem in phrase_prefix with filter

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:

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:

{
  "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.

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