Match phrase query

Use the match_phrase query to match documents that contain an exact phrase in a specified order. You can add flexibility to phrase matching by providing the slop parameter.

The match_phrase query creates a phrase query that matches a sequence of terms.

The following example shows a basic match_phrase query:

  1. GET _search
  2. {
  3. "query": {
  4. "match_phrase": {
  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": {
  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 query searches for the phrase wind rises, where the word wind is followed by the word rises:

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

copy

The response contains the matching document:

Response

  1. {
  2. "took": 30,
  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. }

Analyzer

By default, when you run a query on a text field, the search text is analyzed using the index analyzer associated with the field. You can specify a different search analyzer in the analyzer parameter. For example, the following query uses the english analyzer:

  1. GET testindex/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "title": {
  6. "query": "the winds",
  7. "analyzer": "english"
  8. }
  9. }
  10. }
  11. }

copy

The english analyzer removes the stopword the and performs stemming, producing the token wind. Both documents match this token and are returned in the results:

Response

  1. {
  2. "took": 2,
  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": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.19363807,
  16. "hits": [
  17. {
  18. "_index": "testindex",
  19. "_id": "1",
  20. "_score": 0.19363807,
  21. "_source": {
  22. "title": "The wind rises"
  23. }
  24. },
  25. {
  26. "_index": "testindex",
  27. "_id": "2",
  28. "_score": 0.17225474,
  29. "_source": {
  30. "title": "Gone with the wind"
  31. }
  32. }
  33. ]
  34. }
  35. }

Slop

If you provide a slop parameter, the query tolerates reorderings of the search terms. Slop specifies the number of other words permitted between words in a query phrase. For example, in the following query, the search text is reordered compared to the document text:

  1. GET _search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "title": {
  6. "query": "wind rises the",
  7. "slop": 3
  8. }
  9. }
  10. }
  11. }

The query still returns the matching document:

Response

  1. {
  2. "took": 2,
  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.44026947,
  16. "hits": [
  17. {
  18. "_index": "testindex",
  19. "_id": "1",
  20. "_score": 0.44026947,
  21. "_source": {
  22. "title": "The wind rises"
  23. }
  24. }
  25. ]
  26. }
  27. }

Empty query

For information about a possible empty query, see the corresponding match query section.

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 string text. Default is the index-time analyzer specified for the default_field. If no analyzer is specified for the default_field, the analyzer is the default analyzer for the index.
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.”
zero_terms_queryStringIn some cases, the analyzer removes all terms from a query string. For example, the stop analyzer removes all terms from the string an but this. In those cases, zero_terms_query specifies whether to match no documents (none) or all documents (all). Valid values are none and all. Default is none.