返回包含指定前缀的所有文档;简单来说,就是文档某个field
字段的前几个单词的前缀为value
,则满足匹配条件。
field
,想要搜索的字段
value
必选项,前缀
GET blak_new/_search
{
"query": {
"prefix": {
"addres": {
"value": "str"
}
}
}
}
/* 简化版 */
GET blak_new/_search
{
"query": {
"prefix" : { "addres" : "str" }
}
}
返回结果
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 389,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "blak_new",
"_type" : "_doc",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"account_number" : 6,
"balance" : 5686,
"firstname" : "Hattie",
"lastname" : "Bond",
"age" : 36,
"gender" : "M",
"address" : "671 Bristol Street",
"employer" : "Netagy",
"email" : "hattiebond@netagy.com",
"city" : "Dante",
"state" : "TN"
}
}
/* 省略其他结果 */
]
}
}
index_prefixes
参数,加快查询速度;如果使用,Elasticsearch 会在一个单独的字段中索引2到5个字符之间的前缀。这使得Elasticsearch 可以以更大的索引为代价,更有效的运行前缀索引。search.allow_expensive_queries
设置为false,则不会执行前缀查询。但是,如果index_prefixes
启用,则会构建一个优化的查询,该查询并不算慢,尽管有此设置也将执行该查询。