Elastic ik插件配置热更新功能

时间:2023-03-09 20:16:18
Elastic ik插件配置热更新功能

ik github地址:https://github.com/medcl/elasticsearch-analysis-ik

官网说明:

热更新 IK 分词使用方法

目前该插件支持热更新 IK 分词,通过上文在 IK 配置文件中提到的如下配置

 	<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">location</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<entry key="remote_ext_stopwords">location</entry>

其中 location 是指一个 url,比如 http://yoursite.com/getCustomDict,该请求只需满足以下两点即可完成分词热更新。

  1. 该 http 请求需要返回两个头部(header),一个是 Last-Modified,一个是 ETag,这两者都是字符串类型,只要有一个发生变化,该插件就会去抓取新的分词进而更新词库。
  2. 该 http 请求返回的内容格式是一行一个分词,换行符用 \n 即可。

满足上面两点要求就可以实现热更新分词了,不需要重启 ES 实例。

可以将需自动更新的热词放在一个 UTF-8 编码的 .txt 文件里,放在 nginx 或其他简易 http server 下,当 .txt 文件修改时,http server 会在客户端请求该文件时自动返回相应的 Last-Modified 和 ETag。可以另外做一个工具来从业务系统提取相关词汇,并更新这个 .txt 文件。

构建一个http请求响应,代码如下:

	/**
* 获取ik自定义词典
* @param request
* @param response
*/
@RequestMapping(value="/getCustomDict.htm")
public void getCustomDict(HttpServletRequest request, HttpServletResponse response){
try {
// 读取字典文件
String path = PropertyUtil.getPro("ES_IK_DICT");
File file = new File(path);
String content = "";
if(file.exists()){
// 读取文件内容
FileInputStream fi = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
int offset = 0, numRead = 0;
while (offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
fi.close();
content = new String(buffer, "UTF-8");
}
// 返回数据
OutputStream out= response.getOutputStream();
response.setHeader("Last-Modified", String.valueOf(content.length()));
response.setHeader("ETag",String.valueOf(content.length()));
response.setContentType("text/plain; charset=utf-8");
out.write(content.getBytes("utf-8"));
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}

更新词典名称为customDict.txt,为utf-8编码。

ik配置文件IKAnalyzer.cfg.xml更改配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic;custom/sougou.dic</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords">custom/ext_stopword.dic</entry>
<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">http://192.168.1.152:8080/tour/interface/getCustomDict.htm</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

先启动http服务,然后再启动ES服务即可,ES 启动log如下:

[2017-08-11T16:37:31,267][INFO ][o.w.a.d.Monitor          ] try load config from D:\elasticsearch-5.5.0-standalone\config\analysis-ik\IKAnalyzer.cfg.xml
[2017-08-11T16:37:31,283][INFO ][o.w.a.d.Monitor ] try load config from D:\elasticsearch-5.5.0-standalone\plugins\ik\config\IKAnalyzer.cfg.xml
[2017-08-11T16:37:31,392][INFO ][o.w.a.d.Monitor ] [Dict Loading] custom\mydict.dic
[2017-08-11T16:37:31,392][INFO ][o.w.a.d.Monitor ] [Dict Loading] custom\single_word_low_freq.dic
[2017-08-11T16:37:31,392][INFO ][o.w.a.d.Monitor ] [Dict Loading] custom\sougou.dic
[2017-08-11T16:37:31,837][INFO ][o.w.a.d.Monitor ] [Dict Loading] http://localhost:8080/tour/web/ESConfig/getCustomDict.htm
[2017-08-11T16:37:31,852][INFO ][o.w.a.d.Monitor ] zzu119
[2017-08-11T16:37:31,852][INFO ][o.w.a.d.Monitor ] archer
[2017-08-11T16:37:31,852][INFO ][o.w.a.d.Monitor ] anchor
[2017-08-11T16:37:31,852][INFO ][o.w.a.d.Monitor ] 小明
[2017-08-11T16:37:31,852][INFO ][o.w.a.d.Monitor ] 侯亮平
[2017-08-11T16:37:31,852][INFO ][o.w.a.d.Monitor ] [Dict Loading] custom\ext_stopword.dic
[2017-08-11T16:37:31,852][INFO ][o.w.a.d.Monitor ] 重新加载词典完毕...

参考文档:http://www.cnblogs.com/liang1101/p/7282744.html