尝试这里描述的解决方案:Exact Substring Searches in ElasticSearch
{
"mappings": {
"my_type": {
"index_analyzer":"index_ngram",
"search_analyzer":"search_ngram"
}
},
"settings": {
"analysis": {
"filter": {
"ngram_filter": {
"type": "ngram",
"min_gram": 3,
"max_gram": 8
}
},
"analyzer": {
"index_ngram": {
"type": "custom",
"tokenizer": "keyword",
"filter": [ "ngram_filter", "lowercase" ]
},
"search_ngram": {
"type": "custom",
"tokenizer": "keyword",
"filter": "lowercase"
}
}
}
}
}
为了解决磁盘使用问题和搜索词过长问题,使用了短 8 个字符的长ngram(配置为:“max_gram”:8)。要搜索超过 8 个字符的术语,请将搜索转换为布尔 AND 查询,以查找该字符串中每个不同的 8 个字符子字符串。例如,如果用户搜索large yard(10 个字符的字符串),则搜索结果为:
“arge ya AND arge yar AND rge yard .