老司机带你 elasticsearch 安装中文分词器

本贴最后更新于 1644 天前,其中的信息可能已经东海扬尘

发车

    为什么要在 elasticsearch 中要使用 ik 这样的中文分词呢,那是因为 es 提供的分词是英文分词,对于中文的分词就做的非常不好了,因此我们需要一个中文分词器来用于搜索和使用。今天我们就尝试安装下 IK 分词。

上车

1、去 github 下载对应的分词插件
https://github.com/medcl/elasticsearch-analysis-ik/releases
根据不同版本下载不同的分词插件

2、到 es 的 plugins 目录创建文件夹
cd your-es-root/plugins/ && mkdir ik

3、解压 ik 分词插件到 ik 文件夹

unzip elasticsearch-analysis-ik-6.4.3.zip

第二种安装方法

还有一种方式 直接通过 es 的命令进行安装,es 版本需要大于 5.5.1

./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip

安装好后 重启 es
会看到 加载了 ik 分词了
image.png

到站

IK 分词器简介

  • 1,Elasticsearch 中文分词我们采用 Ik 分词,ik 有两种分词模式,ik_max_word,和 ik_smart 模式;
    • ik_max_word 和 ik_smart 什么区别?

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

索引时,为了提供索引的覆盖范围,通常会采用 ik_max_word 分析器,会以最细粒度分词索引,搜索时为了提高搜索准确度,会采用 ik_smart 分析器,会以粗粒度分词

实测

创建 index

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

创建 mapping

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

添加几个数据

curl -XPOST http://localhost:9200/index/index/1 -H 'Content-Type:application/json' -d'  {"content":"美国留给伊拉克的是个烂摊子吗"}  '

curl -XPOST http://localhost:9200/index/index/3 -H 'Content-Type:application/json' -d'  {"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}  '

curl -XPOST http://localhost:9200/index/index/4 -H 'Content-Type:application/json' -d'  {"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}  '

进行查询

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

查看效果
im1age.png

  • Elasticsearch

    Elasticsearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful 接口。Elasticsearch 是用 Java 开发的,并作为 Apache 许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

    116 引用 • 99 回帖 • 267 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...