Elasticsearch NEST:NoopPropertyVisitor为keyword字段加归一器无效
解决方法
问题出在你当前的CustomNormalizerVisitor只处理了顶级的keyword字段,而那些嵌套在text字段fields下的keyword字段并没有被遍历到。要处理这些嵌套的keyword字段,需要在visitor中重写ITextProperty的访问方法,遍历其内部的Fields集合并为其中的keyword字段设置归一器。
改进后的CustomNormalizerVisitor代码
public class CustomNormalizerVisitor : NoopPropertyVisitor { // 处理顶级的keyword字段 public override void Visit(IKeywordProperty type, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute) { type.Normalizer = "customnormalizer"; } // 处理text字段下嵌套的keyword字段 public override void Visit(ITextProperty type, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute) { if (type.Fields != null) { foreach (var field in type.Fields.Values) { if (field is IKeywordProperty keywordField) { keywordField.Normalizer = "customnormalizer"; } } } // 调用基类方法保持text字段的默认映射行为 base.Visit(type, propertyInfo, attribute); } }
验证效果
重新创建索引后,查看映射会发现嵌套的keyword字段已应用自定义归一器:
"assetSourceId": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256, "normalizer": "customnormalizer" } } }
这样既保留了text和keyword字段的共存,又为所有keyword字段(包括嵌套的)统一应用了自定义归一器。
内容的提问来源于stack exchange,提问作者Avatar
相关产品推荐
相关产品推荐

