先来一个标准分词(standard),配置如下:

curl -XPUT localhost:9200/local -d '{
"settings" : {
"analysis" : {
"analyzer" : {
"stem" : {
"tokenizer" : "standard",
"filter" : ["standard", "lowercase", "stop", "porter_stem"]
}
}
}
},
"mappings" : {
"article" : {
"dynamic" : true,
"properties" : {
"title" : {
"type" : "string",
"analyzer" : "stem"
}
}
}
}
}'

index:local

type:article

default analyzer:stem (filter:小写、停用词等)

field:title  

测试:

# Sample Analysis
curl -XGET localhost:9200/local/_analyze?analyzer=stem -d '{Fight for your life}'
curl -XGET localhost:9200/local/_analyze?analyzer=stem -d '{Bruno fights Tyson tomorrow}' # Index Data
curl -XPUT localhost:9200/local/article/1 -d'{"title": "Fight for your life"}'
curl -XPUT localhost:9200/local/article/2 -d'{"title": "Fighting for your life"}'
curl -XPUT localhost:9200/local/article/3 -d'{"title": "My dad fought a dog"}'
curl -XPUT localhost:9200/local/article/4 -d'{"title": "Bruno fights Tyson tomorrow"}' # search on the title field, which is stemmed on index and search
curl -XGET localhost:9200/local/_search?q=title:fight # searching on _all will not do anystemming, unless also configured on the mapping to be stemmed...
curl -XGET localhost:9200/local/_search?q=fight

例如:

Fight for your life

分词如下:

{"tokens":[
{"token":"fight","start_offset":1,"end_offset":6,"type":"<ALPHANUM>","position":1},
{"token":"your","start_offset":11,"end_offset":15,"type":"<ALPHANUM>","position":3},
{"token":"life","start_offset":16,"end_offset":20,"type":"<ALPHANUM>","position":4}
]}

  


部署ik分词器:

1)将ik分词器插件(es)拷贝到./plugins/analyzerIK/中

2)在elasticsearch.yml中配置

index.analysis.analyzer.ik.type : "ik"

3)在config中添加./config/ik

IKAnalyzer.cfg.xml

main.dic

quantifier.dic

ext.dic

stopword.dic

delete之前创建的index,重新配置如下:

curl -XPUT localhost:9200/local -d '{
"settings" : {
"analysis" : {
"analyzer" : {
"ik" : {
"tokenizer" : "ik"
}
}
}
},
"mappings" : {
"article" : {
"dynamic" : true,
"properties" : {
"title" : {
"type" : "string",
"analyzer" : "ik"
}
}
}
}
}'

  

测试:

curl 'http://localhost:9200/index/_analyze?analyzer=ik&pretty=true' -d'
{
"text":"中华人民共和国国歌"
}
'
{
"tokens" : [ {
"token" : "text",
"start_offset" : 12,
"end_offset" : 16,
"type" : "ENGLISH",
"position" : 1
}, {
"token" : "中华人民共和国",
"start_offset" : 19,
"end_offset" : 26,
"type" : "CN_WORD",
"position" : 2
}, {
"token" : "国歌",
"start_offset" : 26,
"end_offset" : 28,
"type" : "CN_WORD",
"position" : 3
} ]
}

  

---------------------------------------

如果我们想返回最细粒度的分词结果,需要在elasticsearch.yml中配置如下:

index:
analysis:
analyzer:
ik:
alias: [ik_analyzer]
type: org.elasticsearch.index.analysis.IkAnalyzerProvider
ik_smart:
type: ik
use_smart: true
ik_max_word:
type: ik
use_smart: false

  

测试:

curl 'http://localhost:9200/index/_analyze?analyzer=ik_max_word&pretty=true' -d'
{
"text":"中华人民共和国国歌"
}
'
{
"tokens" : [ {
"token" : "text",
"start_offset" : 12,
"end_offset" : 16,
"type" : "ENGLISH",
"position" : 1
}, {
"token" : "中华人民共和国",
"start_offset" : 19,
"end_offset" : 26,
"type" : "CN_WORD",
"position" : 2
}, {
"token" : "中华人民",
"start_offset" : 19,
"end_offset" : 23,
"type" : "CN_WORD",
"position" : 3
}, {
"token" : "中华",
"start_offset" : 19,
"end_offset" : 21,
"type" : "CN_WORD",
"position" : 4
}, {
"token" : "华人",
"start_offset" : 20,
"end_offset" : 22,
"type" : "CN_WORD",
"position" : 5
}, {
"token" : "人民共和国",
"start_offset" : 21,
"end_offset" : 26,
"type" : "CN_WORD",
"position" : 6
}, {
"token" : "人民",
"start_offset" : 21,
"end_offset" : 23,
"type" : "CN_WORD",
"position" : 7
}, {
"token" : "共和国",
"start_offset" : 23,
"end_offset" : 26,
"type" : "CN_WORD",
"position" : 8
}, {
"token" : "共和",
"start_offset" : 23,
"end_offset" : 25,
"type" : "CN_WORD",
"position" : 9
}, {
"token" : "国",
"start_offset" : 25,
"end_offset" : 26,
"type" : "CN_CHAR",
"position" : 10
}, {
"token" : "国歌",
"start_offset" : 26,
"end_offset" : 28,
"type" : "CN_WORD",
"position" : 11
} ]
}

  

