2016-04-05 08:16:43 -04:00
|
|
|
[[query-dsl-match-query-phrase-prefix]]
|
2019-07-18 10:18:11 -04:00
|
|
|
=== Match phrase prefix query
|
|
|
|
++++
|
|
|
|
<titleabbrev>Match phrase prefix</titleabbrev>
|
|
|
|
++++
|
2016-04-05 08:16:43 -04:00
|
|
|
|
2019-08-06 14:01:49 -04:00
|
|
|
Returns documents that contain the words of a provided text, in the **same
|
|
|
|
order** as provided. The last term of the provided text is treated as a
|
|
|
|
<<query-dsl-prefix-query,prefix>>, matching any words that begin with that term.
|
2016-04-05 08:16:43 -04:00
|
|
|
|
|
|
|
|
2019-08-06 14:01:49 -04:00
|
|
|
[[match-phrase-prefix-query-ex-request]]
|
|
|
|
==== Example request
|
|
|
|
|
|
|
|
The following search returns documents that contain phrases beginning with
|
|
|
|
`quick brown f` in the `message` field.
|
|
|
|
|
|
|
|
This search would match a `message` value of `quick brown fox` or `two quick
|
|
|
|
brown ferrets` but not `the fox is quick and brown`.
|
2016-04-05 08:16:43 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2016-04-05 08:16:43 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"match_phrase_prefix" : {
|
|
|
|
"message" : {
|
2019-08-06 14:01:49 -04:00
|
|
|
"query" : "quick brown f"
|
2016-05-24 05:58:43 -04:00
|
|
|
}
|
2016-04-05 08:16:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2016-04-22 06:45:12 -04:00
|
|
|
|
|
|
|
|
2019-08-06 14:01:49 -04:00
|
|
|
[[match-phrase-prefix-top-level-params]]
|
|
|
|
==== Top-level parameters for `match_phrase_prefix`
|
|
|
|
`<field>`::
|
|
|
|
(Required, object) Field you wish to search.
|
2016-04-22 06:45:12 -04:00
|
|
|
|
2019-08-06 14:01:49 -04:00
|
|
|
[[match-phrase-prefix-field-params]]
|
|
|
|
==== Parameters for `<field>`
|
|
|
|
`query`::
|
|
|
|
+
|
|
|
|
--
|
|
|
|
(Required, string) Text you wish to find in the provided `<field>`.
|
|
|
|
|
|
|
|
The `match_phrase_prefix` query <<analysis,analyzes>> any provided text into
|
|
|
|
tokens before performing a search. The last term of this text is treated as a
|
|
|
|
<<query-dsl-prefix-query,prefix>>, matching any words that begin with that term.
|
|
|
|
--
|
|
|
|
|
|
|
|
`analyzer`::
|
|
|
|
(Optional, string) <<analysis,Analyzer>> used to convert text in the `query`
|
|
|
|
value into tokens. Defaults to the <<specify-index-time-analyzer,index-time
|
|
|
|
analyzer>> mapped for the `<field>`. If no analyzer is mapped, the index's
|
|
|
|
default analyzer is used.
|
|
|
|
|
|
|
|
`max_expansions`::
|
|
|
|
(Optional, integer) Maximum number of terms to which the last provided term of
|
|
|
|
the `query` value will expand. Defaults to `50`.
|
|
|
|
|
|
|
|
`slop`::
|
|
|
|
(Optional, integer) Maximum number of positions allowed between matching tokens.
|
|
|
|
Defaults to `0`. Transposed terms have a slop of `2`.
|
|
|
|
|
|
|
|
`zero_terms_query`::
|
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, string) Indicates whether no documents are returned if the `analyzer`
|
|
|
|
removes all tokens, such as when using a `stop` filter. Valid values are:
|
|
|
|
|
|
|
|
`none` (Default)::
|
|
|
|
No documents are returned if the `analyzer` removes all tokens.
|
|
|
|
|
|
|
|
`all`::
|
|
|
|
Returns all documents, similar to a <<query-dsl-match-all-query,`match_all`>>
|
|
|
|
query.
|
|
|
|
--
|
|
|
|
|
|
|
|
|
|
|
|
[[match-phrase-prefix-query-notes]]
|
|
|
|
==== Notes
|
|
|
|
|
|
|
|
[[match-phrase-prefix-autocomplete]]
|
|
|
|
===== Using the match phrase prefix query for search autocompletion
|
|
|
|
While easy to set up, using the `match_phrase_prefix` query for search
|
|
|
|
autocompletion can sometimes produce confusing results.
|
|
|
|
|
|
|
|
For example, consider the query string `quick brown f`. This query works by
|
|
|
|
creating a phrase query out of `quick` and `brown` (i.e. the term `quick` must
|
|
|
|
exist and must be followed by the term `brown`). Then it looks at the sorted
|
|
|
|
term dictionary to find the first 50 terms that begin with `f`, and adds these
|
|
|
|
terms to the phrase query.
|
2016-04-22 06:45:12 -04:00
|
|
|
|
|
|
|
The problem is that the first 50 terms may not include the term `fox` so the
|
2019-08-06 14:01:49 -04:00
|
|
|
phrase `quick brown fox` will not be found. This usually isn't a problem as
|
2016-04-22 06:45:12 -04:00
|
|
|
the user will continue to type more letters until the word they are looking
|
|
|
|
for appears.
|
|
|
|
|
|
|
|
For better solutions for _search-as-you-type_ see the
|
2019-07-19 09:16:35 -04:00
|
|
|
<<completion-suggester,completion suggester>> and
|
2019-08-06 14:01:49 -04:00
|
|
|
the <<search-as-you-type,`search_as_you_type` field type>>.
|