我们有一个文本字段,其中包含带名字缩写的人物姓名,这些名字缩写不一致(某些地方用空格/点分隔,有些地方则不一样)。
例如:-G.J.拉贾(Raja) J. Raja,GJ Raja,G J Raja ...
我尝试了以下解决方案,但无法获得预期的解决方案
输入:-G J Raja
输出:-G.J.拉贾(Raja) J. Raja,GJ Raja,G J Raja
答案 0 :(得分:4)
使用pattern_replace
token filter,可以实现您想要的。以下名为initial_analyzer
的分析器将清理您的姓名,并确保将所有姓名都转换为GJ Raja
PUT test
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"initial_analyzer": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"initials"
]
}
},
"filter": {
"initials": {
"type": "pattern_replace",
"pattern": """[\.\s]*([A-Z])[\.\s]*([A-Z])[\.\s]*(\w+)""",
"replacement": "$1$2 $3"
}
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "initial_analyzer"
}
}
}
}
然后我们可以为一些文档建立索引
PUT test/_bulk
{"index": {}}
{"name": "G.J. Raja"}
{"index":{}}
{"name":"G . J . Raja"}
{"index": {}}
{"name":"GJ Raja"}
{"index":{}}
{"name":"G J Raja"}
最后,以下查询将找到所有四个不同的名称(以及其他变体)。您也可以搜索G. J. Raja
或G. J Raja
,所有四个文档都将匹配:
POST test/_search
{
"query": {
"match": {
"name": "G J Raja"
}
}
}
结果:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 0.18232156,
"hits" : [
{
"_index" : "test",
"_type" : "doc",
"_id" : "Z7pF7WwBvUQmOB95si05",
"_score" : 0.18232156,
"_source" : {
"name" : "G . J . Raja"
}
},
{
"_index" : "test",
"_type" : "doc",
"_id" : "aLpF7WwBvUQmOB95si05",
"_score" : 0.18232156,
"_source" : {
"name" : "GJ Raja"
}
},
{
"_index" : "test",
"_type" : "doc",
"_id" : "ZrpF7WwBvUQmOB95si05",
"_score" : 0.18232156,
"_source" : {
"name" : "G.J. Raja"
}
},
{
"_index" : "test",
"_type" : "doc",
"_id" : "abpF7WwBvUQmOB95si05",
"_score" : 0.18232156,
"_source" : {
"name" : "G J Raja"
}
}
]
}
}