映射 - ElasticSearch [7+]

Posted

技术标签:

【中文标题】映射 - ElasticSearch [7+]【英文标题】:Mapping - ElasticSearch [7+] 【发布时间】:2019-09-27 15:24:01 【问题描述】:

我已经解决了很多这个问题我现在想知道问题是什么以及我可能做错了什么。

我打算使用 NEST 重新创建的控制台查询是:

    
  "tdindex" : 
    "mappings" : 
      "documentModel" : 
        "properties" : 
          "accessControl" : 
            "type" : "boolean",
            "copy_to" : [
              "copyTo"
            ]
          ,
          "comments" : 
            "properties" : 
              "comment" : 
                "type" : "text",
                 "fields" : 
              "keyword" : 
                "type" : "keyword",
                "ignore_above" : 256
              
            ,
                "copy_to" : [
                  "copyTo"
                ],
              "createDate" : 
                "type" : "date",
 "fields" : 
              "keyword" : 
                "type" : "keyword",
                "ignore_above" : 256
              
            ,
                "copy_to" : [
                  "copyTo"
                ],
              "user" : 
                "type" : "text",
                 "fields" : 
              "keyword" : 
                "type" : "keyword",
                "ignore_above" : 256
              
            ,
                "copy_to" : [
                  "copyTo"
                ]
              
            
          ,
          "copyTo" : 
            "type" : "text"
          ,
          "documentType" : 
            "type" : "text",
            "fields" : 
              "keyword" : 
                "type" : "keyword",
                "ignore_above" : 256
              
            ,
            "copy_to" : [
              "copyTo"
            ]
          ,
          "filename" : 
            "type" : "text",
            "fields" : 
              "keyword" : 
                "type" : "keyword",
                "ignore_above" : 256
              
            ,
            "copy_to" : [
              "copyTo"
            ]
          ,
          "folderID" : 
            "type" : "long",
            "copy_to" : [
              "copyTo"
            ]
          ,
          "metadata" : 
            "properties" : 
              "key" : 
                "type" : "text",
                "fields" : 
                  "keyword" : 
                    "type" : "keyword",
                    "ignore_above" : 256
                  
                ,
                "copy_to" : [
                  "copyTo"
                ]
              ,
              "value" : 
                "type" : "text",
                "fields" : 
                  "keyword" : 
                    "type" : "keyword",
                    "ignore_above" : 256
                  
                ,
                "copy_to" : [
                  "copyTo"
                ]
              
            
          ,
          "uploadDate" : 
            "type" : "date",
            "copy_to" : [
              "copyTo"
            ]
          ,
          "uploadUser" : 
            "type" : "text",
            "fields" : 
              "keyword" : 
                "type" : "keyword",
                "ignore_above" : 256
              
            ,
            "copy_to" : [
              "copyTo"
            ]
          
        
      
    
  

我的巢目前正在创建以下控制台查询:

  
  "mappings": 
    "properties": 
      "folderid": 
        "copy_to": [
          "CopyTo"
        ],
        "fields": 
          "keyword": 
            "ignore_above": 256,
            "type": "keyword"
          
        ,
        "type": "text"
      ,
      "filename": 
        "copy_to": [
          "CopyTo"
        ],
        "fields": 
          "keyword": 
            "ignore_above": 256,
            "type": "keyword"
          
        ,
        "type": "text"
      ,
      "documenttype": 
        "copy_to": [
          "CopyTo"
        ],
        "fields": 
          "keyword": 
            "ignore_above": 256,
            "type": "keyword"
          
        ,
        "type": "text"
      ,
      "uploaddate": 
        "copy_to": [
          "CopyTo"
        ],
        "fields": 
          "keyword": 
            "ignore_above": 256,
            "type": "keyword"
          
        ,
        "type": "text"
      ,
      "uploaduser": 
        "copy_to": [
          "CopyTo"
        ],
        "fields": 
          "keyword": 
            "ignore_above": 256,
            "type": "keyword"
          
        ,
        "type": "text"
      ,
      "comments": 
        "properties": 
          "createdate": 
            "type": "date"
          ,
          "comment": 
            "type": "text"
          ,
          "user": 
            "type": "text"
          
        ,
        "copy_to": [
          "CopyTo"
        ],
        "type": "nested"
      ,
      "metadata": 
        "properties": 
          "key": 
            "type": "text"
          ,
          "value": 
            "type": "text"
          
        ,
        "copy_to": [
          "CopyTo"
        ],
        "type": "nested"
      ,
      "CopyTo": 
        "type": "text"
      
    
  

