elasticsearch安装ik分词器

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介:

一、概要:


1.es默认的分词器对中文支持不好,会分割成一个个的汉字。ik分词器对中文的支持要好一些,主要由两种模式:ik_smart和ik_max_word
2.环境
操作系统:centos
es版本:6.0.0

二、安装插件


1.插件地址:https://github.com/medcl/elasticsearch-analysis-ik
2.运行命令行:

./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.0.0/elasticsearch-analysis-ik-6.0.0.zip

运行完成后会发现多了以下文件:esroot 下的plugins和config文件夹多了analysis-ik目录。

三、重启es


1.查找es进程

ps -ef | grep elastic

2.终止进程
从上面的结果可以看到es进程号是12776.
执行命令:

kill 12776

3.启动es后台运行

./bin/sh elastic search –d

提醒:重启es会重新分片,线上环境要注意了。

四、测试


1.使用ik_max_word分词

复制代码
GET _analyze 
{ 
   "analyzer":"ik_max_word",
   "text":"中华人民共和国国歌"
}
复制代码

分词结果:

复制代码
{
   "tokens": [
     {
       "token": "中华人民共和国",
       "start_offset": 0,
       "end_offset": 7,
       "type": "CN_WORD",
       "position": 0
     },
     {
       "token": "中华人民",
       "start_offset": 0,
       "end_offset": 4,
       "type": "CN_WORD",
       "position": 1
     },
     {
       "token": "中华",
       "start_offset": 0,
       "end_offset": 2,
       "type": "CN_WORD",
       "position": 2
     },
     {
       "token": "华人",
       "start_offset": 1,
       "end_offset": 3,
       "type": "CN_WORD",
       "position": 3
     },
     {
       "token": "人民共和国",
       "start_offset": 2,
       "end_offset": 7,
       "type": "CN_WORD",
       "position": 4
     },
     {
       "token": "人民",
       "start_offset": 2,
       "end_offset": 4,
       "type": "CN_WORD",
       "position": 5
     },
     {
       "token": "共和国",
       "start_offset": 4,
       "end_offset": 7,
       "type": "CN_WORD",
       "position": 6
     },
     {
       "token": "共和",
       "start_offset": 4,
       "end_offset": 6,
       "type": "CN_WORD",
       "position": 7
     },
     {
       "token": "",
       "start_offset": 6,
       "end_offset": 7,
       "type": "CN_CHAR",
       "position": 8
     },
     {
       "token": "国歌",
       "start_offset": 7,
       "end_offset": 9,
       "type": "CN_WORD",
       "position": 9
     }
   ]
}
复制代码

 

2.使用ik_smart分词

复制代码
GET _analyze 
{ 
   "analyzer":"ik_smart",
   "text":"中华人民共和国国歌"
}
复制代码

分词结果:

复制代码
{
   "tokens": [
     {
       "token": "中华人民共和国",
       "start_offset": 0,
       "end_offset": 7,
       "type": "CN_WORD",
       "position": 0
     },
     {
       "token": "国歌",
       "start_offset": 7,
       "end_offset": 9,
       "type": "CN_WORD",
       "position": 1
     }
   ]
}
复制代码

五、java api分词测试

1.调用ik_max_word分词

复制代码
@Test
public void analyzer_ik_max_word() throws Exception {
     java.lang.String text = "提前祝大家春节快乐!";

    TransportClient client = EsClient.get();
     AnalyzeRequest request = (new AnalyzeRequest()).analyzer("ik_max_word").text(text);
     List<AnalyzeResponse.AnalyzeToken> tokens = client.admin().indices().analyze(request).actionGet().getTokens();
     System.out.println(tokens.size());//6
     for (AnalyzeResponse.AnalyzeToken token : tokens) {
         System.out.println(token.getTerm() + " ");
     }
}
复制代码

结果:

复制代码
6
提前 
祝 
大家 
春节快乐 
春节 
快乐
复制代码

2.调用ik_smart分词

复制代码
@Test
public void analyzer_ik_smart() throws Exception {
     java.lang.String text = "提前祝大家春节快乐!";

    TransportClient client = EsClient.get();
     AnalyzeRequest request = (new AnalyzeRequest()).analyzer("ik_smart").text(text);
     List<AnalyzeResponse.AnalyzeToken> tokens = client.admin().indices().analyze(request).actionGet().getTokens();
     System.out.println(tokens.size());
     for (AnalyzeResponse.AnalyzeToken token : tokens) {
         System.out.println(token.getTerm() + " ");
     }
}
复制代码

结果:

复制代码
4
提前 
祝 
大家 
春节快乐
复制代码






相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
9天前
|
数据可视化 索引
elasticsearch head、kibana 安装和使用
elasticsearch head、kibana 安装和使用
|
21天前
|
存储 负载均衡 索引
linux7安装elasticsearch-7.4.0集群配置
linux7安装elasticsearch-7.4.0集群配置
109 0
|
2月前
|
存储 监控 搜索推荐
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——安装篇(一)
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——安装篇(一)
|
4月前
ElasticSearch-Head浏览器插件离线安装
ElasticSearch-Head浏览器插件离线安装
91 0
|
8天前
|
JSON Unix Linux
Elasticsearch如何安装
Elasticsearch如何安装
|
3月前
|
前端开发 安全 Ubuntu
Elasticsearch安装和配置
Elasticsearch安装和配置
116 0
|
1月前
|
监控 安全 Java
ElasticSearch在Windows上的下载与安装
ElasticSearch在Windows上的下载与安装
|
2月前
|
开发工具 Docker 容器
docker安装集群版ElasticSearch
docker安装集群版ElasticSearch
|
2月前
|
Java Docker 容器
Docker安装ElasticSearch
Docker如何安装ElasticSearch
|
2月前
|
自然语言处理
Elasticsearch+IK+pinyin自定义分词器
Elasticsearch+IK+pinyin自定义分词器
27 0