elasticsearch 拼音搜索

现在很多公司都开始使用es来做搜索,我们公司目前也有好几个业务部门在用,我主要做商户搜索,为业务部门提供基础支持。上周把呼叫中心的搜索重新整理了下,在新增几个字段后,全量同步发现通过拼音首字母搜索无法搜索出来了,最后发现是词库地址变更,导致分词出现了问题。

我整理了下es的搜索分词插件和流程,如下:

1. 下载安装分词插件 https://github.com/medcl/elasticsearch-analysis-ik 

修改 IKAnalyzer.cfg.xml 配置加载自己的远程扩展词库,我的词库由于一次机房迁移导致地址失效了,但是一直都没有发现是因为大部分商户数据并没有更新,分词索引必须要在数据更新时才会被重建!

2. 下载安装拼音插件 https://github.com/medcl/elasticsearch-analysis-pinyin

创建索引

curl -XPUT http://127.0.0.1:9200/demo/ -d'{
"settings" : {
      "index" : {
          "analysis": {
             "analyzer": {
                 "ik_smart_pinyin": {
                     "tokenizer": "ik_smart",
                     "filter": [
                         "my_pinyin",
                         "lowercase",
                         "word_delimiter"
                     ]
                 },
                 "ik_max_word_pinyin": {
                     "tokenizer": "ik_max_word",
                     "filter": [
                         "my_pinyin",
                         "lowercase",
                         "word_delimiter"
                     ]
                 }
             },
             "tokenizer": {
                 "ik_smart": {
                     "type": "ik_smart",
                     "use_smart": "true"
                 },
                 "ik_max_word": {
                     "type": "ik_max_word",
                     "use_smart": "false"
                 }
             },
             "filter": {
                 "my_pinyin": {
                     "type": "pinyin",
                     "first_letter": "all"
                 }
             }
         }
      }
}}'

curl -XPUT http://127.0.0.1:9200/_analyze?analyzer=ik_smart_pinyin&text=望湘园

{
    "tokens": [
        {
            "token": "wang",
            "start_offset": 0,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "xiang",
            "start_offset": 0,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 1
        },
        {
            "token": "yuan",
            "start_offset": 0,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 2
        },
        {
            "token": "wxy",
            "start_offset": 0,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 3
        }
    ]
}
"token": "wxy" 就是首字母
原文地址:https://www.cnblogs.com/raoshaoquan/p/6692249.html