POST test002/_analyze?pretty=true
{“text”:“我们是软件工程师”}
收到响应如下,可见每个汉字都被拆分成一个词了,这样会导致词项搜索时收不到我们想要的(例如用"我们"来搜索是没有结果的):
{
“tokens”: [
{
“token”: “我”,
“start_offset”: 0,
“end_offset”: 1,
“type”: “”,
“position”: 0
},
{
“token”: “们”,
“start_offset”: 1,
“end_offset”: 2,
“type”: “”,
“position”: 1
},
{
“token”: “是”,
“start_offset”: 2,
“end_offset”: 3,
“type”: “”,
“position”: 2
},
{
“token”: “软”,
“start_offset”: 3,
“end_offset”: 4,
“type”: “”,
“position”: 3
},
{
“token”: “件”,
“start_offset”: 4,
“end_offset”: 5,
“type”: “”,
“position”: 4
},
{
“token”: “工”,
“start_offset”: 5,
“end_offset”: 6,
“type”: “”,
“position”: 5
},
{
“token”: “程”,
“start_offset”: 6,
“end_offset”: 7,
“type”: “”,
“position”: 6
},
{
“token”: “师”,
“start_offset”: 7,
“end_offset”: 8,
“type”: “”,
“position”: 7
}
]
}
为了词项搜索能得到我们想要的结果,需要换一个分词器,理想的分词效果应该是"我们"、“是”、“软件”、“工程师”,ik分词器可以满足我们的要求,接下来开始实战;
注意事项-
下面的所有 *** 作都使用es账号来进行,不要用root账号;
-
编译ik分词器需要用到maven,如果您有docker,但是不想安装maven,可以参考《没有JDK和Maven,用Docker也能构建Maven工程》来编译工程;
-
登录ik分词器网站:https://github.com/medcl/elasticsearch-analysis-ik
-
按照网站提供的版本对应表,确认我们要使用的分词器版本,很遗憾写文章的时候还没有匹配elasticsearch-6.5.0的版本,那就用master吧,也就是下图中的红框版本:
- 如下图,点击下载zip文件:
- 将下载的zip包放到Ubuntu机器上,解压后是个名为elasticsearch-analysis-ik-master的文件夹,在此文件夹下执行以下命令,即可开始构建ik分词器工程:
mvn clean package -U -DskipTests
- 等待编译完成后,在target/release目录下会生产名为elasticsearch-analysis-ik-6.5.0.zip的文件,如下所示:
$ pwd
/usr/local/work/es/elasticsearch-analysis-ik-master
$ cd target/
$ ls
archive-tmp elasticsearch-analysis-ik-6.5.0.jar generated-sources maven-status
classes elasticsearch-analysis-ik-6.5.0-sources.jar maven-archiver releases
$ cd releases/
$ ls
elasticsearch-analysis-ik-6.5.0.zip
-
停止集群中所有机器的elasticsearch进程,在所有机器上做这些 *** 作:在elasticsearch的plugins目录下创建名为ik的目录,再将上面生成的elasticsearch-analysis-ik-6.5.0.zip文件复制到这个新创建的ik目录下;
-
在elasticsearch-analysis-ik-6.5.0.zip所在文件夹下,执行目录unzip elasticsearch-analysis-ik-6.5.0.zip进行解压;
-
确认elasticsearch-analysis-ik-6.5.0.zip已经复制到每个elasticsearch的plugins/ik目录下并解压后,将所有elasticsearch启动,可以发现控制台上会输出ik分词器被加载的信息,如下图所示:
![在这里插入图片描述](https:
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
//img-blog.csdnimg.cn/20190120113336189.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2JvbGluZ19jYXZhbHJ5,size_16,color_FFFFFF,t_70)
至此,ik分词器安装完成,来验证一下吧;
验证ik分词器- 在postman发起请求,在json中通过tokenizer指定分词器:
POST test002/_analyze?pretty=true
{
“text”:“我们是软件工程师”,
“tokenizer”:“ik_max_word”
}
这一次得到了分词的效果:
{
“tokens”: [
{
“token”: “我们”,
“start_offset”: 0,
“end_offset”: 2,
“type”: “CN_WORD”,
“position”: 0
},
{
“token”: “是”,
“start_offset”: 2,
“end_offset”: 3,
“type”: “CN_CHAR”,
“position”: 1
},
{
“token”: “软件工程”,
“start_offset”: 3,
“end_offset”: 7,
“type”: “CN_WORD”,
“position”: 2
},
{
“token”: “软件”,
“start_offset”: 3,
“end_offset”: 5,
“type”: “CN_WORD”,
“position”: 3
},
{
“token”: “工程师”,
“start_offset”: 5,
“end_offset”: 8,
“type”: “CN_WORD”,
“position”: 4
},
{
“token”: “工程”,
“start_offset”: 5,
“end_offset”: 7,
“type”: “CN_WORD”,
“position”: 5
},
{
“token”: “师”,
“start_offset”: 7,
“end_offset”: 8,
“type”: “CN_CHAR”,
“position”: 6
}
]
}
可见所有可能形成的词语都被分了出来,接下试试ik分词器的另一种分词方式ik_smart;
2. 使用ik_smart方式分词的请求如下:
shellshell
POST test002/_analyze?pretty=true
{
“text”:“我们是软件工程师”,
“tokenizer”:“ik_smart”
}
这一次得到了分词的效果:
{
“tokens”: [
{
“token”: “我们”,
“start_offset”: 0,
“end_offset”: 2,
“type”: “CN_WORD”,
“position”: 0
},
{
“token”: “是”,
“start_offset”: 2,
“end_offset”: 3,
“type”: “CN_CHAR”,
“position”: 1
},
{
“token”: “软件”,
“start_offset”: 3,
“end_offset”: 5,
“type”: “CN_WORD”,
“position”: 2
},
{
“token”: “工程师”,
“start_offset”: 5,
“end_offset”: 8,
“type”: “CN_WORD”,
“position”: 3
}
]
}
可见ik_smart的特点是将原句做拆分,不会因为各种组合出现部分的重复,以下是来自官方的解释:
ik_max_word 和 ik_smart 什么区别?
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。
验证搜索前面通过http请求验证了分词效果,现在通过搜索来验证分词效果;
- 通过静态mapping的方式创建索引,指定了分词器和分词方式:
PUT test003
{
“mappings”: {
“article”: {
“dynamic”: false,
“properties”: {
“title”: {
“type”: “keyword”
},
“content”: {
“type”: “text”,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)