[英]Elasticsearch Analyzer that ignores hyphen
We are in the process of implementing Elasticsearch
and using .Nest
in a .NET solution. 我们正在实施Elasticsearch
并在.NET解决方案中使用.Nest
。 We've created and loaded an index having several fields. 我们创建并加载了一个包含多个字段的索引。 We wish to define and analyzer that will produce the same search results for an itemNumber field when the user enters UNV-1234
or UNV1234
. 我们希望定义和分析器在用户输入UNV-1234
或UNV1234
时将对itemNumber字段产生相同的搜索结果。 The itemNumber field is limited to item number only with no additional words. itemNumber字段仅限于项目编号,没有其他单词。 However the item number field may have a number separated by a space. 但是,项目编号字段的编号可以用空格分隔。
Based on my searches, it seems that the keyword analyzer would provide the desired results. 根据我的搜索,关键字分析器似乎可以提供所需的结果。 This is not working for us. 这对我们不起作用。
Can someone provide information on how best to accomplish this? 有人可以提供有关如何最好地完成此任务的信息吗?
"itemNumber": {
"type": "string",
"index": "not_analyzed",
"fields": {
"_english": {
"type": "string",
"analyzer": "english"
},
"_keyword": {
"type": "string",
"analyzer": "keyword"
},
"_standard": {
"type": "string",
"analyzer": "standard"
}
}
}
You need to define a custom analyzer with "keyword" tokenizer and a pattern replace token filter to remove any special character and use it for your field. 您需要使用“关键字”令牌生成器和模式替换令牌过滤器定义自定义分析器,以删除任何特殊字符并将其用于您的字段。 The analyzer can be defined as below 分析仪可以定义如下
"analysis" : {
"filter" : {
"cleanspecial": {
"type": "pattern_replace",
"pattern": "[^a-zA-Z0-9]",
"replacement": ""
}
},
"analyzer" : {
"cleanspecialanalyzer": {
"filter": ["cleanspecial"],
"type": "custom",
"tokenizer": "keyword"
}
}
}
Note: Please verify the pattern, I haven't tested it. 注意:请验证模式,我尚未测试。
You can change the mapping as below 您可以如下更改映射
"itemNumber": {
"type": "string",
"index": "not_analyzed",
"fields": {
"_english": {
"type": "string",
"analyzer": "english"
},
"_keyword": {
"type": "string",
"analyzer": "cleanspecialanalyzer"
},
"_standard": {
"type": "string",
"analyzer": "standard"
}
}
}
Search can happen on the field itemNumber._keyword 搜索可以在字段itemNumber._keyword上进行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.