安装IK分词器

一、进入到es的plugins文件夹创建文件夹analysis-ik
mkdir analysis-ik
二、下载ik压缩包文件
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip
三、解压到当前目录
unzip elasticsearch-analysis-ik-6.4.0.zip
四、验证ik是否安装成功
http://192.168.105.180:9200/_analyze
{
"analyzer": "ik_max_word",
"text": "中华人民共和国国歌"
}

安装拼音插件

一、进入到es的plugins文件夹创建文件夹analysis-pinyin
mkdir analysis-pinyin
二、下载拼音压缩包文件
wget https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v6.4.0/elasticsearch-analysis-pinyin-6.4.0.zip
三、解压到当前目录
unzip elasticsearch-analysis-pinyin-6.4.0.zip
四、验证拼音插件是否安装成功
http://192.168.105.180:9200/_analyze
{
"analyzer": "pinyin_analyzer",
"text": "苏坡蜜"
}

将ik和拼音插件放到一起的配置

http://192.168.105.180:9200/index/_settings
{
    "index": {
        "analysis": {
            "analyzer": {
                "ik_pinyin_analyzer": {
                    "type": "custom",
                    "tokenizer": "ik_max_word",
                    "filter": [
                        "my_pinyin",
                        "word_delimiter"
                    ]
                }
            },
            "filter": {
                 "my_pinyin" : {
                    "type" : "pinyin",
                    "keep_separate_first_letter" : false,
                    "keep_full_pinyin" : true,
                    "keep_original" : false,
                    "limit_first_letter_length" : 10,
                    "lowercase" : true,
                    "remove_duplicated_term" : true
                }
            }
        }
    }
}

然后设置mapping

http://192.168.105.180:9200/index/folks/_mapping
{
  "folks": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "pinyin": {
            "analyzer": "ik_pinyin_analyzer",
            "term_vector": "with_offsets",
            "boost": 10,
            "type": "text"
          }
        }
      }
    }
  }
}

都设置好以后验证是否成功

http://192.168.105.180:9200/_analyze
 {
  "analyzer": "ik_pinyin_analyzer",
  "text": "如家酒店"
}

下面我们在name字段里面加一条数据验证是否成功

增加了一条叫“如家酒店”的数据,验证一下能不能搜到吧

可以看到搜索“rj”是可以匹配到如家酒店的

参考文献:

https://blog.csdn.net/jinyidong/article/details/79690186 

https://www.cnblogs.com/yijialong/p/9719964.html

转载于:https://www.cnblogs.com/Hero-/p/10070575.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