简体   繁体   English

ElasticSearch 映射与 NEST 6.6.0

[英]ElasticSearch Mapping With NEST 6.6.0

I'm new with ElasticSearch and encountered some problem while mapping my documents into ES Index.我是 ElasticSearch 的新手,在将文档映射到 ES 索引时遇到了一些问题。 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.如您所见,我正在尝试将 map 定义为 DICTIONARY 类型。 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.我不知道该怎么做。

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.您可以为props object 下的所有字符串字段配置动态模板,这将使用某些分析器为此类字段创建映射。

Here is the example with creating text fields with english analyzer这是使用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"))))))));

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.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM