Elastic ik插件配置热更新功能
来源:互联网 发布:优化环境建设 编辑:程序博客网 时间:2024/04/28 12:44
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,该请求只需满足以下两点即可完成分词热更新。
- 该 http 请求需要返回两个头部(header),一个是 Last-Modified,一个是 ETag,这两者都是字符串类型,只要有一个发生变化,该插件就会去抓取新的分词进而更新词库。
- 该 http 请求返回的内容格式是一行一个分词,换行符用 \n 即可。
可以将需自动更新的热词放在一个 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
阅读全文
0 0
- Elastic ik插件配置热更新功能
- elastic笔记:ik分词插件安装
- 热更新 IK 分词使用方法
- 热更新 IK 分词使用方法
- Cordova的热更新插件配置
- es ik 分词插件 词库热加载源码分析
- springboot 热更新配置
- webpack 配置热更新
- android插件化,热修复,热更新。
- unity3d热更新插件uLua
- Elasticsearch5.x安装IK分词器以及热词配置
- Elastic Stack X-Pack 插件 更新 Updating Your License
- android热插件,热更新,热修复,模块化
- android热插件,热更新,热修复,模块化
- 关于JSPatch 热更新配置
- Elasticsearch-IK分词器详解以及源码修改实现mysql热更新
- unity3d热更新插件uLua学习整理
- unity3d热更新插件uLua学习整理
- php 获取当前目录和当前文件夹
- PHP中加运算
- strcpy和memcpy的区别
- JPA学习笔记(9)——映射双向一对多关联关系
- 第三篇:JAVA集合之Vector源码剖析
- Elastic ik插件配置热更新功能
- 欢迎使用CSDN-markdown编辑器
- SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)
- 归并排序
- 安装opencv 和 pip
- SpringMvc+ajax 实现json格式数据传递
- python sys.argv的用法
- 刷题——Meteor Shower POJ
- C#中Form的Paint事件响应方法与重载虚方法OnPaint()的区别