opensearch-docs-cn/_query-dsl/full-text/match-phrase-prefix.md

3.9 KiB

layout title parent grand_parent nav_order
default Match phrase prefix Full-text queries Query DSL 40

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:

GET _search
{
  "query": {
    "match_phrase_prefix": {
      "title": "the wind"
    }
  }
}

{% include copy-curl.html %}

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

GET _search
{
  "query": {
    "match_phrase_prefix": {
      "title": {
        "query": "the wind",
        "analyzer": "stop"
      }
    }
  }
}

{% include copy-curl.html %}

Example

For example, consider an index with the following documents:

PUT testindex/_doc/1
{
  "title": "The wind rises"
}

{% include copy-curl.html %}

PUT testindex/_doc/2
{
  "title": "Gone with the wind"
  
}

{% include copy-curl.html %}

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

GET testindex/_search
{
  "query": {
    "match_phrase_prefix": {
      "title": "wind ri"
    }
  }
}

{% include copy-curl.html %}

The response contains the matching document:

Response {: .text-delta}
{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.92980814,
    "hits": [
      {
        "_index": "testindex",
        "_id": "1",
        "_score": 0.92980814,
        "_source": {
          "title": "The wind rises"
        }
      }
    ]
  }
}

Parameters

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

GET _search
{
  "query": {
    "match_phrase": {
      "<field>": {
        "query": "text to search for",
        ... 
      }
    }
  }
}

{% include copy-curl.html %}

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

Parameter Data type Description
query String The query string to use for search. Required.
analyzer String The analyzer used to tokenize the query.
max_expansions Positive integer The 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.
slop 0 (default) or a positive integer Controls 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."