如您所见,它忽略了 DocumentModel……

我使用的 NEST 代码是:

_elasticClient.CreateIndex(indexParameters.IndexName, c =>
            
                c.Map<DocumentModel>(m => m

                   .Properties(ps => ps

                                      .Text(t => t.Name(n => n.FolderID).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
                      .Text(t => t.Name(n => n.Filename).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
                      .Text(t => t.Name(n => n.DocumentType).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
                      .Text(t => t.Name(n => n.UploadDate).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
                      .Text(t => t.Name(n => n.uploadUser).CopyTo(n => n.Field(f => f.CopyTo)).Fields(fd => fd.Keyword(k => k.Name("keyword").IgnoreAbove(256))))
                      .Nested<Comments>(cm => cm.Name(n => n.Comments).AutoMap().CopyTo(n => n.Field(f => f.CopyTo)))
                      .Nested<Metadata>(md => md.Name(n => n.Metadata).AutoMap().CopyTo(n => n.Field(f => f.CopyTo)))
                      .Text(t => t.Name(n => n.CopyTo)))

我的 DocumentModel 看起来像这样(我不能在模型中使用 [ElasticsearchType(Name = "documentModel")] 不确定这是否是问题的一部分?):

public class DocumentModel

    [Text(Name = "filename")]
    public string Filename  get; set; 
    [Text(Name = "folderid")]
    public int FolderID  get; set; 
    [Text(Name = "uploaduser")]
    public string uploadUser  get; set; 
    [Date(Format = "MMddyyyy")]
    public DateTime UploadDate  get; set; 
    [Text(Name = "documenttype")]
    public string DocumentType  get; set; 
    [Boolean(NullValue = false)]
    public bool AccessControl  get; set; 
    [Nested]
    public List<Comments> Comments  get; set; 
    [Nested]
    public List<Metadata> Metadata  get; set; 
    [Text(Name = "CopyTo")]
    public string CopyTo  get; set; 




public class Comments

    [Date(Format = "MMddyyyy")]
    public DateTime CreateDate  get; set; 
    [Text(Name = "comment")]
    public string Comment  get; set; 
    [Text(Name = "user")]
    public string User  get; set; 


public class Metadata

    [Text(Name = "key")]
    public string Key  get; set; 
    [Text(Name = "value")]
    public string Value  get; set; 

我在运行查询时从控制台得到的错误是:


  "error": 
    "root_cause": [
      
        "type": "mapper_parsing_exception",
        "reason": "Root mapping definition has unsupported parameters:  [metadata : copy_to=[CopyTo], type=nested, properties=value=type=text, key=type=text] [filename : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text] [comments : copy_to=[CopyTo], type=nested, properties=createdate=type=date, comment=type=text, user=type=text] [uploaddate : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text] [CopyTo : type=text] [documenttype : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text] [folderid : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text] [uploaduser : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text]"
      
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping [properties]: Root mapping definition has unsupported parameters:  [metadata : copy_to=[CopyTo], type=nested, properties=value=type=text, key=type=text] [filename : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text] [comments : copy_to=[CopyTo], type=nested, properties=createdate=type=date, comment=type=text, user=type=text] [uploaddate : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text] [CopyTo : type=text] [documenttype : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text] [folderid : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text] [uploaduser : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text]",
    "caused_by": 
      "type": "mapper_parsing_exception",
      "reason": "Root mapping definition has unsupported parameters:  [metadata : copy_to=[CopyTo], type=nested, properties=value=type=text, key=type=text] [filename : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text] [comments : copy_to=[CopyTo], type=nested, properties=createdate=type=date, comment=type=text, user=type=text] [uploaddate : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text] [CopyTo : type=text] [documenttype : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text] [folderid : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text] [uploaduser : copy_to=[CopyTo], fields=keyword=ignore_above=256, type=keyword, type=text]"
    
  ,
  "status": 400

有什么想法吗?

我认为问题是由于地图引起的?

附:我在 localhost 上运行 Kibana 6.7.1 和 Elastic 6.7.1。我正在运行查询(来自使用 NEST 的请求创建的 JSON 对象)

【问题讨论】:

您使用的是哪个版本的 Nest?当前版本尚不支持 Elastic 7,但如果您在 Nuget 中启用预发布版本,则可以使用 alpha。您可以在此处查看支持的版本:github.com/elastic/elasticsearch-net 原来我正在使用 NEST 7.0.0-aplha...我已经设法让控制台查询看起来更接近我想要的但仍然没有乐趣...我一定会如果我取得任何进展,请更新这篇文章......干杯 至于缺少的“documentModel”键:默认情况下,ES 7.0 不允许通过默认 include_type_name 查询参数为 false 来指定文档类型。见elastic.co/guide/en/elasticsearch/reference/master/…。您的实际问题可能与您如何将映射传递给 Elasticsearch 有关。您需要更准确地理解“运行查询时我从控制台得到的错误”的意思——什么控制台?什么端点?你是如何运行查询的? 我刚刚在我的集群上试过了;嵌套为您生成的映射实际上是正确的,并按预期创建了一个索引。查看elastic.co/guide/en/elasticsearch/reference/current/… 和elastic.co/guide/en/elasticsearch/reference/current/… 的示例,了解您应该如何向 ES 提出请求。这是一个巢虫的可能性很小。回复:“除非你能帮助解决问题,否则不要发帖”:作为对等,除非你准备好为网站做出贡献,否则不要在 *** 上提问。 @Backgammon 我认为它在 NEST 7.x(弹性)中贬值了,Nest 现在声明“CreateIndexDescriptor.Mapings(Func)' 已过时:'映射不再是字典在 7.x 中,请使用简化的 Map()..." 找到新语法很痛苦 【参考方案1】:

对于 Elasticsearch 6.7.1,请使用最新的 6.x NEST 客户端,is 6.7.0 at this time。客户端的主要版本与 Elasticsearch 的主要版本兼容。

使用 NEST 6.7.0,映射类似于

private static void Main()

    var defaultIndex = "tdindex";
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex);

    var client = new ElasticClient(settings);

    var visitor = new MyVisitor();  

    client.CreateIndex(defaultIndex, c => c
        .Mappings(m => m
            .Map<DocumentModel>(mm => mm
                .AutoMap(visitor)
                .Properties(ps => ps
                    .Nested<Comments>(cm => cm
                        .Name(n => n.Comments)
                        .AutoMap(visitor)
                    )
                    .Nested<Metadata>(md => md
                        .Name(n => n.Metadata)
                        .AutoMap(visitor)
                    )
                    .Text(t => t.Name(n => n.CopyTo))
                )
            )
        )
    );



public class MyVisitor : NoopPropertyVisitor

    public override void Visit(ITextProperty property, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute)
    
        base.Visit(property, propertyInfo, attribute);
        property.CopyTo = Infer.Fields<DocumentModel>(f => f.CopyTo);
        property.Fields = new Properties
        
             "keyword", new KeywordProperty  IgnoreAbove = 256   
        ;
    


[ElasticsearchType(Name = "documentModel")]
public class DocumentModel

    [Text(Name = "filename")]
    public string Filename  get; set; 
    [Text(Name = "folderid")]
    public int FolderID  get; set; 
    [Text(Name = "uploaduser")]
    public string uploadUser  get; set; 
    [Date(Format = "MMddyyyy")]
    public DateTime UploadDate  get; set; 
    [Text(Name = "documenttype")]
    public string DocumentType  get; set; 
    [Boolean(NullValue = false)]
    public bool AccessControl  get; set; 
    [Nested]
    public List<Comments> Comments  get; set; 
    [Nested]
    public List<Metadata> Metadata  get; set; 
    [Text(Name = "copyTo")]
    public string CopyTo  get; set; 


public class Comments

    [Date(Format = "MMddyyyy")]
    public DateTime CreateDate  get; set; 
    [Text(Name = "comment")]
    public string Comment  get; set; 
    [Text(Name = "user")]
    public string User  get; set; 


public class Metadata

    [Text(Name = "key")]
    public string Key  get; set; 
    [Text(Name = "value")]
    public string Value  get; set; 

由于除了CopyTo 属性之外的所有文本映射都有关键字multi_fields 和copy_to 来复制到CopyTo 字段,这是define this is with a visitor 的最简单方法。首先,调用Automap(),传递访问者。自动映射将获取模型上的属性映射,访问者上的访问方法将允许我们覆盖其中的任何一个。接下来,Properties() 将覆盖自动映射过程中的所有映射。

最终的输出映射为

PUT http://localhost:9200/tdindex?pretty=true 

  "mappings": 
    "documentModel": 
      "properties": 
        "filename": 
          "type": "text",
          "copy_to": [
            "copyTo"
          ],
          "fields": 
            "keyword": 
              "type": "keyword",
              "ignore_above": 256
            
          
        ,
        "folderid": 
          "type": "text",
          "copy_to": [
            "copyTo"
          ],
          "fields": 
            "keyword": 
              "type": "keyword",
              "ignore_above": 256
            
          
        ,
        "uploaduser": 
          "type": "text",
          "copy_to": [
            "copyTo"
          ],
          "fields": 
            "keyword": 
              "type": "keyword",
              "ignore_above": 256
            
          
        ,
        "uploadDate": 
          "type": "date",
          "format": "MMddyyyy"
        ,
        "documenttype": 
          "type": "text",
          "copy_to": [
            "copyTo"
          ],
          "fields": 
            "keyword": 
              "type": "keyword",
              "ignore_above": 256
            
          
        ,
        "accessControl": 
          "type": "boolean",
          "null_value": false
        ,
        "comments": 
          "type": "nested",
          "properties": 
            "createDate": 
              "type": "date",
              "format": "MMddyyyy"
            ,
            "comment": 
              "type": "text",
              "copy_to": [
                "copyTo"
              ],
              "fields": 
                "keyword": 
                  "type": "keyword",
                  "ignore_above": 256
                
              
            ,
            "user": 
              "type": "text",
              "copy_to": [
                "copyTo"
              ],
              "fields": 
                "keyword": 
                  "type": "keyword",
                  "ignore_above": 256
                
              
            
          
        ,
        "metadata": 
          "type": "nested",
          "properties": 
            "key": 
              "type": "text",
              "copy_to": [
                "copyTo"
              ],
              "fields": 
                "keyword": 
                  "type": "keyword",
                  "ignore_above": 256
                
              
            ,
            "value": 
              "type": "text",
              "copy_to": [
                "copyTo"
              ],
              "fields": 
                "keyword": 
                  "type": "keyword",
                  "ignore_above": 256
                
              
            
          
        ,
        "copyTo": 
          "type": "text"
        
      
    
  

与 Elasticsearch 7.x 兼容的 NEST 7.x 的语法相同;有几件事已过时,可以更改以删除警告,但 6.x 语法按原样工作

client.CreateIndex(defaultIndex, c => c
    // remove .Mappings()
    .Map<DocumentModel>(mm => mm
        .AutoMap(visitor)
        .Properties(ps => ps
            .Nested<Comments>(cm => cm
                .Name(n => n.Comments)
                .AutoMap(visitor)
            )
            .Nested<Metadata>(md => md
                .Name(n => n.Metadata)
                .AutoMap(visitor)
            )
            .Text(t => t.Name(n => n.CopyTo))
        )
    )
);

// Use RelationName instead of Name
[ElasticsearchType(RelationName = "documentModel")]
public class DocumentModel

    [Text(Name = "filename")]
    public string Filename  get; set; 
    [Text(Name = "folderid")]
    public int FolderID  get; set; 
    [Text(Name = "uploaduser")]
    public string uploadUser  get; set; 
    [Date(Format = "MMddyyyy")]
    public DateTime UploadDate  get; set; 
    [Text(Name = "documenttype")]
    public string DocumentType  get; set; 
    [Boolean(NullValue = false)]
    public bool AccessControl  get; set; 
    [Nested]
    public List<Comments> Comments  get; set; 
    [Nested]
    public List<Metadata> Metadata  get; set; 
    [Text(Name = "copyTo")]
    public string CopyTo  get; set; 

【讨论】:

这正是我所需要的,我上面的代码的问题(我不确定我是否清楚...)是 [ElasticsearchType(Name = "documentModel")] 是显示错误:“找不到类型或命名空间“名称”。”...我不确定这是否是由于使用 NEST 7.0-alpha...您使用的是哪个版本的 NEST? 我使用的是 6.7.0。这将是由于使用了与 Elasticsearch 6.7.0 不兼容的 7.0.0-alpha1 我会尝试降级我的 NEST,我会尽快返回结果 使用 NEST 6.7.0,您的解决方案和我的代码都可以工作;话虽如此,如果有人知道,我想看看 7.x 的语法?现在我将坚持使用 6.7.0 并为你实现访问者类,如果有人没有用 7.x 语法回复我,我会为 Russ 欢呼很快就会给你答案 相同的语法将在 7.x 中工作。有几个过时的警告可以更改代码来修复。

以上是关于映射 - ElasticSearch [7+]的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch:字段太多, 在 Elasticsearch 中防止映射爆炸的 3 种方法

elasticsearch的字符串动态映射

大数据技术之_20_Elasticsearch学习_01_概述 + 快速入门 + Java API 操作 + 创建删除索引 + 新建搜索更新删除文档 + 条件查询 + 映射操作

ElasticSearch1.7.1 安装

linux12企业实战 -- 03docker elasticsearch单机映射文件到宿主机

ElasticSearch基础