疲劳是最舒适的枕头,努力工作吧。

请教汉字同音词pinyin分词器搜索问题

Elasticsearch | 作者 dotNetDR_ | 发布于2018年04月27日 | 阅读数:3616

比如 [汉字, 汗渍] 这两个词用pinyin分词得出 [han, zi]
 
当业务应用搜索 [汉子] 时,search_analyzer分词器得出的pinyin也是[han, zi]
 
此时匹配到的是 [汉字, 汗渍] 。这并不符合我的业务需求。
 
请教怎么解决pinyin分词器在遭遇汉字时仍然原样保留汉字,遭遇字母时切割拼音?
例如:
hanzi => [han, zi]
汉子 => [汉子]

中文feichangbucuo => [中文, fei, chang, bu, cuo]
还是说在应用里检测输入,发现搜索关键词存在汉字时,搜索条件不要使用 myField.pinyin 这一列?如果这样子做了,那么汉字跟拼音的混搜有什么好办法?例如:  中guo, xue习等
 
--- updated by 2018-04-27 19:41 ---
 
建立索引
PUT /mypinyin
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"pinyin_analyzer": {
"type": "custom",
"tokenizer": "pinyin_tokenizer"
}
},
"tokenizer": {
"pinyin_tokenizer": {
"type": "pinyin",
"keep_first_letter": false
}
}
}
}
},
"mappings": {
"doc": {
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word",
"fields": {
"pinyin": {
"type": "text",
"analyzer": "pinyin_analyzer"
}
}
}
}
}
}
}

插入2个文档
put mypinyin/doc/1
{
"name": "汉字"
}

put mypinyin/doc/2
{
"name": "汗渍"
}

搜索同音字
GET mypinyin/doc/_search
{
"query": {
"match": {
"name.pinyin": {
"query": "韩资",
"operator": "and"
}
}
}
}

// 结果
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.51623213,
"hits": [
{
"_index": "mypinyin",
"_type": "doc",
"_id": "2",
"_score": 0.51623213,
"_source": {
"name": "汗渍"
}
},
{
"_index": "mypinyin",
"_type": "doc",
"_id": "1",
"_score": 0.51623213,
"_source": {
"name": "汉字"
}
}
]
}
}

搜索[韩资]时,分词器得到了[han, zi]所以能匹配到文档,这样其实不是很符合用户期望的,我希望搜索[韩资]时返回空,搜索[hanzi]时返回id1,id2文档。
 
 
 
 
 
已邀请:

hnj1575565068 - 90后

赞同来自:

设置自定义分词器,自定义分词器中拼音分词+ik分词组合,拼音分词参考这个:https://github.com/medcl/elast ... inyin
参数可以自己设置,分词后保留中文,mapping中analyzer用custom_analyzer,search_analyzer用ik_smart,输中文可以正常搜

要回复问题请先登录注册