ik分词器的热词更新_Elastic ik插件配置热更新功能

官网说明:

热更新 IK 分词使用方法

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

location

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

该 http 请求需要返回两个头部(header),一个是 Last-Modified,一个是 ETag,这两者都是字符串类型,只要有一个发生变化,该插件就会去抓取新的分词进而更新词库。

该 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更改配置如下:

IK Analyzer 扩展配置

custom/mydict.dic;custom/single_word_low_freq.dic;custom/sougou.dic

custom/ext_stopword.dic

http://192.168.1.152:8080/tour/interface/getCustomDict.htm

先启动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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值