先来一个标准分词(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. [LeetCode&Python] Problem 705. Design HashSet

    Design a HashSet without using any built-in hash table libraries. To be specific, your design should ...

  2. 螺旋图 comet3 (comet) 不同轴的圆周运动图

    matlab 绘图 螺旋图小实例  动态显示comet3函数(comet显示平面) t=[:]; x=*t*sin(pi/).*cos(*t); y=*t*sin(pi/).*sin(*t); z=* ...

  3. ZOJ 1002:Fire Net(DFS+回溯)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  4. Arrays ---------------copyOf赋值

  5. ACM-ICPC 2018 北京赛区网络预赛(9.22)

    #include<bits/stdc++.h> using namespace std; ; *maxn]; *maxn]; *maxn]; int main() { int T; sca ...

  6. mysql条件查询-排除null ---oracle、mysql 区分总结

    ' AND IFNULL(c.`COURSE_NO`,'-1') != '-1' ;

  7. git pull refusing to merge unrelated histories

    1. 简介 最近的项目开发环境我换到实验室的机器上了,毕竟是台式机,速度杠杠的.于是出现了一个问题,台式机上面的代码我笔记上的代码同步的问题.于是想到了Git这个强大的工具.关于git的简介我就不说了 ...

  8. java保留2位小数及BigDecimal使用

    java保留两位小数的方法 import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFo ...

  9. Spring事务管理机制的实现原理-动态代理

    之前在做项目中遇到spring无法进行事务代理问题,最后发现是因为没有写接口,原因当时明白了,看到这篇文章写的清楚些,转过来 我们先来分析一下Spring事务管理机制的实现原理.由于Spring内置A ...

  10. python 属性的访问权限,_,__,__XXX__

    1. 非私有变量,可以随意调用和修改 在class内部,有属性和方法,如下面的class Student 有name和score class Student(object): def __init__ ...