elasticsearch 口水篇(8)分词 中文分词 ik插件
先来一个标准分词(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插件的更多相关文章
- elasticsearch 口水篇(1) 安装、插件
一)安装elasticsearch 1)下载elasticsearch-0.90.10,解压,运行\bin\elasticsearch.bat (windwos) 2)进入http://localho ...
- elasticsearch 口水篇(4)java客户端 - 原生esClient
上一篇(elasticsearch 口水篇(3)java客户端 - Jest)Jest是第三方客户端,基于REST Api进行调用(httpClient),本篇简单介绍下elasticsearch原生 ...
- ElasticSearch简介(三)——中文分词
很多时候,我们需要在ElasticSearch中启用中文分词,本文这里简单的介绍一下方法.首先安装中文分词插件.这里使用的是 ik,也可以考虑其他插件(比如 smartcn). $ ./bin/ela ...
- elasticsearch学习笔记-倒排索引以及中文分词
我们使用数据库的时候,如果查询条件太复杂,则会涉及到很多问题 1.无法维护,各种嵌套查询,各种复杂的查询,想要优化都无从下手 2.效率低下,一般语句复杂了之后,比如使用or,like %,,%查询之后 ...
- elasticsearch 口水篇(9)Facet
FACET 1)Terms Facet { "query" : { "match_all" : { } }, "facets" : { &q ...
- elasticsearch 口水篇(2)CRUD Sense
Sense 为了方便.直观的使用es的REST Api,我们可以使用sense.Sense是Chrome浏览器的一个插件,使用简单. 如图: Sense安装: https://chrome.googl ...
- elasticsearch 口水篇(7) Eclipse中部署ES源码、运行
ES源码可以直接从svn下载 https://github.com/elasticsearch/elasticsearch 下载后,用Maven导入(import——>Existing Mave ...
- elasticsearch 口水篇(6) Mapping 定义索引
前面我们感觉ES就想是一个nosql数据库,支持Free Schema. 接触过Lucene.solr的同学这时可能会思考一个问题——怎么定义document中的field?store.index.a ...
- elasticsearch 口水篇(3)java客户端 - Jest
elasticsearch有丰富的客户端,java客户端有Jest.其原文介绍如下: Jest is a Java HTTP Rest client for ElasticSearch.It is a ...
随机推荐
- (4)MySQL的外键(不同表之间的数据关联)
问题:下列这张表中部门等列名下输入的数据没有约束,那么可以随便填写符合规则的数据但是不符合实际需求的值,这样就造成了不符合规则的数据在表中存在,外键就是为了解决这个问题,管理员可以在另一张表中设置好符 ...
- dijksta 模板
#include<bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f ]; ]; ][]; void dijkstra(i ...
- Example [mybatis] 的用法
example.or() .andField1EqualTo() .andField2IsNull(); example.or() .andField3NotEqualTo() .andField4I ...
- WordPress博客插入直播源
方法很简单: 找到直播源地址,撰写新文章(可视化切换到文本模式下)插入直播源地址 代码:<iframe id="tv_iframe" width="880" ...
- 在Java中,以下关于方法重载和方法重写描述正确的是?
public class TTTTT extends SuperC{ public String get(){ return null; } } class SuperC{ Object get(){ ...
- Spring的依赖注入的2种方式(1天时间)
今天花了一天的时间才调试出来 private 接口 实现类的那个bean; 最后面的那个名字不能随便的写,必须是配置文件中,实现类的那个bean 就是后面的那个名字写错了,花了整整一天 ...
- Java数组逆序排列
//逆序排列原理 /* A: 数组逆序原理* a: 题目分析* 通过观察发现,本题目要实现原数组元素倒序存放操作.即原数组存储元素为{12,69,852,25,89,588},逆序后为原数组存储元素变 ...
- oracle-scn
在2012年第一季度的CPU补丁中,包含了一个关于SCN修正的重要变更,这个补丁提示,在异常情况下,Oracle的SCN可能出现异常增长,使得数据库的一切事务停止,由于SCN不能后退,所以数据库必须重 ...
- CIDR合并
code #include <iostream> #include <list> #include <sstream> #include <vector> ...
- 使用 Composer 查看 FastAdmin 项目 组件的版本
比如查看 ThinkPHP 的版本号. 使用命令 composer show topthink/* 显示如下,可以看到 ThinPHP 版本为 V5.0.12: