【问题标题】:ElasticSearch Mapping With NEST 6.6.0ElasticSearch 与 NEST 6.6.0 的映射
【发布时间】:2020-01-29 11:46:36
【问题描述】:

我是 ElasticSearch 的新手,在将文档映射到 ES 索引时遇到了一些问题。 我的文档结构是

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

}

这是我的地图

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

如您所见,我正在尝试映射 DICTIONARY 类型。在每个文档中,字典的键都是不同的。 我的目标是将我的自定义分析器设置为字典的所有文本值。我不知道该怎么做。

【问题讨论】:

    标签: c# nest


    【解决方案1】:

    Dynamic templates 功能将在这里为您提供帮助。您可以为props 对象下的所有字符串字段配置动态模板,这将使用特定分析器为此类字段创建映射。

    这是使用english 分析器创建文本字段的示例

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

    这是索引以下文档后的映射

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

    映射

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

    希望对您有所帮助。

    【讨论】:

    • 非常感谢,罗布。这似乎是一个解决方案))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 2015-12-01
    • 2016-06-21
    相关资源
    最近更新 更多