elasticsearch 口水篇(8)分词 中文分词 ik插件的更多相关文章

  1. elasticsearch 口水篇(1) 安装、插件

    一)安装elasticsearch 1)下载elasticsearch-0.90.10,解压,运行\bin\elasticsearch.bat (windwos) 2)进入http://localho ...

  2. elasticsearch 口水篇(4)java客户端 - 原生esClient

    上一篇(elasticsearch 口水篇(3)java客户端 - Jest)Jest是第三方客户端,基于REST Api进行调用(httpClient),本篇简单介绍下elasticsearch原生 ...

  3. ElasticSearch简介(三)——中文分词

    很多时候,我们需要在ElasticSearch中启用中文分词,本文这里简单的介绍一下方法.首先安装中文分词插件.这里使用的是 ik,也可以考虑其他插件(比如 smartcn). $ ./bin/ela ...

  4. elasticsearch学习笔记-倒排索引以及中文分词

    我们使用数据库的时候,如果查询条件太复杂,则会涉及到很多问题 1.无法维护,各种嵌套查询,各种复杂的查询,想要优化都无从下手 2.效率低下,一般语句复杂了之后,比如使用or,like %,,%查询之后 ...

  5. elasticsearch 口水篇(9)Facet

    FACET 1)Terms Facet { "query" : { "match_all" : { } }, "facets" : { &q ...

  6. elasticsearch 口水篇(2)CRUD Sense

    Sense 为了方便.直观的使用es的REST Api,我们可以使用sense.Sense是Chrome浏览器的一个插件,使用简单. 如图: Sense安装: https://chrome.googl ...

  7. elasticsearch 口水篇(7) Eclipse中部署ES源码、运行

    ES源码可以直接从svn下载 https://github.com/elasticsearch/elasticsearch 下载后,用Maven导入(import——>Existing Mave ...

  8. elasticsearch 口水篇(6) Mapping 定义索引

    前面我们感觉ES就想是一个nosql数据库,支持Free Schema. 接触过Lucene.solr的同学这时可能会思考一个问题——怎么定义document中的field?store.index.a ...

  9. elasticsearch 口水篇(3)java客户端 - Jest

    elasticsearch有丰富的客户端,java客户端有Jest.其原文介绍如下: Jest is a Java HTTP Rest client for ElasticSearch.It is a ...

随机推荐

  1. matlab学习(1)strsplit与strtok

    strsplit函数用法: <1>默认使用空格符分割,返回一个cell数组 <2>也可以指定第二个参数进行分割 <3>第二个参数也可以时包含多个分隔符的元胞数组 & ...

  2. 51Nod 1084:矩阵取数问题 V2(多维DP)

    1084 矩阵取数问题 V2  基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励 ...

  3. 四则运算可能需要注意的地方V1.1

    作业:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/997 文档下载:https://coding.net/u/Dawnfox/p/ ...

  4. hdu3294 Girls' research manacher

    One day, sailormoon girls are so delighted that they intend to research about palindromic strings. O ...

  5. 计算字符串最后一个单词的长度,单词以空格隔开。 java算法

    import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = ne ...

  6. linux服务器进程信息查看命令

    #lsof 列出当前系统打开文件,常与-i选项使用,用于查看某个端口被哪个程序占用 [root@bogon ~]# lsof -i:80 COMMAND PID USER FD TYPE DEVICE ...

  7. MySQL导出用户权限

    在MySQL 5.5/5.6版本中,使用SHOW GRANTS命令可以导出用户的创建脚本和授权脚本. hostname='127.0.0.1' port= username='root' passwo ...

  8. haproxy httpcheck with basic auth

    一个简单的需求,就是需要在 haproxy 的 httpcheck 使用 basic 认证,解决方法 base64 编码username 以及密码 echo -n "my_username: ...

  9. Why service collaboration needs choreography AND orchestration

    转自:https://blog.bernd-ruecker.com/why-service-collaboration-needs-choreography-and-orchestration-239 ...

  10. gogs wekan 集成试用

    wekan 官方提供了一个集成gogs 的扩展,不是完全的自动化,需要结合cli,但是官方的cli 写的...(不是很全) 备注: 测试环境使用docker-compose 环境准备 docker-c ...