IK分词器

分词:即把一段中文或者别的划分成一个个的关键字,我们在搜索时候会把自己的信息进行分词,会把 数据库中或者索引库中的数据进行分词,然后进行一个匹配操作,默认的中文分词是将每个字看成一个 词,比如 “我爱狂神” 会被分为”我”,”爱”,”狂”,”神”,这显然是不符合要求的,所以我们需要安装中文分词器ik来解决这个问题。

IK提供了两个分词算法:ik_smart 和 ik_max_word,其中 ik_smart 为最少切分,ik_max_word为最细粒度划分!一会我们测试!

ik_smart : 粗粒度分词,从第一个开始分割,分离词汇,不会阿紫回来再分割一次,就只分割一次!

ik_max_word : 细粒度分词,会穷尽一个语句中所有分词可能,测试!

ik_smart

1
2
3
4
5
GET _analyze
{
"analyzer": "ik_smart"
, "text": ["曲靖师范学院信息工程学院"]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
"tokens" : [
{
"token" : "曲靖",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "师范学院",
"start_offset" : 2,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "信息",
"start_offset" : 6,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "工程学院",
"start_offset" : 8,
"end_offset" : 12,
"type" : "CN_WORD",
"position" : 3
}
]
}

ik_max_word

1
2
3
4
5
GET _analyze
{
"analyzer": "ik_max_word"
, "text": ["曲靖师范学院信息工程学院"]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{
"tokens" : [
{
"token" : "曲靖",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "师范学院",
"start_offset" : 2,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "师范",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "学院",
"start_offset" : 4,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "信息",
"start_offset" : 6,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "工程学院",
"start_offset" : 8,
"end_offset" : 12,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "工程学",
"start_offset" : 8,
"end_offset" : 11,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "工程",
"start_offset" : 8,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 7
},
{
"token" : "学院",
"start_offset" : 10,
"end_offset" : 12,
"type" : "CN_WORD",
"position" : 8
}
]
}

smart模式只会分割一次。

自定义词汇

步骤:

(1) 进入elasticsearch/plugins/ik/config目录

(2) 新建一个my.dic文件,编辑内容:

(3) 修改IKAnalyzer.cfg.xml(在ik/config目录下)

使用以后变化:

我在字典里面添加了一个==信息工程学院==

1
2
3
4
5
GET _analyze
{
"analyzer": "ik_smart"
, "text": ["曲靖师范学院信息工程学院"]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"tokens" : [
{
"token" : "曲靖",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "师范学院",
"start_offset" : 2,
"end_offset" : 6,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "信息工程学院",
"start_offset" : 6,
"end_offset" : 12,
"type" : "CN_WORD",
"position" : 2
}
]
}

变化在于:他把我们的原来拆分成俩个的信息和工程学院变成信息工程学院了