美文网首页
[7]分词器的对比和自定义分词器的安装

[7]分词器的对比和自定义分词器的安装

作者: 不怕天黑_0819 | 来源:发表于2022-03-24 16:13 被阅读0次

    本文集主要是总结自己在项目中使用ES 的经验教训,包括各种实战和调优。

    除了采用其他的分词器,也可以使用n-grams来实现。具体内容参考如下:https://www.elastic.co/guide/en/elasticsearch/guide/master/mixed-lang-fields.html

    https://github.com/medcl/elasticsearch-analysis-ik

    https://github.com/medcl/elasticsearch-analysis-ik/releases

    可以自己maven打包生成也可以直接从release里面选择自己所需的版本下载zip文件。

    wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.3.0/elasticsearch-analysis-ik-5.3.0.zip

    下载之后解压到elasticsearch/plugins/analysis-ik下。

    然后重新启动elasticsearch即可。

    定义map时需要同时指定:

    "analyzer": "ik_smart", "search_analyzer": "ik_max_word",

    同时es5.0以后移除了IK的analyzer和tokenizer,分别改成ik_smart和ik_max_word

    ik_max_word会更大程度的进行单词拆分,个人觉得分词效果不如ik_smart好。

    查看中文分词如何拆分的:curl http://localhost:9200/subscribe/_analyze?analyzer=ik_smart -d'{"text":"北京大学"}'

    同时可以配置自己的分词词典。

    修改plugins/analysis-ik/config/IKAnalyzer.cfg.xml即可。

    也可以热更新IK分词,需要在xml中配置远程扩展词典,具体方法未尝试。

    参考链接:https://github.com/medcl/elasticsearch-analysis-ik


    关于热更新分词的使用:

    在custom/mydict.dic里面新增的分词需要重新启动elasticsearch才能够有用。

    实现热分词需要在/IKAnalyzer.cfg.xml添加如下配置,相当于一个http请求,这个请求会返回Last-Modify、ETags字段。

    可以启动tomcat或者任意一个http请求即可。我这里通过python启动的一个简单的http服务进行测试。

    python -m SimpleHTTPServer 端口号

    nohup python -m SimpleHTTPServer 9999 > /home/appops/elasticsearch/logs/SimpleHTTPServer.log & 后台执行,指定端口号

    注:需要在某一个文件夹下执行上面的命令,然后通过http://localhost:9999就可以访问到当时执行命令所在路径下的所有文件了。

    关于tomat的作为请求的方法:http://www.cnblogs.com/zlslch/p/6441315.html

    也可以自定义分析器,https://es.xiaoleilu.com/070_Index_Mgmt/20_Custom_Analyzers.html

    各种分词器的比较

    IK分词器可以很好地解决中文分词,但是面对英文分词表现较差,比如单复数的形式,标准分词和中文分词都不能解决单复数问题。可如下设置:

    "properties": {

    "title": {

    "type": "string",

    "index": "analyzed",

    "fields": {

    "cn": {

    "type": "string",

    "analyzer": "ik_smart",

    "search_analyzer": "ik_max_word"

    },

    "en": {

    "type": "string",

    "analyzer": "english"

    }

    }

    }
    即同时创建多个fields,分别指定不同的分词器。

    参考链接:http://keenwon.com/1404.html

    相关文章

      网友评论

          本文标题:[7]分词器的对比和自定义分词器的安装

          本文链接:https://www.haomeiwen.com/subject/vsiirktx.html