Prefix query
editPrefix query
editReturns documents that contain a specific prefix in a provided field.
Example request
editThe following search returns documents where the user.id
field contains a term
that begins with ki
.
resp = client.search( query={ "prefix": { "user.id": { "value": "ki" } } }, ) print(resp)
response = client.search( body: { query: { prefix: { 'user.id' => { value: 'ki' } } } } ) puts response
const response = await client.search({ query: { prefix: { "user.id": { value: "ki", }, }, }, }); console.log(response);
GET /_search { "query": { "prefix": { "user.id": { "value": "ki" } } } }
Top-level parameters for prefix
edit-
<field>
- (Required, object) Field you wish to search.
Parameters for <field>
edit-
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
rewrite
parameter. -
case_insensitive
[7.10.0] Added in 7.10.0. - (Optional, Boolean) Allows ASCII case insensitive matching of the 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.
Notes
editShort request example
editYou can simplify the prefix
query syntax by combining the <field>
and
value
parameters. For example:
resp = client.search( query={ "prefix": { "user": "ki" } }, ) print(resp)
response = client.search( body: { query: { prefix: { user: 'ki' } } } ) puts response
const response = await client.search({ query: { prefix: { user: "ki", }, }, }); console.log(response);
GET /_search { "query": { "prefix" : { "user" : "ki" } } }
Speed up prefix queries
editYou can speed up prefix queries using the index_prefixes
mapping parameter. If enabled, Elasticsearch indexes prefixes in a separate field,
according to the configuration settings. This lets Elasticsearch run prefix queries
more efficiently at the cost of a larger index.
Allow expensive queries
editPrefix queries will not be executed if search.allow_expensive_queries
is set to false. However, if index_prefixes
are enabled, an optimised query is built which
is not considered slow, and will be executed in spite of this setting.