4
4
<titleabbrev>Match phrase prefix</titleabbrev>
5
5
++++
6
6
7
- The `match_phrase_prefix` is the same as `match_phrase`, except that it
8
- allows for prefix matches on the last term in the text. For example:
7
+ Returns documents that contain the words of a provided text, in the **same
8
+ order** as provided. The last term of the provided text is treated as a
9
+ <<query-dsl-prefix-query,prefix>>, matching any words that begin with that term.
9
10
10
- [source,js]
11
- --------------------------------------------------
12
- GET /_search
13
- {
14
- "query": {
15
- "match_phrase_prefix" : {
16
- "message" : "quick brown f"
17
- }
18
- }
19
- }
20
- --------------------------------------------------
21
- // CONSOLE
22
11
23
- It accepts the same parameters as the phrase type. In addition, it also
24
- accepts a `max_expansions` parameter (default `50`) that can control to how
25
- many suffixes the last term will be expanded. It is highly recommended to set
26
- it to an acceptable value to control the execution time of the query. For
27
- example:
12
+ [[match-phrase-prefix-query-ex-request]]
13
+ ==== Example request
14
+
15
+ The following search returns documents that contain phrases beginning with
16
+ `quick brown f` in the `message` field.
17
+
18
+ This search would match a `message` value of `quick brown fox` or `two quick
19
+ brown ferrets` but not `the fox is quick and brown`.
28
20
29
21
[source,js]
30
22
--------------------------------------------------
@@ -33,35 +25,81 @@ GET /_search
33
25
"query": {
34
26
"match_phrase_prefix" : {
35
27
"message" : {
36
- "query" : "quick brown f",
37
- "max_expansions" : 10
28
+ "query" : "quick brown f"
38
29
}
39
30
}
40
31
}
41
32
}
42
33
--------------------------------------------------
43
34
// CONSOLE
44
35
45
- [IMPORTANT]
46
- ===================================================
47
36
48
- The `match_phrase_prefix` query is a poor-man's autocomplete. It is very easy
49
- to use, which lets you get started quickly with _search-as-you-type_ but its
50
- results, which usually are good enough, can sometimes be confusing.
37
+ [[match-phrase-prefix-top-level-params]]
38
+ ==== Top-level parameters for `match_phrase_prefix`
39
+ `<field>`::
40
+ (Required, object) Field you wish to search.
41
+
42
+ [[match-phrase-prefix-field-params]]
43
+ ==== Parameters for `<field>`
44
+ `query`::
45
+ +
46
+ --
47
+ (Required, string) Text you wish to find in the provided `<field>`.
48
+
49
+ The `match_phrase_prefix` query <<analysis,analyzes>> any provided text into
50
+ tokens before performing a search. The last term of this text is treated as a
51
+ <<query-dsl-prefix-query,prefix>>, matching any words that begin with that term.
52
+ --
53
+
54
+ `analyzer`::
55
+ (Optional, string) <<analysis,Analyzer>> used to convert text in the `query`
56
+ value into tokens. Defaults to the <<specify-index-time-analyzer,index-time
57
+ analyzer>> mapped for the `<field>`. If no analyzer is mapped, the index's
58
+ default analyzer is used.
59
+
60
+ `max_expansions`::
61
+ (Optional, integer) Maximum number of terms to which the last provided term of
62
+ the `query` value will expand. Defaults to `50`.
63
+
64
+ `slop`::
65
+ (Optional, integer) Maximum number of positions allowed between matching tokens.
66
+ Defaults to `0`. Transposed terms have a slop of `2`.
67
+
68
+ `zero_terms_query`::
69
+ +
70
+ --
71
+ (Optional, string) Indicates whether no documents are returned if the `analyzer`
72
+ removes all tokens, such as when using a `stop` filter. Valid values are:
73
+
74
+ `none` (Default)::
75
+ No documents are returned if the `analyzer` removes all tokens.
76
+
77
+ `all`::
78
+ Returns all documents, similar to a <<query-dsl-match-all-query,`match_all`>>
79
+ query.
80
+ --
81
+
82
+
83
+ [[match-phrase-prefix-query-notes]]
84
+ ==== Notes
85
+
86
+ [[match-phrase-prefix-autocomplete]]
87
+ ===== Using the match phrase prefix query for search autocompletion
88
+ While easy to set up, using the `match_phrase_prefix` query for search
89
+ autocompletion can sometimes produce confusing results.
51
90
52
- Consider the query string `quick brown f`. This query works by creating a
53
- phrase query out of `quick` and `brown` (i.e. the term `quick` must exist and
54
- must be followed by the term `brown`). Then it looks at the sorted term
55
- dictionary to find the first 50 terms that begin with `f`, and
56
- adds these terms to the phrase query.
91
+ For example, consider the query string `quick brown f`. This query works by
92
+ creating a phrase query out of `quick` and `brown` (i.e. the term `quick` must
93
+ exist and must be followed by the term `brown`). Then it looks at the sorted
94
+ term dictionary to find the first 50 terms that begin with `f`, and adds these
95
+ terms to the phrase query.
57
96
58
97
The problem is that the first 50 terms may not include the term `fox` so the
59
- phrase `quick brown fox` will not be found. This usually isn't a problem as
98
+ phrase `quick brown fox` will not be found. This usually isn't a problem as
60
99
the user will continue to type more letters until the word they are looking
61
100
for appears.
62
101
63
102
For better solutions for _search-as-you-type_ see the
64
103
<<search-suggesters-completion,completion suggester>> and
65
104
{defguide}/_index_time_search_as_you_type.html[Index-Time Search-as-You-Type].
66
105
67
- ===================================================
0 commit comments