IK分词器应用

本文详细介绍了IK分词器的特性,包括IK_max_word和IK_smart两种分词策略,并展示了如何在Elasticsearch中集成和使用IK分词器进行索引创建、数据插入及查询操作。通过示例演示了term查询和query_string查询的不同效果。
摘要由CSDN通过智能技术生成

本篇博客的主要目的是介绍IK分词器与ES的集成使用.

  1. IK分词器简介
    IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。从2006年12月推出1.0版开始,IKAnalyzer已经推出 了3个大版本。最初,它是以开源项目Lucene为应用主体的,结合词

典分词和文法分析算法的中文分词组件。新版本的IKAnalyzer3.0则发展为 面向Java的公用分词组件,独立于Lucene项目,同时提供了对Lucene的默认优化实现。

IK分词器3.0的特性如下:

1)采用了特有的“正向迭代最细粒度切分算法“,具有60万字/秒的高速处理能力。

2)采用了多子处理器分析模式,支持:英文字母(IP地址、Email、URL)、数字(日期,常用中文数量词,罗马数字,科学计数法),中文词汇(姓名、地名处理)等分词处理。

3)对中英联合支持不是很好,在这方面的处理比较麻烦.需再做一次查询,同时是支持个人词条的优化的词典存储,更小的内存占用。

4)支持用户词典扩展定义。 5)针对Lucene全文检索优化的查询分析器IKQueryParser;采用歧义分析算法优化查询关键字的搜索排列组合,能极大的提高Lucene检索的命中率。

  1. IK分词器搭建
    https://www.cnblogs.com/sjfxwj/p/14547402.html

  2. IK分词器测试使用
    IK提供了两个分词算法ik_smart 和 ik_max_word,其中 ik_smart 为最少切分,ik_max_word为最细粒度划分。

  3. ik_max_word
    输入数据:

curl -XGET “http://192.168.140.101:9200/_analyze?pretty” -H ‘Content-Type: application/json’ -d’
{
“text”:“联想是全球最大的笔记本厂商”,“tokenizer”: “ik_max_word”
}’
运行结果:

[root@s101 /usr/local/software/elasticsearch-head]#curl -XGET “http://192.168.140.101:9200/_analyze?pretty” -H ‘Content-Type: application/json’ -d’

{
“text”:“联想是全球最大的笔记本厂商”,“tokenizer”: “ik_max_word”
}’
{
“tokens” : [
{
“token” : “联想”,
“start_offset” : 0,
“end_offset” : 2,
“type” : “CN_WORD”,
“position” : 0
},
{
“token” : “是”,
“start_offset” : 2,
“end_offset” : 3,
“type” : “CN_CHAR”,
“position” : 1
},
{
“token” : “全球”,
“start_offset” : 3,
“end_offset” : 5,
“type” : “CN_WORD”,
“position” : 2
},
{
“token” : “最大”,
“start_offset” : 5,
“end_offset” : 7,
“type” : “CN_WORD”,
“position” : 3
},
{
“token” : “的”,
“start_offset” : 7,
“end_offset” : 8,
“type” : “CN_CHAR”,
“position” : 4
},
{
“token” : “笔记本”,
“start_offset” : 8,
“end_offset” : 11,
“type” : “CN_WORD”,
“position” : 5
},
{
“token” : “笔记”,
“start_offset” : 8,
“end_offset” : 10,
“type” : “CN_WORD”,
“position” : 6
},
{
“token” : “本厂”,
“start_offset” : 10,
“end_offset” : 12,
“type” : “CN_WORD”,
“position” : 7
},
{
“token” : “本”,
“start_offset” : 10,
“end_offset” : 11,
“type” : “CN_CHAR”,
“position” : 8
},
{
“token” : “厂商”,
“start_offset” : 11,
“end_offset” : 13,
“type” : “CN_WORD”,
“position” : 9
}
]
}

  1. ik_smart
    输入数据:

curl -XGET “http://192.168.140.101:9200/_analyze?pretty” -H ‘Content-Type: application/json’ -d’
{
“text”:“联想是全球最大的笔记本厂商”,“tokenizer”: “ik_smart”
}’
运行结果:

[root@s101 /usr/local/software/elasticsearch-head]#curl -XGET “http://192.168.140.101:9200/_analyze?pretty” -H ‘Content-Type: application/json’ -d’

