elasticsearch中文分词器详解(九)

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 1.es安装中文分词器官网:https://github.com/medcl/elasticsearch-analysis-ik1.1.安装中文分词器安装中文分词器的要求:​ 1.分词器的版本要与es的版本一直​ 2.所有es节点都需要安装中文分词器​ 3.安装完分词器需要重启

1.es安装中文分词

官网:https://github.com/medcl/elasticsearch-analysis-ik

1.1.安装中文分词器

安装中文分词器的要求:

1.分词器的版本要与es的版本一直

2.所有es节点都需要安装中文分词器

3.安装完分词器需要重启

1.在线安装
[root@elasticsearch ~/soft]# cd /usr/share/elasticsearch/bin/
[root@elasticsearch /usr/share/elasticsearch/bin]# ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.6.0/elasticsearch-analysis-ik-6.6.0.zip
2.离线安装
[root@elasticsearch ~/soft]# cd /usr/share/elasticsearch/bin/
[root@elasticsearch /usr/share/elasticsearch/bin]# cd /usr/share/elasticsearch/plugins/
[root@elasticsearch /usr/share/elasticsearch/plugins]# mkdir ik
[root@elasticsearch /usr/share/elasticsearch/plugins]# cd ik
将分词器的包扔到服务器上
[root@elasticsearch /usr/share/elasticsearch/plugins/ik]# unzip elasticsearch-analysis-ik-6.6.0.zip 
[root@elasticsearch /usr/share/elasticsearch/plugins/ik]# rm -rf elasticsearch-analysis-ik-6.6.0.zip
3.任何方式安装的分词器都需要重启
[root@elasticsearch ~]# systemctl restart elasticsearch
另一种安装中文分词器方法
[root@elasticsearch ~]# /usr/share/elasticsearch/bin/elasticsearch-plugin install  file:///root/elasticsearch-analysis-ik-6.6.0.zip 

重启的时候在日志里会显示导入了分词器插件

1.2.开启kibana的api查询功能

开启kibana的api查询功能之后就可以在kibana上执行curl交互式时传输的一些指令,并且kibana会把我们的curl命令自动转换成kibana自己的命令类型

在这里就可以执行各种curl命令,当curl命令粘贴到这里时自动回变成kibana自己的格式,可以全选中一并执行,也可以单个执行,并且即使刷新页面,里面的命令也不会丢失

2.没有使用中文分词器查询中文高亮词显示的效果

来看一下索引没有设置中文分词器之前查询包含某个汉字显示高亮的效果

2.1.建一个xinwen索引并插入几条中文数据

在xinwen索引中插入几条中文数据

curl -XPOST http://localhost:9200/xinwen/create/1 -H 'Content-Type:application/json' -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -XPOST http://localhost:9200/xinwen/create/2 -H 'Content-Type:application/json' -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -XPOST http://localhost:9200/xinwen/create/3 -H 'Content-Type:application/json' -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'
curl -XPOST http://localhost:9200/xinwen/create/4 -H 'Content-Type:application/json' -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}

直接将curl命令复制到kibana的Dev tools里执行比较方便

2.2.查看es-head是否能查到数据

已经可以查到数据

2.3.查询包含中国这个词并显示高亮

查询一下包含中国这两个汉字并显示高亮,在没有使用中文分词器之前,es只是按字去搜索,也就是我们查询中国这个词,只要数据中包含"中"和"国"这两个字任意一个都会被显示出来

curl -XPOST http://localhost:9200/xinwen/_search  -H 'Content-Type:application/json' -d'
{
    "query" : { "match" : { "content" : "中国" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],        #前--高亮
        "post_tags" : ["</tag1>", "</tag2>"],         #后---高亮,前后高亮是指只把关键字高亮
        "fields" : {
            "content" : {}
        }
    }
}

根据输出的结果可以明显的看出,不使用中文分词器之前,只是按某一个字去搜索,而不是按词去搜索,哪个数据出现我们要搜索的词位置最靠前就优先显示

3.使用中文分词器之后查询中文高亮词显示效果

本次验证一下使用了中文分词器之后的明显变化

注意:使索引支持中文分词器的时,要先把索引创建出来,然后在创建索引映射,最后在插入数据,否则索引映射会创建失败

3.1.创建xinwen2索引

curl -XPUT http://localhost:9200/index

使用kibana的Dev tools非常方便,创建一个索引直接就是PUT /索引名

3.2.创建xinwen2索引映射

这一步主要是在索引中增加中文分词器

ik_max_word 和 ik_smart 什么区别?

ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合,适合 Term Query;


ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”,适合 Phrase 查询。


一般还使用ik_max_word这个比较多,毕竟词多就是强

curl -XPOST http://localhost:9200/xinwen2/fulltext/_mapping -H 'Content-Type:application/json' -d'
{
        "properties": {
            "content": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word"
            }
        }
}'

3.3.在xinwen2中插入中文数据

curl -XPOST http://localhost:9200/xinwen2/fulltext/1 -H 'Content-Type:application/json' -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -XPOST http://localhost:9200/xinwen2/fulltext/2 -H 'Content-Type:application/json' -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -XPOST http://localhost:9200/xinwen2/fulltext/3 -H 'Content-Type:application/json' -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'
curl -XPOST http://localhost:9200/xinwen2/fulltext/4 -H 'Content-Type:application/json' -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}

3.4.在es-head上查看所有是否已经支持中文分词器

点击索引—信息—索引信息

3.5.查询包含中国这个词并显示高亮

curl -XPOST http://localhost:9200/xinwen2/_search  -H 'Content-Type:application/json' -d'
{
    "query" : { "match" : { "content" : "中国" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],        
        "post_tags" : ["</tag1>", "</tag2>"],       
        "fields" : {
            "content" : {}
        }
    }
}'

执行完可以看到这次已经是以词进行搜索了,上次没有使用分词器之前查询是按字搜索的,使用了分词器之后就是按词进行搜索

4.使用中文分词器前后对比结论

没有使用中文分词器之前搜索"中国"这个词,搜索出来的结果就是只要包含中、国任意一个字都会被搜索出来,显然不是我们想要的需求,并且数据展示杂乱

使用了中文分词器之后,他会把我们搜索的中国当成一个词,只有数据中包含中国这个词才会被显示

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
5月前
|
存储 人工智能 自然语言处理
ElasticSearch实战指南必知必会:安装分词器、高级查询、打分机制
ElasticSearch实战指南必知必会:安装分词器、高级查询、打分机制
ElasticSearch实战指南必知必会:安装分词器、高级查询、打分机制
|
2月前
|
自然语言处理
Elasticsearch+IK+pinyin自定义分词器
Elasticsearch+IK+pinyin自定义分词器
29 0
|
8月前
|
存储 自然语言处理 索引
【Elasticsearch】学好Elasticsearch系列-分词器 1
【Elasticsearch】学好Elasticsearch系列-分词器
76 0
|
3月前
|
自然语言处理 数据可视化 算法
史上最详细Docker安装Elasticsearch、ik分词器、可视化工具,每一步都带有步骤图!!!
史上最详细Docker安装Elasticsearch、ik分词器、可视化工具,每一步都带有步骤图!!!
360 0
史上最详细Docker安装Elasticsearch、ik分词器、可视化工具,每一步都带有步骤图!!!
|
4月前
|
自然语言处理 搜索推荐 算法
数据库-Elasticsearch进阶学习笔记(分片、映射、分词器、即时搜索、全文搜索等)
数据库-Elasticsearch进阶学习笔记(分片、映射、分词器、即时搜索、全文搜索等)
142 0
|
7月前
|
自然语言处理 Java Windows
Windows使用elasticsearch+Kibana+ik分词器
Windows使用elasticsearch+Kibana+ik分词器
72 0
|
8月前
|
自然语言处理 关系型数据库 MySQL
|
8月前
|
自然语言处理 关系型数据库 数据库连接
【Elasticsearch】学好Elasticsearch系列-分词器 2
【Elasticsearch】学好Elasticsearch系列-分词器
149 0
【Elasticsearch】学好Elasticsearch系列-分词器  2
|
8月前
|
存储 JSON 自然语言处理
|
8月前
|
存储 自然语言处理 搜索推荐
Elasticsearch插件管理(ik分词器、附件文本抽取插件)
Elasticsearch插件管理(ik分词器、附件文本抽取插件)
189 0

热门文章

最新文章