为所有关键字字段NEST添加归一化器

问题描述 投票:0回答:1

我可以使用以下方法在NEST中的关键字映射上设置规范化器:

   client.Indices.Create(indexName, c => c
        .Map<Item>(m => m.Properties(ps => ps
          .Text(s => s
            .Name(new PropertyName("someProp"))
            .Fields(f => f
              .Keyword(kw => kw
                .Name("keyword")
                .Normalizer("my_normalizer")
              )
            )
          )
        )
      )

是否有一种方法可以在不声明所有字段的情况下跨指定映射的所有关键字字段添加规范化器?我已经研究了属性访问者模式并使用了AutoMap,但是我运气不佳,因为其中设置的任何内容似乎都被覆盖了,也许这不是执行此操作的正确位置?

c# elasticsearch nest
1个回答
0
投票

其中一个选项是使用dynamic template,它将使用指定的规范化器为所有字符串创建关键字映射

var createIndexResponse = await client.Indices.CreateAsync("my_index", c => c
    .Settings(s => s.Analysis(a => a
        .Normalizers(n => n.Custom("lowercase", cn => cn.Filters("lowercase")))))
    .Map(m => m.DynamicTemplates(dt => dt.DynamicTemplate("string_to_keyword", t => t
        .MatchMappingType("string")
        .Mapping(map => map.Keyword(k => k.Normalizer("lowercase")))))));

索引此文档

var indexDocumentAsync = await client.IndexDocumentAsync(new Document {Id = 1, Name = "name"});

将产生以下索引映射

{
  "my_index": {
    "mappings": {
      "dynamic_templates": [
        {
          "string_to_keyword": {
            "match_mapping_type": "string",
            "mapping": {
              "normalizer": "lowercase",
              "type": "keyword"
            }
          }
        }
      ],
      "properties": {
        "id": {
          "type": "long"
        },
        "name": {
          "type": "keyword",
          "normalizer": "lowercase"
        }
      }
    }
  }
}

希望有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.