案例
对 汽车改装鲨鱼鳍 这句进行分词
GET /_analyze
{
"analyzer": "ik_smart",
"text": ["汽车改装鲨鱼鳍"]
}
结果如下:
{
"tokens" : [
{
"token" : "汽车",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "改装",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "鲨",
"start_offset" : 4,
"end_offset" : 5,
"type" : "CN_CHAR",
"position" : 2
},
{
"token" : "鱼鳍",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 3
}
]
}
可见,鲨鱼鳍被分成了鲨、鱼鳍,现在我们需要鲨鱼鳍这三个字不要拆分,就得添加自定义词汇。
步骤
进入IK配置目录(我这边是docker环境,步骤基本一样)
docker exec -it es /bin/bash
cd plugins/ik/config/
创建自定义分词文件
touch myDict.dic
vi myDict.dic
加上鲨鱼鳍这三个字,保存退出
让IK分词器识别自定义文件
vi IKAnalyzer.cfg.xml
在这里加上之前创建的分词文件名,然后保存退出
重启ElasticSearch服务,再次测试,结果如下
{
"tokens" : [
{
"token" : "汽车",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "改装",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "鲨鱼鳍",
"start_offset" : 4,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 2
}
]
}
这样就说明配置生效了。