elasticsearch的IK 中文分词器下载和使用

IK 中文分词器下载网址

注意版本要一模一样才能使用

将解压后的后的文件夹放入 ES 根目录下的 plugins 目录下,重启 ES 即可使用。

我们这次加入新的查询参数"analyzer":“ik_max_word”。

# GET http://localhost:9200/_analyze
{
	"text":"测试单词",
	"analyzer":"ik_max_word"
}
  • ik_max_word:会将文本做最细粒度的拆分。
  • ik_smart:会将文本做最粗粒度的拆分。

使用中文分词后的结果为:

{
    "tokens": [
        {
            "token": "测试", 
            "start_offset": 0, 
            "end_offset": 2, 
            "type": "CN_WORD", 
            "position": 0
        }, 
        {
            "token": "单词", 
            "start_offset": 2, 
            "end_offset": 4, 
            "type": "CN_WORD", 
            "position": 1
        }
    ]
}

ES 中也可以进行扩展词汇,首先查询

#GET http://localhost:9200/_analyze

{
    "text":"弗雷尔卓德",
    "analyzer":"ik_max_word"
}

仅仅可以得到每个字的分词结果,我们需要做的就是使分词器识别到弗雷尔卓德也是一个词语。

{
    "tokens": [
        {
            "token": "弗",
            "start_offset": 0,
            "end_offset": 1,
            "type": "CN_CHAR",
            "position": 0
        },
        {
            "token": "雷",
            "start_offset": 1,
            "end_offset": 2,
            "type": "CN_CHAR",
            "position": 1
        },
        {
            "token": "尔",
            "start_offset": 2,
            "end_offset": 3,
            "type": "CN_CHAR",
            "position": 2
        },
        {
            "token": "卓",
            "start_offset": 3,
            "end_offset": 4,
            "type": "CN_CHAR",
            "position": 3
        },
        {
            "token": "德",
            "start_offset": 4,
            "end_offset": 5,
            "type": "CN_CHAR",
            "position": 4
        }
    ]
}
  1. 首先进入 ES 根目录中的 plugins 文件夹下的 ik 文件夹,进入 config 目录,创建 custom.dic文件,写入“弗雷尔卓德”。

  2. 同时打开 IKAnalyzer.cfg.xml 文件,将新建的 custom.dic 配置其中。

  3. 重启 ES 服务器 。

    
    
    
    	IK Analyzer 扩展配置
    	
    	custom.dic
    	 
    	
    	
    	
    	
    	
    
    
    

扩展后再次查询

# GET http://localhost:9200/_analyze
{
	"text":"弗雷尔卓德",
	"analyzer":"ik_max_word"
}

你可能感兴趣的:(elasticsearch,中文分词)