ik分词 动态增加词库 mysql_ElasticSearch 重写IK分词器源码设置mysql热词更新词库

常用热词词库的配置方式

1.采用IK 内置词库

优点:部署方便,不用额外指定其他词库位置

缺点:分词单一化,不能指定想分词的词条

2.IK 外置静态词库

优点:部署相对方便,可以通过编辑指定文件分词文件得到想要的词条

缺点:需要指定外部静态文件,每次需要手动编辑整个分词文件,然后放到指定的文件目录下,重启ES后才能生效

3.IK 远程词库

优点:通过指定一个静态文件代理服务器来设置IK分词的词库信息

缺点:需要手动编辑整个分词文件来进行词条的添加, IK源码中判断头信息Last-Modified  ETag 标识来判断是否更新,有时不生效

结合上面的优缺点,决定采用Mysql作为外置热词词库,定时更新热词 和 停用词。

准备工作

1.下载合适的ElasticSearch对应版本的IK分词器:https://github.com/medcl/elasticsearch-analysis-ik

2.我们来查看它config文件夹下的文件:

44e75fe8d1c197d6aff2c2ad35d326e6.png因为我本地安装的是ES是5.5.0版本,所以下载的IK为5.5.0的适配版

3.分析IKAnalyzer.cfg.xml 配置文件:

IK Analyzer 扩展配置

custom/mydict.dic;custom/single_word_low_freq.dic

custom/ext_stopword.dic

ext_dict:对应的扩展热词词典的位置,多个热词文件之间使用分号来进行间隔

ext_stopwords:对应扩展停用词词典位置,多个之间用分号进行间隔

remote_ext_dict:远程扩展热词位置 如:https://xxx.xxx.xxx.xxx/ext_hot.txt

remote_ext_stopwords:远程扩展停用词位置 如:https://xxx.xxx.xxx.xxx/ext_stop.txt

4.除了config/ 文件夹中IKAnalyzer.cfg.xml 文件,我们开下config文件夹下其他文件的作用:

Dictionary中单例方法public static synchronized Dictionary initial(Configuration cfg)

private DictSegment _MainDict;

private DictSegment _SurnameDict;

private DictSegment _QuantifierDict;

private DictSegment _SuffixDict;

private DictSegment _PrepDict;

private DictSegment _StopWords;

...

public static synchronized Dictionary initial(Configuration cfg) {

if (singleton == null) {

synchronized (Dictionary.class) {

if (singleton == null) {

singleton = new Dictionary(cfg);

singleton.loadMainDict();

singleton.loadSurnameDict();

singleton.loadQuantifierDict();

singleton.loadSuffixDict();

singleton.loadPrepDict();

singleton.loadStopWordDict();

if(cfg.isEnableRemoteDict()){

// 建立监控线程

for (String location : singleton.getRemoteExtDictionarys()) {

// 10 秒是初始延迟可以修改的 60是间隔时间 单位秒

pool.scheduleAtFixedRate(new Monitor(location), 10, 60, TimeUnit.SECONDS);

}

for (String location : singleton.getRemoteExtStopWordDictionarys()) {

pool.scheduleAtFixedRate(new Monitor(location), 10, 60, TimeUnit.SECONDS);

}

}

return singleton;

}

}

}

return singlet

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值