Match phrase prefix query

Use the match_phrase_prefix query to specify a phrase to match in order. The documents that contain the phrase you specify will be returned. The last partial term in the phrase is interpreted as a prefix, so any documents that contain phrases that begin with the phrase and prefix of the last term will be returned.

Similar to match phrase, but creates a prefix query out of the last term in the query string.

For differences between the match_phrase_prefix and the match_bool_prefix queries, see The match_bool_prefix and match_phrase_prefix queries.

The following example shows a basic match_phrase_prefix query:

  1. GET _search
  2. {
  3. "query": {
  4. "match_phrase_prefix": {
  5. "title": "the wind"
  6. }
  7. }
  8. }

copy

To pass additional parameters, you can use the expanded syntax:

  1. GET _search
  2. {
  3. "query": {
  4. "match_phrase_prefix": {
  5. "title": {
  6. "query": "the wind",
  7. "analyzer": "stop"
  8. }
  9. }
  10. }
  11. }

copy

Example

For example, consider an index with the following documents:

  1. PUT testindex/_doc/1
  2. {
  3. "title": "The wind rises"
  4. }

copy

  1. PUT testindex/_doc/2
  2. {
  3. "title": "Gone with the wind"
  4. }

copy

The following match_phrase_prefix query searches for the whole word wind, followed by a word that starts with ri:

  1. GET testindex/_search
  2. {
  3. "query": {
  4. "match_phrase_prefix": {
  5. "title": "wind ri"
  6. }
  7. }
  8. }

copy

The response contains the matching document:

Response

  1. {
  2. "took": 6,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.92980814,
  16. "hits": [
  17. {
  18. "_index": "testindex",
  19. "_id": "1",
  20. "_score": 0.92980814,
  21. "_source": {
  22. "title": "The wind rises"
  23. }
  24. }
  25. ]
  26. }
  27. }

Parameters

The query accepts the name of the field (<field>) as a top-level parameter:

  1. GET _search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "<field>": {
  6. "query": "text to search for",
  7. ...
  8. }
  9. }
  10. }
  11. }

copy

The <field> accepts the following parameters. All parameters except query are optional.

ParameterData typeDescription
queryStringThe query string to use for search. Required.
analyzerStringThe analyzer used to tokenize the query.
max_expansionsPositive integerThe maximum number of terms to which the query can expand. Fuzzy queries “expand to” a number of matching terms that are within the distance specified in fuzziness. Then OpenSearch tries to match those terms. Default is 50.
slop0 (default) or a positive integerControls the degree to which words in a query can be misordered and still be considered a match. From the Lucene documentation: “The number of other words permitted between words in query phrase. For example, to switch the order of two words requires two moves (the first move places the words atop one another), so to permit reorderings of phrases, the slop must be at least two. A value of zero requires an exact match.”