简介
插件的下载地址: https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v6.8.1
主要原理: 创建热更新的http服务,配置IK远端更新地址;步骤如下:
修改IK配置文件
vi plugins/ik/config/IKAnalyzer.cfg.xml
修改 remote_ext_dict和remote_ext_stopwords这两项
<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">http://localhost:9527/extdic</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<entry key="remote_ext_stopwords">http://localhost:9527/stopwords</entry>
服务器代码
使用python的tornado模块构建服务器,代码如下:
remotedic.py
import tornado.ioloop
import tornado.web
import os,time
##配置文件
conf={
"port": 9527,
"ext_dic":"ext.dic",
"stopwords":"stop.dic"
}
## Server句柄
class MainHandler(tornado.web.RequestHandler):
# 初始化,传入字典文件
def initialize(self, file):
self.file = file
# 文件不存在就创建
if not os.access(self.file, os.F_OK):
f = open(self.file, 'w')
f.close()
# GET method
def get(self):
f = open(self.file, 'r', encoding='utf-8')
data = f.read()
f.close()
self.set_header("Content-Type", "text/plain; charset=UTF-8")
self.set_header("ETag", "2")
self.write(data)
# HEAD mothod
def head(self):
# 获取更新时间,设置为上次更改的标志
mTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.stat(self.file).st_mtime))
self.set_header("Last-Modified",mTime)
self.set_header("ETag","2" )
self.set_header("Content-Length", "0")
self.finish()
# 注册webMapping
def make_app():
return tornado.web.Application([
(r"/extdic", MainHandler,{
"file": conf["ext_dic"]}),
(r"/stopwords", MainHandler,{
"file": conf["stopwords"]})
])
if __name__ == "__main__":
app = make_app()
app.listen(conf["port"])
tornado.ioloop.IOLoop.current()