Elasticsearch NEST使用NoopPropertyVisitor在所有关键字字段上添加规范器

nwnhqdif  于 2022-12-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(164)

我正在尝试在所有关键字字段上添加自定义规范化程序。我正在使用NoonPropertyVisitor重写:

public class CustomNormalizerVisitor : NoopPropertyVisitor
    {
        public override void Visit(IKeywordProperty type, PropertyInfo propertyInfo,  ElasticsearchPropertyAttributeBase attribute)
        {
            type.Normalizer = "customnormalizer";
        }
    }

然后我在这里使用它:

CustomNormalizerVisitor visitor = new CustomNormalizerVisitor();

        client.Indices.Create(indexName, i => i
        .Settings(s => s
            .Analysis(a => a
                .Normalizers(n => n.
                    Custom("customnormalizer", cn => cn
                     .Filters(new string[] { "lowercase", "asciifolding" })))))
        .Map<ELSEntity>(m => m
         .AutoMap(visitor)
             .Properties(p => p
                 .Nested<FieldValue>(ne => ne.Name(n => n.Fields).Enabled(false)))));

但是当我继续Map的时候,我没有看到规范器应用在关键字字段上。我用type.DocValues = false测试了IBooleanProperty及其工作。
这是Map:

{
  "elsentity": {
    "mappings": {
      "properties": {
        "assets": {
          "properties": {
            "active": {
              "type": "boolean",
              "doc_values": false
            },
            "assetCreationDate": {
              "type": "date"
            },
            "assetSourceId": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "assetStatusId": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "assetType": {
              "type": "integer"
            },
            "categoryId": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "default": {
              "type": "boolean",
              "doc_values": false
            },
            "id": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },

不确定,也许是因为关键字在字段下?我试图覆盖关键字上的不同属性,但不起作用。我需要文本和关键字都在字符串字段上,因为不同的查询。

qcuzuvrc

qcuzuvrc1#

我想我找到了解决方案。在NooPPropertyVisitor中,使用type.Fields,我们可以添加关键字字段并设置Normalizer,另外在文本字段上,我们可以设置Analyzer,这是它的外观:

public class CustomNormalizerVisitor : NoopPropertyVisitor
{
    public override void Visit(ITextProperty type, PropertyInfo propertyInfo,  ElasticsearchPropertyAttributeBase attribute)
    {
        Dictionary<PropertyName, IProperty> container = new Dictionary<PropertyName, IProperty>();
        
        container.Add(new PropertyName("keyword"), new KeywordProperty() { Normalizer = "customnom"});
       
        type.Analyzer = "custom";
        
        type.Fields = new Nest.Properties(container);
        
    }
}

相关问题