💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# Mapping Character Filter 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.3/analysis-mapping-charfilter.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/analysis-mapping-charfilter.html) 译文链接 :[http://www.apache.wiki/display/Elasticsearch/Mapping+Character+Filter](http://www.apache.wiki/display/Elasticsearch/Mapping+Character+Filter) 贡献者 : [谢雄](/display/~xiexiong) ,  [ApacheCN](/display/~apachecn) , [Apache中文网](/display/~apachechina) **Mapping Character Filter** 接收键和值映射作为配置参数,每当在预处理过程中遇到与键值映射中的键相同的字符串时,就会使用该键对应的值去替换它。 原始文本中的字符串和键值映射中的键的匹配是贪心的,在对给定的文本进行预处理过程中如果配置的键值映射存在包含关系,会优先匹配最长键。可以用空字符串进行替换。 ## 配置 | 参数名称 | 参数说明 | | --- | --- | | `mappings` | 一组映射,每个元素的格式为key => value。 | | `mappings_path` | 一个相对或者绝对的文件路径,指向一个每行包含一个 key =>value 映射的UTF-8编码文本映射文件。 | 以上两个参数必须提供一个。 在下面的例子中,我们将使用**Mapping Character Filter**来实现用拉丁语数字和阿拉伯数字的等价替换: **案例** ``` PUT my_index { "settings": { "analysis": { "analyzer": { "my_analyzer": { "tokenizer": "keyword", "char_filter": [ "my_char_filter" ] } }, "char_filter": { "my_char_filter": { "type": "mapping", "mappings": [ "٠ => 0", "١ => 1", "٢ => 2", "٣ => 3", "٤ => 4", "٥ => 5", "٦ => 6", "٧ => 7", "٨ => 8", "٩ => 9" ] } } } } } POST my_index/_analyze { "analyzer": "my_analyzer", "text": "My license plate is ٢٥٠١٥" } ``` 上面的案例将会返回如下结果: ``` [ My license plate is 25015 ] ``` 键和值都可以是具有多个字符的字符串。 以下示例用指定的文本替换 :) 和 :( 表情符号: **案例** ``` PUT my_index { "settings": { "analysis": { "analyzer": { "my_analyzer": { "tokenizer": "standard", "char_filter": [ "my_char_filter" ] } }, "char_filter": { "my_char_filter": { "type": "mapping", "mappings": [ ":) => _happy_", ":( => _sad_" ] } } } } } POST my_index/_analyze { "analyzer": "my_analyzer", "text": "I'm delighted about it :(" } ``` 上面的案例将会返回如下结果: ``` [ I'm, delighted, about, it, _sad_ ] ```