es 使用ik停词_es-ik自定义词库步骤
时间: 2023-12-24 07:45:17 浏览: 41
下面是使用 Elasticsearch 中的 IK 分词器进行停词和自定义词库的步骤:
1. 安装 Elasticsearch 和 IK 分词器插件。
2. 创建自定义词库文件,格式如下:
```
# 单个词语
词语1
词语2
...
# 带有词性的词语
词语1,词性1
词语2,词性2
...
```
注:词性可以不写,用逗号隔开。
3. 将自定义词库文件放置在 Elasticsearch 安装目录下的 `plugins/ik/config/` 目录下。
4. 修改 IK 分词器配置文件,指定停用词文件和自定义词库文件,示例如下:
```
{
"index": {
"analysis": {
"analyzer": {
"my_analyzer": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": [
"my_stopwords",
"my_synonyms"
]
}
},
"filter": {
"my_stopwords": {
"type": "stop",
"stopwords_path": "stopwords.txt"
},
"my_synonyms": {
"type": "synonym",
"synonyms_path": "synonyms.txt"
}
}
}
}
}
```
注:上面示例中使用了停用词文件和同义词文件,可以根据需要自行配置。
5. 创建索引并指定使用自定义分词器。
```
PUT /my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": [
"my_stopwords",
"my_synonyms"
]
}
},
"filter": {
"my_stopwords": {
"type": "stop",
"stopwords_path": "stopwords.txt"
},
"my_synonyms": {
"type": "synonym",
"synonyms_path": "synonyms.txt"
}
}
}
},
"mappings": {
"properties": {
"my_field": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
```
6. 测试分词效果。
可以使用以下命令测试分词效果:
```
GET /my_index/_analyze
{
"analyzer": "my_analyzer",
"text": "自定义分词器测试"
}
```
上述命令会返回分词结果,可以根据需要调整自定义词库和停用词文件。