{
“text”:“联想是全球最大的笔记本厂商”,“tokenizer”: “ik_smart”
}’
{
“tokens” : [
{
“token” : “联想”,
“start_offset” : 0,
“end_offset” : 2,
“type” : “CN_WORD”,
“position” : 0
},
{
“token” : “是”,
“start_offset” : 2,
“end_offset” : 3,
“type” : “CN_CHAR”,
“position” : 1
},
{
“token” : “全球”,
“start_offset” : 3,
“end_offset” : 5,
“type” : “CN_WORD”,
“position” : 2
},
{
“token” : “最大”,
“start_offset” : 5,
“end_offset” : 7,
“type” : “CN_WORD”,
“position” : 3
},
{
“token” : “的”,
“start_offset” : 7,
“end_offset” : 8,
“type” : “CN_CHAR”,
“position” : 4
},
{
“token” : “笔记本”,
“start_offset” : 8,
“end_offset” : 11,
“type” : “CN_WORD”,
“position” : 5
},
{
“token” : “厂商”,
“start_offset” : 11,
“end_offset” : 13,
“type” : “CN_WORD”,
“position” : 6
}
]
}

  1. IK分词器和ES的集成使用
    我们先创建索引:

curl -XPUT ‘http://192.168.140.101:9200/blog?pretty’

curl -H “Content-Type: application/json” -XPUT http://192.168.140.101:9200/blog/article/_mapping -d ‘{
“properties”: {
“id”: {
“type”: “long”,
“store”: true,
“index”: false
},
“title”: {
“type”: “text”,
“store”: true,
“index”: true,
“analyzer”: “ik_smart”
},
“content”: {
“type”: “text”,
“store”: true,
“index”: true,
“analyzer”: “ik_smart”
}
}
}’
随后我们向该索引当中插入三条数据:

curl -H “Content-Type: application/json” -XPUT ‘http://192.168.140.101:9200/blog/article/1?pretty’ -d ‘{
“id”: 1,
“title”:“明日起70岁退休时代来临 日本终于迈出这一步”,
“content”:“辛巴复出直播当天周围路被封了 街道回应”
}’

curl -H “Content-Type: application/json” -XPUT ‘http://192.168.140.101:9200/blog/article/2?pretty’ -d ‘{
“id”: 2,
“title”:“海南省市场监管局对椰树涉嫌违法广告进行立案调查”,
“content”:“小米官宣造车 “赌”上全部的雷军这次会赢吗?”
}’

curl -H “Content-Type: application/json” -XPUT ‘http://192.168.140.101:9200/blog/article/3?pretty’ -d ‘{
“id”: 3,
“title”:“韩国禁止对朝发传单 却被美国列为重大人权问题”,
“content”:“辛巴复出直播当天周围路被封了 街道回应”
}’
运行结果:

查询文档方式2-term关键词在倒排索引当中进行查询(关键词不分词):

curl -H “Content-Type: application/json” -XGET ‘http://192.168.140.101:9200/blog/article/_search/?pretty’ -d ‘{
“query”: {
“term”: {
“title”: “明日”
}
}
}’
运行结果:

[root@s101 /usr/local/software/elasticsearch-head]#curl -H “Content-Type: application/json” -XGET ‘http://192.168.140.101:9200/blog/article/_search/?pretty’ -d '{

"query": {
    "term": {
        "title": "明日"
    }
}

}’
{
“took” : 25,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 1,
“max_score” : 0.2876821,
“hits” : [
{
“_index” : “blog”,
“_type” : “article”,
“_id” : “1”,
“_score” : 0.2876821,
“_source” : {
“id” : 1,
“title” : “明日起70岁退休时代来临 日本终于迈出这一步”,
“content” : “辛巴复出直播当天周围路被封了 街道回应”
}
}
]
}
} 
查询文档方式3-使用query_string字符串进行分析查询(字符串–>分词器分词–>分词的结果在倒排索引当中进行查询):

curl -H “Content-Type: application/json” -XGET ‘http://192.168.140.101:9200/blog/article/_search/?pretty’ -d ‘{
“query”: {
“query_string”: {
“default_field”: “title”,
“query”: “日本和美国”
}
}
}’
运行结果:

[root@s101 /usr/local/software/elasticsearch-head]#curl -H “Content-Type: application/json” -XGET ‘http://192.168.140.101:9200/blog/article/_search/?pretty’ -d '{

"query": {
    "query_string": {
        "default_field": "title",
        "query": "日本和美国"
    }
}

}’
{
“took” : 9,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 2,
“max_score” : 0.2876821,
“hits” : [
{
“_index” : “blog”,
“_type” : “article”,
“_id” : “1”,
“_score” : 0.2876821,
“_source” : {
“id” : 1,
“title” : “明日起70岁退休时代来临 日本终于迈出这一步”,
“content” : “辛巴复出直播当天周围路被封了 街道回应”
}
},
{
“_index” : “blog”,
“_type” : “article”,
“_id” : “3”,
“_score” : 0.2876821,
“_source” : {
“id” : 3,
“title” : “韩国禁止对朝发传单 却被美国列为重大人权问题”,
“content” : “辛巴复出直播当天周围路被封了 街道回应”
}
}
]
}
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只懒得睁眼的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值