Skip to content

[DOCS] Rewrite prefix query docs #41955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 29, 2019
69 changes: 51 additions & 18 deletions docs/reference/query-dsl/prefix-query.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,66 @@
<titleabbrev>Prefix</titleabbrev>
++++

Matches documents that have fields containing terms with a specified
prefix (*not analyzed*). The prefix query maps to Lucene `PrefixQuery`.
The following matches documents where the user field contains a term
that starts with `ki`:
Returns documents that contain a specific prefix in a provided field.

[[prefix-query-ex-request]]
==== Example request

The following search returns documents where the `user` field contains a term
that begins with `ki`.

[source,js]
--------------------------------------------------
----
GET /_search
{ "query": {
"prefix" : { "user" : "ki" }
}
{
"query": {
"prefix": {
"user": {
"value": "ki"
}
}
}
}
--------------------------------------------------
----
// CONSOLE

A boost can also be associated with the query:
[[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>>.

[[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:

[source,js]
--------------------------------------------------
----
GET /_search
{ "query": {
"prefix" : { "user" : { "value" : "ki", "boost" : 2.0 } }
}
{
"query": {
"prefix" : { "user" : "ki" }
}
}
--------------------------------------------------
----
// CONSOLE

This multi term query allows you to control how it gets rewritten using the
<<query-dsl-multi-term-rewrite,rewrite>>
parameter.
[[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
efficiently at the cost of a larger index.