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更加常用语对于输入词的分析