16.index_prefixes 前缀搜索
- 提升prefix的查询速度。
- 搭配text类型
配置:
- min_chars:前缀最小长度,>0,默认2
- max_chars:前缀最大长度,<20,默认5
DELETE my-index-000001
PUT /my-index-000001
{
"mappings": {
"properties": {
"name": {
"type": "text",
"index_prefixes": {
"min_chars": 3,
"max_chars": 6
},
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
# 索引
POST /my-index-000001/_doc/1
{
"name" : "attractive girl"
}
POST /my-index-000001/_doc/2
{
"name" : "self-confidence attractiveness"
}
# 搜索attractive的前面几个单词at att attr,g
# 查询at:错误:query_shard_exception,failed to create query: Cannot invoke \"Object.hashCode()\" because \"this.rewriteMethod\" is null
# 查询att,attr:两条记录
# 查询attractive:两条记录
# attractiveness:一条记录
GET /my-index-000001/_search
{
"query" : {
"prefix" : {
"name" : {
"value" : "attr"
}
}
}
}
#不加index_prefixes 前缀默认:2-5
DELETE my-index-000001
PUT /my-index-000001
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
# 索引
POST /my-index-000001/_doc/1
{
"name" : "attractive girl"