什么是分词器?
把一段文字划分成一个个的 关键字,在搜索时,会把数据库中或者索引库中的数据进行分词,然后进行匹配操作
默认分词器
es默认的分词器,对中文支持的不好,会将 每个汉字看成一个词,
比如:中华人民共和国国歌
会被分为 中
、华
、人
、民
、共
、和
、国
、国
、歌
,这显然不符合中文分词
get _analyze
{
"text":"中华人民共和国国歌"
}
执行结果:
{
"tokens" : [
{
"token" : "中",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "华",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "人",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "民",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
},
{
"token" : "共",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<IDEOGRAPHIC>",
"position" : 4
},
{
"token" : "和",
"start_offset" : 5,
"end_offset" : 6,
"type" : "<IDEOGRAPHIC>",
"position" : 5
},
{
"token" : "国",
"start_offset" : 6,
"end_offset" : 7,
"type" : "<IDEOGRAPHIC>",
"position" : 6
},
{
"token" : "国",
"start_offset" : 7,
"end_offset" : 8,
"type" : "<IDEOGRAPHIC>",
"position" : 7
},
{
"token" : "歌",
"start_offset" : 8,
"end_offset" : 9,
"type" : "<IDEOGRAPHIC>",
"position" : 8
}
]
}
不支持中文 match 全文检索
这里为了演示,把 技入
当成一个词,但 ES 会查询出所有包含 技
、入
的标题,ES 不会把 技入
当成一个词
GET /toutiao_v1/_search
{
"query":{
"match":{
"title":"技入"
}
}
}
会查询出所有包含 技
、入
的标题
分析原因
对 技入
分词,如下:
GET _analyze
{
"query":{
"match":{
"title":"技入"
}
}
}
分成了两个字
解决
所以需要安装 中文分词器 IK 来解决这个问题
IK分词器
IK分词器在是一款 基于 词典 和 规则 的 中文分词器
提供了两种分词策略:ik_smart 和 ik_max_word
官网