文档简介:
分词器说明
在Elasticsearch搜索引擎中使用分词词库时,有两种用法:
- ik_max_word:会将文本做最细粒度的拆分,比如会将“昨夜西风吹折千林梢”拆分为“昨夜西风,昨夜,西风,吹折千林梢,吹折,千林梢,千,林,折千林,千林,吹”,会穷尽各种可能的组合。
- ik_smart:会做最粗粒度的拆分,比如会将“昨夜西风吹折千林梢”拆分为“昨夜西风,吹折千林梢”。
示例指导
假设,用户将“智能手机”设置为主词,“是”设置为停词。然后,使用Elasticsearch引擎对“智能手机是很好用”文本内容进行分词。
1.在云搜索服务管理控制台,选择需要使用的集群,单击集群名称进入集群“基本信息”页面。
2.准备主词库文件、停词词库文件和同义词词库文件,主词库文件中包含“智能手机”词语,停词文件中包含“是”词语,同义词词库文件中包含一组同义词:“开心”和“高兴”。然后将UTF-8无BOM格式编码的文件存储到对应OBS路径下,如“obs-b8ed”桶。
由于系统默认词库的停用词包含了“是”、“的”等常用词,此处您也可以不上传示例中的停用词。
3.参考配置自定义词库步骤描述,选择对应OBS路径,并选择对应的“主词库文件”、“停词词库文件”和“同义词词库文件”。单击“保存”,等待词库配置信息生效。
4.待词库状态变更为“成功”后,进入集群管理页面,在对应的集群列,单击“操作”列的“Kibana”接入集群。
5.在Dev Tools的Console中,输入如下代码,并单击运行,您可以在右侧查看分词效果。
- 使用ik_smart分词器,对“智能手机是很好用”文本内容进行分词。
示例代码:
POST /_analyze
{
"analyzer":"ik_smart",
"text":"智能手机是很好用"
}复制
运行结束后,查看分词效果:
{
"tokens": [
{
"token": "智能手机",
"start_offset": 0,
"end_offset": 4,
"type": "CN_WORD",
"position": 0
},
{
"token": "很好用",
"start_offset": 5,
"end_offset": 8,
"type": "CN_WORD",
"position": 1
}
]
}复制
- 使用ik_max_word分词器,对“智能手机是很好用”文本内容进行分词。
示例代码:
POST /_analyze
{
"analyzer":"ik_max_word",
"text":"智能手机是很好用"
}复制
运行结束后,查看分词效果:
{
"tokens" : [
{
"token" : "智能手机",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "智能",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "智",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "能手",
"start_offset" : 1,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "手机",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "机",
"start_offset" : 3,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "很好用",
"start_offset" : 5,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "很好",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 7
},
{
"token" : "好用",
"start_offset" : 6,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 8
},
{
"token" : "用",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_WORD",
"position" : 9
}
]
}复制
6.参考如下示例,创建索引、导入数据、并使用关键字进行搜索和查看搜索结果。
a. 创建索引“book”。如下所示,示例中“analyzer”和“search_analyzer”选择“ik_max_word”,您也可以选择“ik_smart”分词器。
(7.x之前版本)
PUT /book
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
},
"mappings": {
"type1": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
}复制
(7.x及之后版本)
PUT /book
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}复制
b. 导入数据。将文本信息导入至“book”索引中。
(7.x之前版本)
PUT /book/type1/1
{
"content":"智能手机是很好用"
}复制
(7.x及之后版本)
PUT /book/_doc/1
{
"content":"智能手机是很好用"
}复制
c. 使用关键字“智能手机”进行搜索,并查看搜索结果。
(7.x之前版本)
GET /book/type1/_search
{
"query": {
"match": {
"content": "智能手机"
}
}
}复制
(7.x及之后版本)
GET /book/_doc/_search
{
"query": {
"match": {
"content": "智能手机"
}
}
}复制
搜索结果:
(7.x之前版本)
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.7260926,
"hits" : [
{
"_index" : "book",
"_type" : "type1",
"_id" : "1",
"_score" : 1.7260926,
"_source" : {
"content" : "智能手机是很好用"
}
}
]
}
}复制
(7.x及之后版本)
{
"took" : 16,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.7260926,
"hits" : [
{
"_index" : "book",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.7260926,
"_source" : {
"content" : "智能手机是很好用"
}
}
]
}
}复制
7.参考如下示例,创建索引、导入数据、并使用同义词进行搜索查看搜索结果。
a. 创建索引。
(7.x之前版本如下所示)
PUT myindex
{
"settings": {
"analysis": {
"filter": {
"my_synonym": {
"type": "dynamic_synonym"
}
},
"analyzer": {
"ik_synonym": {
"filter": [
"my_synonym"
],
"type": "custom",
"tokenizer": "ik_smart"
}
}
}
},
"mappings": {
"mytype" :{
"properties": {
"desc": {
"type": "text",
"analyzer": "ik_synonym"
}
}
}
}
}复制
(7.x及之后版本如下所示)
PUT myindex
{
"settings": {
"analysis": {
"filter": {
"my_synonym": {
"type": "dynamic_synonym"
}
},
"analyzer": {
"ik_synonym": {
"filter": [
"my_synonym"
],
"type": "custom",
"tokenizer": "ik_smart"
}
}
}
},
"mappings": {
"properties": {
"desc": {
"type": "text",
"analyzer": "ik_synonym"
}
}
}
}复制
b. 导入数据。将文本信息导入至“myindex”索引中。
(7.x之前版本)
PUT /myindex/mytype/1
{
"desc": "我今天获奖了我很开心"
}复制
(7.x及之后版本)
PUT /myindex/_doc/1
{
"desc": "我今天获奖了我很开心"
}复制
c. 使用同义词“高兴”进行搜索,并查看搜索结果。
执行如下命令搜索“高兴”。
GET /myindex/_search
{
"query": {
"match": {
"desc": "高兴"
}
}
}复制
搜索结果:
(7.x之前版本)
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.41048482,
"hits": [
{
"_index": "myindex",
"_type": "mytype",
"_id": "1",
"_score": 0.41048482,
"_source": {
"desc": "我今天获奖了我很开心"
}
}
]
}
}复制
(7.x及之后版本)
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.1519955, "hits" : [ { "_index" : "myindex", "_type" : "_doc", "_id" : "1", "_score" : 0.1519955, "_source" : { "desc" : "我今天获奖了我很开心" } } ] } }