AliNLP分词插件(英文名为analysis-aliws)是阿里云Elasticsearch自带的一个系统默认插件。通过该插件,您可以在阿里云Elasticsearch中集成对应的分析器和分词器,完成文档的分析和检索。您也可以使用analysis-aliws插件的词库配置功能,通过词典文件热更新自定义词库配置(替换默认词典)。本文介绍如何使用analysis-aliws插件。
背景信息
安装了analysis-aliws插件后,阿里云Elasticsearch默认会集成以下分析器和分词器:
- 分析器:aliws(不会截取虚词、虚词短语、符号)
- 分词器:aliws_tokenizer
您可以使用上述分析器和分词器查询文档,也可以通过词库配置功能自定义更新分词词库。详细信息,请参见下文的查询文档和配置词库。当您在使用analysis-aliws插件得到的结果不符合预期时,可通过下文的测试分析器和测试分词器排查调试。
说明 analysis-aliws插件分词完成后,还会经过filter处理,包括:去词根filter、LowerCaseFilter、PorterStemFilter和StopFilter。如果您的自定义分析器上也需要使用这些filter,可在自定义分析器中加入analysis-aliws插件的分词器aliws_tokenizer,示例如下。
PUT my-index-000001
{
"settings": {
"analysis": {
"analyzer": {
"my_custom_analyzer": {
"type": "custom",
"tokenizer": "aliws_tokenizer"
}
}
}
}
}
前提条件
已安装analysis-aliws插件(默认未安装)。
如果还未安装,请先安装analysis-aliws插件。安装前需要确保实例的内存大小为4 GB及以上(生产环境中要求最低为8 GB)。具体安装步骤,请参见安装或卸载系统默认插件。
注意
- 5.x版本的实例不支持analysis-aliws插件。
- 如果实例的内存大小不满足要求,需要先升级。具体操作,请参见升配集群。
使用限制
5.x版本实例不支持analysis-aliws插件。
查询文档
配置词库
analysis-aliws插件支持词库配置,即上传自定义的词典文件aliws_ext_dict.txt。上传后节点能自动加载词典文件,实现词典的热更新操作(不会触发集群重启)。
注意
- analysis-aliws插件安装后,系统不会自带默认词典文件aliws_ext_dict.txt,需要您手动上传。
- 在配置词库前,您需要先准备自定义的词典文件,并重命名为aliws_ext_dict.txt。
测试分析器
执行如下命令,测试aliws分析器。
GET _analyze
{
"text": "I like go to school.",
"analyzer": "aliws"
}
预期结果如下。
{
"tokens" : [
{
"token" : "i",
"start_offset" : 0,
"end_offset" : 1,
"type" : "word",
"position" : 0
},
{
"token" : "like",
"start_offset" : 2,
"end_offset" : 6,
"type" : "word",
"position" : 2
},
{
"token" : "go",
"start_offset" : 7,
"end_offset" : 9,
"type" : "word",
"position" : 4
},
{
"token" : "school",
"start_offset" : 13,
"end_offset" : 19,
"type" : "word",
"position" : 8
}
]
}
测试分词器
执行如下命令,测试aliws_tokenizer分词器。
GET _analyze
{
"text": "I like go to school.",
"tokenizer": "aliws_tokenizer"
}
预期结果如下。
{
"tokens" : [
{
"token" : "I",
"start_offset" : 0,
"end_offset" : 1,
"type" : "word",
"position" : 0
},
{
"token" : " ",
"start_offset" : 1,
"end_offset" : 2,
"type" : "word",
"position" : 1
},
{
"token" : "like",
"start_offset" : 2,
"end_offset" : 6,
"type" : "word",
"position" : 2
},
{
"token" : " ",
"start_offset" : 6,
"end_offset" : 7,
"type" : "word",
"position" : 3
},
{
"token" : "go",
"start_offset" : 7,
"end_offset" : 9,
"type" : "word",
"position" : 4
},
{
"token" : " ",
"start_offset" : 9,
"end_offset" : 10,
"type" : "word",
"position" : 5
},
{
"token" : "to",
"start_offset" : 10,
"end_offset" : 12,
"type" : "word",
"position" : 6
},
{
"token" : " ",
"start_offset" : 12,
"end_offset" : 13,
"type" : "word",
"position" : 7
},
{
"token" : "school",
"start_offset" : 13,
"end_offset" : 19,
"type" : "word",
"position" : 8
},
{
"token" : ".",
"start_offset" : 19,
"end_offset" : 20,
"type" : "word",
"position" : 9
}
]
}