Phrase prefix query is not working with pagination

The following query does not give any results when the from parameter is greater than 50. The number of hits also reduces as this parameter is gradulaly increased from 0. This works as expected if the query type is bool_prefix.

All the users have name in the following format: "test_n", e.g. test_1, test_2, test_3, etc. And search.name, search.email are of type search_as_you_type.

GET /users/_search
{
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "filter": [
                        {
                            "term": {
                                "status.name": "active"
                            }
                        },
                        {
                            "term": {
                                "organisation._id": "1"
                            }
                        }
                    ],
                    "should": [
                        {
                            "multi_match": {
                                "boost": 1,
                                "query": "test_2",
                                "type": "phrase_prefix",
                                "fields": [
                                    "search.name",
                                    "search.email"
                                ]
                            }
                        }
                    ],
                    "minimum_should_match": 1
                }
            }
        }
    },
    "size": 5,
    "from": 50,
}

From Elastic Search to Elasticsearch

Hi @Aryan11 and welcome to the community.

The issue that you're experiencing is because the max_expansions parameter defaults to 50. You can set it at a higher number if you need more results. For example, setting max_expansions to 100 will now return a max of 100 results, and thus pagination starting from 50 will work on a document that has that many matching documents:

GET /test-prefix-query/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "multi_match": {
                "boost": 1,
                "query": "test_2",
                "type": "phrase_prefix",
                "max_expansions": 100,
                "fields": [
                  "name"
                ]
              }
            }
          ],
          "minimum_should_match": 1
        }
      }
    }
  },
  "size": 5,
  "from": 0
}

Hope that helps!

Thanks @Kathleen_DeRusso this works!

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