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

keyword类型的数据怎么做繁转简

Elasticsearch | 作者 依诺 | 发布于2019年10月29日 | 阅读数:193

背景:搜索 趙又廷、赵又廷,简体或繁体,都能把库里完整匹配赵又廷的用户搜索出来,但是现在我搜繁体的趙又廷,没有结果,这种情况怎么处理?
PUT user
{
"settings": {
"analysis": {
"char_filter": {
"tsconvert": {
"convert_type": "t2s",
"type": "stconvert",
"keep_both": "true",
"delimiter": "#"
}
},
"analyzer": {
"keywordAnalyzer": {
"type": "custom",
"char_filter": [
"tsconvert"
],
"tokenizer": "keyword"
}
}
}
},
"mappings": {
"doc": {
"properties": {
"name": {
"type": "text",
"analyzer": "keywordAnalyzer"
}
}
}
}
}
看网上有用normalizer的,代码如下,但是会报错,mapping建不成功。
PUT user
{
"settings": {
"analysis": {
"normalizer": {
"my_normalizer": {
"type": "custom",
"filter": ["lowercase"],
"char_filter": ["tsconvert"]
}
},
"char_filter": {
"tsconvert": {
"convert_type": "t2s",
"type": "stconvert",
"keep_both": "true",
"delimiter": "#"
}
}
}
},
"mappings": {
"doc": {
"properties": {
"name": {
"type": "keyword",
"normalizer": "my_normalizer"
}
}
}
}
}


报错信息
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Custom normalizer [my_normalizer] may not use char filter [tsconvert]"
}
],
"type": "illegal_argument_exception",
"reason": "Custom normalizer [my_normalizer] may not use char filter [tsconvert]"
},
"status": 400
}
已邀请:

medcl - 今晚打老虎。

赞同来自:

为什么我拿你的例子是可以的呢?
PUT user?include_type_name=true
{
"settings": {
"analysis": {
"char_filter": {
"tsconvert": {
"convert_type": "t2s",
"type": "stconvert",
"keep_both": "true",
"delimiter": "#"
}
},
"analyzer": {
"keywordAnalyzer": {
"type": "custom",
"char_filter": [
"tsconvert"
],
"tokenizer": "keyword"
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"name": {
"type": "text",
"analyzer": "keywordAnalyzer"
}
}
}
}
}

POST user/_doc/
{
"name":"趙又廷"
}

POST user/_doc/
{
"name":"赵又廷"
}

GET /user/_search
{
"query": {
"match": {
"name": "趙又廷"
}
}
}

{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.18232156,
"hits" : [
{
"_index" : "user",
"_type" : "_doc",
"_id" : "XiJwF24B-ihUTsvJX_PS",
"_score" : 0.18232156,
"_source" : {
"name" : "趙又廷"
}
},
{
"_index" : "user",
"_type" : "_doc",
"_id" : "XyJwF24B-ihUTsvJifP5",
"_score" : 0.18232156,
"_source" : {
"name" : "赵又廷"
}
}
]
}
}

依诺

赞同来自:

我昨天试了下  
{ "filter": { "term": { "name": "趙又廷" } }, "weight": 200 }



可以换成
{ "filter": { "match": { "name": "趙又廷" } }, "weight": 200 }


我的问题解决了,谢谢

要回复问题请先登录注册