Elasticsearch IK分词器热更新

简介

插件的下载地址: 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()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值