elasticsearch基本配置上篇已经简单介绍过,本文讲述配置ik分词器插件的安装,测试,自定义扩展字典,简单使用。但愿能帮助后来者少走点弯路。git
个人elasticsearch版本是elasticsearch-v6.5.4,因此须要下载的ik分词器版本是elasticsearch-ik-v6.5.4github
下载文件:json
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.tar.gz
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip
复制代码
进入elasticsearch的安装目录,解压ik分词器内文件到plugin目录:bash
root @ localhost in /data/elasticsearch-6.5.4 [17:18:23]
$ l
总用量 4.8M
drwxrwxr-x 9 euser euser 198 12月 11 11:26 .
drwxr-xr-x. 7 root root 90 1月 16 16:35 ..
drwxrwxr-x 3 euser euser 4.0K 12月 11 11:13 bin
drwxrwxr-x 2 euser euser 178 12月 11 11:32 config
drwxrwxr-x 3 euser euser 19 12月 11 11:25 data
-rwxrwxr-x 1 euser euser 4.3M 12月 6 22:30 elasticsearch-analysis-ik-6.5.4.zip
drwxrwxr-x 3 euser euser 4.0K 11月 30 08:02 lib
-rwxrwxr-x 1 euser euser 14K 11月 30 07:55 LICENSE.txt
drwxrwxrwx 2 euser euser 8.0K 2月 11 01:30 logs
drwxrwxr-x 28 euser euser 4.0K 11月 30 08:02 modules
-rwxrwxr-x 1 euser euser 395K 11月 30 08:01 NOTICE.txt
drwxrwxr-x 3 euser euser 25 12月 11 11:29 plugins
-rwxrwxr-x 1 euser euser 8.4K 11月 30 07:55 README.textile
复制代码
进入到plugin目录,建立文件夹app
mkdir analysis-ik/
复制代码
解压ik分词器中的文件到analysis-ik目录:curl
# root @ iZ2zedtbewsc8oa9i1cb4tZ in /data/elasticsearch-6.5.4 [18:01:37]
$ cd plugins/
# root @ iZ2zedtbewsc8oa9i1cb4tZ in /data/elasticsearch-6.5.4 [18:01:37]
$ mkdir analysis-ik
# root @ iZ2zedtbewsc8oa9i1cb4tZ in /data/elasticsearch-6.5.4 [18:01:37]
$ mv ../../../analysis-ik analysis-ik
# root @ iZ2zedtbewsc8oa9i1cb4tZ in /data/elasticsearch-6.5.4/plugins [18:04:29]
$ ls
analysis-ik
# root @ iZ2zedtbewsc8oa9i1cb4tZ in /data/elasticsearch-6.5.4/plugins [18:04:34]
$ ls -l ./analysis-ik/
total 1432
-rw-r--r-- 1 root root 263965 May 6 2018 commons-codec-1.9.jar
-rw-r--r-- 1 root root 61829 May 6 2018 commons-logging-1.2.jar
drwxr-xr-x 2 root root 4096 Aug 26 17:52 config
-rw-r--r-- 1 root root 54693 Dec 23 11:26 elasticsearch-analysis-ik-6.5.4.jar
-rw-r--r-- 1 root root 736658 May 6 2018 httpclient-4.5.2.jar
-rw-r--r-- 1 root root 326724 May 6 2018 httpcore-4.4.4.jar
-rw-r--r-- 1 root root 1805 Dec 23 11:26 plugin-descriptor.properties
-rw-r--r-- 1 root root 125 Dec 23 11:26 plugin-security.policy
复制代码
配置默认分词工具为ik分词,在ElasticSearch的配置文件config/elasticsearch.yml中的最后一行添加参数: index.analysis.analyzer.default.type:ik (则设置全部索引的默认分词器为ik分词,也能够不这么作,经过设置mapping来使用ik分词)elasticsearch
# root @ iZ2zedtbewsc8oa9i1cb4tZ in /data/elasticsearch-6.5.4 [18:33:16]
$ cd config/
# root @ iZ2zedtbewsc8oa9i1cb4tZ in /data/elasticsearch-6.5.4/config [18:33:21]
$ echo "index.analysis.analyzer.default.type:ik" >> elasticsearch.yml
复制代码
方便测试,以普通模式启动:工具
./bin/elasticsearch
复制代码
建立索引文件:post
curl -XPUT http://localhost:9200/class
复制代码
使用ik分词,查看效果:测试
curl -XGET -H "Content-Type: application/json" 'http://localhost:9200/class/_analyze?pretty' -d ' { "analyzer": "ik_max_word", "text": "我是中国人,我爱个人祖国和人民" }'
{
"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" : 5,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "中国",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "国人",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "我",
"start_offset" : 6,
"end_offset" : 7,
"type" : "CN_CHAR",
"position" : 5
},
{
"token" : "爱我",
"start_offset" : 7,
"end_offset" : 9,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "的",
"start_offset" : 9,
"end_offset" : 10,
"type" : "CN_CHAR",
"position" : 7
},
{
"token" : "祖国",
"start_offset" : 10,
"end_offset" : 12,
"type" : "CN_WORD",
"position" : 8
},
{
"token" : "和",
"start_offset" : 12,
"end_offset" : 13,
"type" : "CN_CHAR",
"position" : 9
},
{
"token" : "人民",
"start_offset" : 13,
"end_offset" : 15,
"type" : "CN_WORD",
"position" : 10
}
]
}
复制代码
./data/elasticsearch/bin/elasticsearch -d
复制代码
待续 ......