ElasticSearch5.X IK分词器使用

IK分词器使用

首先确保IK插件安装成功(安装文档:http://blog.csdn.net/wwd0501/article/details/78258274),然后在创建mapping时,设置IK分词器,设置analyzer和search_analyzer;在java api搜索中将不用再关注IK分词器的事情,原有代码可以不做任何修改。例:

1.create a index

curl -XPUT http://localhost:9200/class

2.create a mapping

curl -XPOST http://localhost:9200/class/student/_mapping -d'
{
    "student": {
        "properties": {
            "name": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_smart"
            },
            "age": {
                "type": "long"
            }
        }
    }
}'

3.index some docs

curl -XPOST http://localhost:9200/class/student/1 -d'
{"name":"菠菜","age":"10"}
'
curl -XPOST http://localhost:9200/class/student/2 -d'
{"name":"芹菜","age":"65"}'
curl -XPOST http://localhost:9200/class/student/3 -d'
{"name":"大萝卜 大菠菜","age":"89"}
'

4.query

curl -XPOST http://localhost:9200/class/student/_search  -d'
{
    "query" : { "match" : { "name" : "芹菜" } }
}
'

Result

{
	"took":  3,
	"timed_out":  false,
	"_shards":  {
		"total":  5,
		"successful":  5,
		"failed":  0
	},
	"hits":  {
		"total":  1,
		"max_score":  0.25316024,
		"hits":  [{
			"_index":   "class",
			"_type":   "student",
			"_id":   "1",
			"_score":  0.25316024,
			"_source":  {
				"name":   "芹菜",
				"age":   "10"
			}
		}]
	}
}

注意:

ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;

ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。

其中我们在做索引的时候,希望能将所有的句子切分的更详细,以便更好的搜索,所以ik_max_word更多的用在做索引的时候,但是在搜索的时候,对于用户所输入的query(查询)词,我们可能更希望得比较准确的结果,例如,我们搜索“无花果”的时候,更希望是作为一个词进行查询,而不是切分为"无",“花”,“果”三个词进行结果的召回,因此ik_smart更加常用语对于输入词的分析

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值