不少时候,咱们须要在ElasticSearch中启用中文分词,本文这里简单的介绍一下方法。首先安装中文分词插件。这里使用的是 ik,也能够考虑其余插件(好比 smartcn)。html
$ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.2.0/elasti csearch-analysis-ik-7.2.0.zipgit
上面代码安装的是7.2.0版的插件,与 Elastic 7.2.0 配合使用。github
PS:其它插件命令:elasticsearch-plugin help 数据结构
接着,从新启动 Elastic,就会自动加载这个新安装的插件。app
而后,新建一个 Index,指定须要分词的字段。这一步根据数据结构而异,下面的命令只针对本文。基本上,凡是须要搜索的中文字段,都要单独设置一下。elasticsearch
PUT /accounts
{
"mappings": {
"person": {
"properties": {
"user": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
} ide
上面代码中,首先新建一个名称为accounts的 Index,里面有一个名称为person的 Type。person有三个字段。 ui
这三个字段都是中文,并且类型都是文本(text),因此须要指定中文分词器,不能使用默认的英文分词器。 spa
Elastic 的分词器称为 analyzer。咱们对每一个字段指定分词器。 插件
"user": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
上面代码中,analyzer是字段文本的分词器,search_analyzer是搜索词的分词器。ik_max_word分词器是插件ik提供的,能够对文本进行最大数量的分词。