Prefix Query
前缀查询
Returns documents that contain a specific prefix in a provided field.
返回在指定字段中包含特定前缀的文档。
Example request
The following search returns documents where the user
field contains a term that begins with ki
.
下面搜索返回user
字段中包含ki
开头的词条的文档。
GET /_search
{
"query": {
"prefix": {
"user": {
"value": "ki"
}
}
}
}
Copy as cURLView in Console
Top-level parameters for prefix
-
field
(Required, object) Field you wish to search.
(必填,对象)您要搜索的字段。
Parameters for field
field
的参数
-
value
(Required, string) Beginning characters of terms you wish to find in the provided
field
.(必需,字符串)您希望在
field
中找到的词条开头字符。 -
rewrite
(Optional, string) Method used to rewrite the query. For valid values and more information, see the
rewrite
parameter.(可选,字符串)用于重写查询的方法。其有效值和更多信息,请参阅
rewrite
参数。
Notes
笔记
Short request example
简化请求示例
You can simplify the prefix
query syntax by combining the field
and value
parameters. For example:
您可以通过组合field
和 value
参数来简化prefix
查询的语法。例如:
GET /_search
{
"query": {
"prefix" : { "user" : "ki" }
}
}
Copy as cURLView in Console
Speed up prefix queries
加速前缀查询
You can speed up prefix queries using the index_prefixes
mapping parameter. If enabled, Elasticsearch indexes prefixes between 2 and 5 characters in a separate field. This lets Elasticsearch run prefix queries more efficiently at the cost of a larger index.
您可以使用mapping设置中的index_prefixes
参数来加快前缀查询的速度。如果启用该参数,Elasticsearch只会为单独的字段索引2到5个字符的前缀。这使Elasticsearch在较大的索引上更有效率执行前缀查询,从而减少成本。