bobwah
bobwah

Reputation: 2568

Adding normalizer for all keyword fields NEST

I am able to set up a normalizer on a keyword mapping in NEST using the following:

   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")
              )
            )
          )
        )
      )

Is there a way to add the normalizer across all keyword fields for a specified mapping without declaring all of the fields? I have looked into the property visitor pattern and used AutoMap however I didn't have much luck as anything set in these seems to be overwritten, perhaps this isn't the right place to do this?

Upvotes: 1

Views: 1750

Answers (1)

Rob
Rob

Reputation: 9979

One of the options is to use dynamic template which will create keyword mapping with specified normalizer for all strings

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")))))));

indexing this document

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

will produce the following index mapping

{
  "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"
        }
      }
    }
  }
}

Hope that helps.

Upvotes: 4

Related Questions