2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-prefix-query]]
|
2019-07-18 10:18:11 -04:00
|
|
|
=== Prefix query
|
|
|
|
++++
|
|
|
|
<titleabbrev>Prefix</titleabbrev>
|
|
|
|
++++
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-07-29 08:37:01 -04:00
|
|
|
Returns documents that contain a specific prefix in a provided field.
|
|
|
|
|
|
|
|
[[prefix-query-ex-request]]
|
|
|
|
==== Example request
|
|
|
|
|
2020-08-03 13:31:19 -04:00
|
|
|
The following search returns documents where the `user.id` field contains a term
|
2019-07-29 08:37:01 -04:00
|
|
|
that begins with `ki`.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2019-07-29 08:37:01 -04:00
|
|
|
----
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2019-07-29 08:37:01 -04:00
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"query": {
|
|
|
|
"prefix": {
|
2020-08-03 13:31:19 -04:00
|
|
|
"user.id": {
|
2020-07-21 15:49:58 -04:00
|
|
|
"value": "ki"
|
|
|
|
}
|
2019-07-29 08:37:01 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2019-07-29 08:37:01 -04:00
|
|
|
----
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-07-29 08:37:01 -04:00
|
|
|
[[prefix-query-top-level-params]]
|
|
|
|
==== Top-level parameters for `prefix`
|
|
|
|
`<field>`::
|
|
|
|
(Required, object) Field you wish to search.
|
|
|
|
|
|
|
|
[[prefix-query-field-params]]
|
|
|
|
==== Parameters for `<field>`
|
|
|
|
`value`::
|
|
|
|
(Required, string) Beginning characters of terms you wish to find in the
|
|
|
|
provided `<field>`.
|
|
|
|
|
|
|
|
`rewrite`::
|
|
|
|
(Optional, string) Method used to rewrite the query. For valid values and more
|
|
|
|
information, see the <<query-dsl-multi-term-rewrite, `rewrite` parameter>>.
|
|
|
|
|
2020-09-22 08:56:51 -04:00
|
|
|
`case_insensitive`::
|
|
|
|
(Optional, boolean) allows ASCII case insensitive matching of the
|
2020-10-21 08:33:19 -04:00
|
|
|
value with the indexed field values when set to true. Default is false which means
|
|
|
|
the case sensitivity of matching depends on the underlying field's mapping.
|
2020-09-22 08:56:51 -04:00
|
|
|
|
2019-07-29 08:37:01 -04:00
|
|
|
[[prefix-query-notes]]
|
|
|
|
==== Notes
|
|
|
|
|
|
|
|
[[prefix-query-short-ex]]
|
|
|
|
===== Short request example
|
|
|
|
You can simplify the `prefix` query syntax by combining the `<field>` and
|
|
|
|
`value` parameters. For example:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2019-07-29 08:37:01 -04:00
|
|
|
----
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2019-07-29 08:37:01 -04:00
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"query": {
|
|
|
|
"prefix" : { "user" : "ki" }
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2019-07-29 08:37:01 -04:00
|
|
|
----
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-07-29 08:37:01 -04:00
|
|
|
[[prefix-query-index-prefixes]]
|
|
|
|
===== Speed up prefix queries
|
|
|
|
You can speed up prefix queries using the <<index-prefixes,`index_prefixes`>>
|
|
|
|
mapping parameter. If enabled, {es} indexes prefixes between 2 and 5
|
|
|
|
characters in a separate field. This lets {es} run prefix queries more
|
2020-02-12 16:56:14 -05:00
|
|
|
efficiently at the cost of a larger index.
|
|
|
|
|
|
|
|
[[prefix-query-allow-expensive-queries]]
|
|
|
|
===== Allow expensive queries
|
|
|
|
Prefix queries will not be executed if <<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>>
|
|
|
|
is set to false. However, if <<index-prefixes, `index_prefixes`>> are enabled, an optimised query is built which
|
|
|
|
is not considered slow, and will be executed in spite of this setting.
|