1
votes

I'm new with ElasticSearch and encountered some problem while mapping my documents into ES Index. My document structure is

 public class DocumentItem
{
    public string Id { get; set; }
    public DocumentType DocType { get; set; }
    public Dictionary<string, string> Props { get; set; } = new Dictionary<string, string>();

}

And here's my mapping

  var indexResponseFiles = dClient.CreateIndex(sedIndex, c => c
 .InitializeUsing(indexConfig)
 .Mappings(m => m
     .Map<DocumentItem>(mp => mp.AutoMap()
     )
 ));

As u see i'm trying to map a DICTIONARY type. In every document the keys of dictionary are different. My goal is to set my custom analyzer to all text values of dictionary. I have no idea how to do this.

1

1 Answers

1
votes

Dynamic templates feature will help you here. You can configure the dynamic template for all string fields below props object which will create a mapping for such fields with certain analyzer.

Here is the example with creating text fields with english analyzer

var createIndexResponse = await client.CreateIndexAsync("index_name",
    c => c.Mappings(m => m
        .Map<Document>(mm => mm.DynamicTemplates(dt => dt
            .DynamicTemplate("props_fields", t => t
                .PathMatch("props.*")
                .MatchMappingType("string")
                .Mapping(dm => dm.Text(text => text.Analyzer("english"))))))));

and here is the mapping after indexing following document

var document = new Document { Id = "1", Name = "name"};
document.Props.Add("field1", "value");
var indexDocument = await client.IndexDocumentAsync(document);

mapping

{
  "index_name": {
    "mappings": {
      "document": {
        "dynamic_templates": [
          {
            "props_fields": {
              "path_match": "props.*",
              "match_mapping_type": "string",
              "mapping": {
                "analyzer": "english",
                "type": "text"
              }
            }
          }
        ],
        "properties": {
          "id": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "props": {
            "properties": {
              "field1": {
                "type": "text",
                "analyzer": "english"
              }
            }
          }
        }
      }
    }
  }
}

Hope that helps.