在 Elasticsearch 的分析词分析中,normalizer 与 analyzer 类似,分析只是分析它们只能发出一个token。 因此,分析它们没有 tokenizer,分析只接受可用 char filters 和 token filters 的分析子集。 只允许使用基于每个字符的分析过滤器。 例如,分析允许使用 lowercase 过滤器,分析但不允许使用 stemming filter(词干过滤器),分析它需要将关键字视为一个整体。分析 当前可以在规范化器中使用的分析过滤器列表如下:arabic_normalization、asciifolding、分析bengali_normalization、分析cjk_width、分析decimal_digit、elision、german_normalization、hindi_normalization、indic_normalization、lowercase、persian_normalization、scandinavian_folding、serbian_normalization、sorani_normalization、uppercase。
Elasticsearch 附带了一个 lowercase 的内置 normalizer。 对于其他形式的规范化,需要自定义配置。在之前的一个中文简体及繁体的示例中有展示。请参阅文章 “Elasticsearch:简体繁体转换分词器 - STConvert analysis”。
定制 normalizers
如上所述,定制的 normalizer 采用一个或多个 char filtes 和一个或多个 token filters。比如,我们定义如下的一个索引:
PUT my_index{ "settings": { "analysis": { "char_filter": { "quote": { "type": "mapping", "mappings": [ "« =>\"", "» =>\"" ] } }, "normalizer": { "my_normalizer": { "type": "custom", "char_filter": [ "quote" ], "filter": [ "lowercase", "asciifolding" ] } } } }, "mappings": { "properties": { "foo": { "type": "keyword", "normalizer": "my_normalizer" } } }}
在上面,我们在 normalizer 的定义中,我们使用了一个 char filter。它把 « 及 » 字符转换为引号 "。同时它也对字母进行小写及 asciifolding。我们现在以如下的一个文档来进行展示:
PUT my_index/_doc/1{ "foo": "«açaí à la Carte»"}
根据我们上面定义的 normalizer,我们可以看出来上面的字段 foo 有且仅有一个 token:"acai a la carte”,这是因为 « 及 » 字符转换为引号 ",而 “açaí à la Carte” 经过 asciifolding 过滤器后,变为 acai a la Carte。再经过 lowercase 的过滤器,它就变为 "acai a la carte”。
针对上面的索引,我们可以进行如下的搜索:
GET my_index/_search{ "query": { "match": { "foo": "\"acai a la carte\"" } }}
上面返回的结果是:
{ "took": 0, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 0.2876821, "hits": [ { "_index": "my_index", "_id": "1", "_score": 0.2876821, "_source": { "foo": "«açaí à la Carte»" } } ] }}