elasticsearch-2.3.4 ik分词器
来源:互联网 发布:mysql服务器硬件配置 编辑:程序博客网 时间:2024/06/14 17:23
1. ik分词器跟es版本对照
cd/work/elasticsearch/bin
安装ik分词器
plugin installhttps://github.com/medcl/elasticsearch-analysis-ik/releases/download/v1.9.4/elasticsearch-analysis-ik-1.9.4.zip
测试
curl –XGET'http://localhost:9200/_analyze?pretty&analyzer=standard' -d '
第二更新
'
上面例子,Elasticsearch 内置的分词器对中文不友好,会把中文分成单个字来进行全文检索,不能达到想要的结果
Curl –XGET ‘http://localhost:9200/_analyze?pretty&analyzer=ik’ –d’
第二更新
‘
Curl –XGET ‘http://localhost:9200/_analyze?pretty&analyzer=ik_max_word’ –d’
联想是全球最大的笔记本厂商’
Curl –XGET ‘http://localhost:9200/_analyze?pretty&analyzer=ik_smart’ –d’
联想是全球最大的笔记本厂商’
Elasticsearch 版本:2.2.0 CSDN下载
IK 插件版本 :1.8 CSDN下载
服务器系统:CentOS 6.4 (虚拟机)
一、安装
可以到 IK 的 GitHub 上获取对应的版本
Elasticsearch 要使用 ik,就要先构建 ik 的 jar包,这里要用到 maven 包管理工具(CSDN下载)
而 maven 需要java 环境,既然在使用 Elasticsearch ,那么我就假定你已有 java 环境了
安装 maven
cd /usr/
local/src
/
tar zxvf /usr/
local/src/apache
-maven-3.3.9-bin.tar
.gz
cp
-r/usr/
local/src/apache
-maven-3.3.9/usr/
local/maven3
.3.9
添加 maven 环境变量
vim /etc/profile
#在最后面添加
MAVEN_HOME=/usr/local/maven3.
3.9
export MAVEN_HOME
exportPATH=
${PATH}:
${MAVEN_HOME}/bin
# 执行如下代码,使更改生效
source /etc/profile
保存退出,查看maven 信息
mvn -v
# 返回
Apache Maven
3.3.9(bb52d8502b132ec0a5a3f4c09453c07478323dc5;
2015-
11-
11T00:
41:
47+
08:
00)
Maven home: /usr/
local/maven3
.3.9
Java
version:
1.7.0_79, vendor: Oracle Corporation
Java home: /usr/
local/java/jre
Default locale: zh_CN,
platformencoding: UTF-
8
OS name:
"linux",
version:
"2.6.32-358.el6.x86_64", arch:
"amd64", family:
"unix"
构建 IK 的 jar 包,你也可以下载我构建好的直接使用CSDN下载
cd /usr/
local/src
/
unzip
-xelasticsearch
-analysis-ik-1.8.zip
cd elasticsearch
-analysis-ik-master/
执行构建,第一次有点慢(下载依赖包)
mvn
package
完成后,会在当前目录下生成 target 的目录,构建好的安装包在./target/releases/elasticsearch-analysis-ik-1.8.0.zip
将安装包解压并移动到 elasticsearch 的插件目录下的ik 文件夹
unzip
-d/usr/
local/elasticsearch
-2.2.0/plugins/ik
./target/releases/elasticsearch
-analysis-ik-1.8.0.zip
重启 elasticsearch,就可以使用 ik 了
二、使用
ik 带有两个分词器
ik_max_word :会将文本做最细粒度的拆分;尽可能多的拆分出词语
ik_smart:会做最粗粒度的拆分;已被分出的词语将不会再次被其它词语占有
例子:
# ik_max_word
curl -XGET
'http://localhost:9200/_analyze?pretty&analyzer=ik_max_word'-d
'联想是全球最大的笔记本厂商'
#返回
{
"tokens"
: [ {
"token"
:
"联想",
"start_offset"
:
0,
"end_offset"
:
2,
"type"
:
"CN_WORD",
"position"
:
0
}, {
"token"
:
"全球",
"start_offset"
:
3,
"end_offset"
:
5,
"type"
:
"CN_WORD",
"position"
:
1
}, {
"token"
:
"最大",
"start_offset"
:
5,
"end_offset"
:
7,
"type"
:
"CN_WORD",
"position"
:
2
}, {
"token"
:
"笔记本",
"start_offset"
:
8,
"end_offset"
:
11,
"type"
:
"CN_WORD",
"position"
:
3
}, {
"token"
:
"笔记",
"start_offset"
:
8,
"end_offset"
:
10,
"type"
:
"CN_WORD",
"position"
:
4
}, {
"token"
:
"笔",
"start_offset"
:
8,
"end_offset"
:
9,
"type"
:
"CN_WORD",
"position"
:
5
}, {
"token"
:
"记",
"start_offset"
:
9,
"end_offset"
:
10,
"type"
:
"CN_CHAR",
"position"
:
6
}, {
"token"
:
"本厂",
"start_offset"
:
10,
"end_offset"
:
12,
"type"
:
"CN_WORD",
"position"
:
7
}, {
"token"
:
"厂商",
"start_offset"
:
11,
"end_offset"
:
13,
"type"
:
"CN_WORD",
"position"
:
8
} ]
}
# ik_smart
curl -XGET
'http://localhost:9200/_analyze?pretty&analyzer=ik_smart'-d
'联想是全球最大的笔记本厂商'
# 返回
{
"tokens"
: [ {
"token"
:
"联想",
"start_offset"
:
0,
"end_offset"
:
2,
"type"
:
"CN_WORD",
"position"
:
0
}, {
"token"
:
"全球",
"start_offset"
:
3,
"end_offset"
:
5,
"type"
:
"CN_WORD",
"position"
:
1
}, {
"token"
:
"最大",
"start_offset"
:
5,
"end_offset"
:
7,
"type"
:
"CN_WORD",
"position"
:
2
}, {
"token"
:
"笔记本",
"start_offset"
:
8,
"end_offset"
:
11,
"type"
:
"CN_WORD",
"position"
:
3
}, {
"token"
:
"厂商",
"start_offset"
:
11,
"end_offset"
:
13,
"type"
:
"CN_WORD",
"position"
:
4
} ]
}
看出区别了吧
下面我们来创建一个索引,使用 ik
创建一个名叫 iktest 的索引,设置它的分析器用 ik ,分词器用 ik_max_word,并创建一个 article 的类型,里面有一个 subject 的字段,指定其使用 ik_max_word 分词器
curl -XPUT 'http:
//localhost:9200/iktest?pretty' -d '{
"settings"
: {
"analysis"
: {
"analyzer"
: {
"ik"
: {
"tokenizer"
:
"ik_max_word"
}
}
}
},
"mappings"
: {
"article"
: {
"dynamic"
:
true,
"properties"
: {
"subject"
: {
"type"
:
"string",
"analyzer"
:
"ik_max_word"
}
}
}
}
}'
批量添加几条数据,这里我指定元数据 _id 方便查看,subject 内容为我随便找的几条新闻的标题
curl
-XPOSThttp:
//localhost:9200/iktest/article/_bulk?pretty -d '
{
"index": {
"_id":
"1"} }
{
"subject":
""闺蜜"崔顺实被韩检方传唤 韩总统府促彻查真相"}
{
"index": {
"_id":
"2"} }
{
"subject":
"韩举行"护国训练" 青瓦台:决不许国家安全出问题"}
{
"index": {
"_id":
"3"} }
{
"subject":
"媒体称FBI已经取得搜查令 检视希拉里电邮"}
{
"index": {
"_id":
"4"} }
{
"subject":
"村上春树获安徒生奖 演讲中谈及欧洲排外问题"}
{
"index": {
"_id":
"5"} }
{
"subject":
"希拉里团队炮轰FBI 参院民主党领袖批其“违法”"}
'
查询 “希拉里和韩国”
curl -XPOST http:
//localhost:9200/iktest/article/_search?pretty -d'
{
"query"
: {
"match": {
"subject":
"希拉里和韩国"}},
"highlight"
: {
"pre_tags"
: [
"<font color='red'>"],
"post_tags"
: [
"</font>"],
"fields"
: {
"subject"
: {}
}
}
}
'
#返回
{
"took"
:
113,
"timed_out"
:
false,
"_shards"
: {
"total"
:
5,
"successful"
:
5,
"failed"
:
0
},
"hits"
: {
"total"
:
4,
"max_score"
:
0.034062363,
"hits"
: [ {
"_index"
:
"iktest",
"_type"
:
"article",
"_id"
:
"2",
"_score"
:
0.034062363,
"_source"
: {
"subject"
:
"韩举行"护国训练" 青瓦台:决不许国家安全出问题"
},
"highlight"
: {
"subject"
: [
"<font color=red>韩</font>举行"护<font color=red>国</font>训练" 青瓦台:决不许国家安全出问题"]
}
}, {
"_index"
:
"iktest",
"_type"
:
"article",
"_id"
:
"3",
"_score"
:
0.0076681254,
"_source"
: {
"subject"
:
"媒体称FBI已经取得搜查令 检视希拉里电邮"
},
"highlight"
: {
"subject"
: [
"媒体称FBI已经取得搜查令 检视<font color=red>希拉里</font>电邮"]
}
}, {
"_index"
:
"iktest",
"_type"
:
"article",
"_id"
:
"5",
"_score"
:
0.006709609,
"_source"
: {
"subject"
:
"希拉里团队炮轰FBI 参院民主党领袖批其“违法”"
},
"highlight"
: {
"subject"
: [
"<font color=red>希拉里</font>团队炮轰FBI 参院民主党领袖批其“违法”"]
}
}, {
"_index"
:
"iktest",
"_type"
:
"article",
"_id"
:
"1",
"_score"
:
0.0021509775,
"_source"
: {
"subject"
:
""闺蜜"崔顺实被韩检方传唤 韩总统府促彻查真相"
},
"highlight"
: {
"subject"
: [
""闺蜜"崔顺实被<font color=red>韩</font>检方传唤 <font color=red>韩</font>总统府促彻查真相"]
}
} ]
}
}
这里用了高亮属性 highlight,直接显示到 html 中,被匹配到的字或词将以红色突出显示。若要用过滤搜索,直接将 match 改为 term 即可
三、热词更新配置
网络词语日新月异,如何让新出的网络热词(或特定的词语)实时的更新到我们的搜索当中呢
先用 ik 测试一下 :
curl -XGET
'http://localhost:9200/_analyze?pretty&analyzer=ik_max_word'-d
'
成龙原名陈港生
'
#返回
{
"tokens"
: [ {
"token"
:
"成龙",
"start_offset"
:
1,
"end_offset"
:
3,
"type"
:
"CN_WORD",
"position"
:
0
}, {
"token"
:
"原名",
"start_offset"
:
3,
"end_offset"
:
5,
"type"
:
"CN_WORD",
"position"
:
1
}, {
"token"
:
"陈",
"start_offset"
:
5,
"end_offset"
:
6,
"type"
:
"CN_CHAR",
"position"
:
2
}, {
"token"
:
"港",
"start_offset"
:
6,
"end_offset"
:
7,
"type"
:
"CN_WORD",
"position"
:
3
}, {
"token"
:
"生",
"start_offset"
:
7,
"end_offset"
:
8,
"type"
:
"CN_CHAR",
"position"
:
4
} ]
}
ik 的主词典中没有”陈港生” 这个词,所以被拆分了。 现在我们来配置一下 修改 IK 的配置文件 :ES 目录/plugins/ik/config/ik/IKAnalyzer.cfg.xml 修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>
IK Analyzer 扩展配置
</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict">
custom/mydict.dic;custom/single_word_low_freq.dic
</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords">
custom/ext_stopword.dic
</entry>
<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">
http://192.168.1.136/hotWords.php
</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
这里我是用的是远程扩展字典,因为可以使用其他程序调用更新,且不用重启 ES,很方便;当然使用自定义的 mydict.dic 字典也是很方便的,一行一个词,自己加就可以了
既然是远程词典,那么就要是一个可访问的链接,可以是一个页面,也可以是一个txt的文档,但要保证输出的内容是 utf-8 的格式
hotWords.php 的内容
$s =
<<<'EOF'
陈港生
元楼
蓝瘦
EOF;
header(
'Last-Modified: '.gmdate(
'D, d M Y H:i:s', time()).
' GMT',
true,
200);
header(
'ETag: "5816f349-19"');
echo$s
;
ik 接收两个返回的头部属性 Last-Modified 和 ETag,只要其中一个有变化,就会触发更新,ik 会每分钟获取一次
重启 Elasticsearch ,查看启动记录,看到了三个词已被加载进来
[
2016-10-31 15:08:57,749][
INFO][
ik-analyzer] 陈港生
[
2016-10-31 15:08:57,749][
INFO][
ik-analyzer] 元楼
[
2016-10-31 15:08:57,749][
INFO][
ik-analyzer] 蓝瘦
现在我们来测试一下,再次执行上面的请求,返回
...
}, {
"token"
:
"陈港生",
"start_offset"
:
5,
"end_offset"
:
8,
"type"
:
"CN_WORD",
"position"
:
2
}, {
...
可以看到 ik 分词器已经匹配到了 “陈港生” 这个词
‘
- elasticsearch-2.3.4 ik分词器
- elasticsearch 配置ik分词器
- Elasticsearch 集成IK分词器
- elasticsearch配置ik分词器
- elasticsearch 安装ik 分词器
- elasticSearch安装ik分词器
- Elasticsearch集成ik分词器
- Elasticsearch安装IK分词器
- elasticsearch ik分词器安装
- elasticsearch安装ik分词器
- Elasticsearch安装IK分词器
- Elasticsearch 安装ik分词器
- elasticsearch 2.3.4中文分词插件ik 1.9.4安装
- elasticsearch java api 使用ik 分词器
- ElasticSearch配置ik中文分词器
- elasticsearch中文分词器IK安装
- ElasticSearch 安装和使用IK分词器
- elasticsearch学习之IK分词器
- Java类变量类型
- IE6双倍边距问题及其解决方法
- Unity "Apply Root Motion" 与 “Bake into Pose”
- linux 通过crt直接上传和下载文件和文件
- AppCompatActivity 中的Menu (菜单)
- elasticsearch-2.3.4 ik分词器
- 2017北京云栖大会拉开帷幕,Clouder Lab动手实验室受开发者欢迎
- px4原生源码学习四--Nuttx 实时操作系统编程
- Python入门1
- HDU2037:今年暑假不AC
- effective C++ 08_别让异常逃离析构函数 读书笔记
- 深圳领存技术发布6U VPX异构加速计算机
- RecyclerView拖拽Item动画
- C++模板--函数模板基本 说明