【问题标题】:Elastic Search map property with diferent types不同类型的 Elasticsearch 地图属性
【发布时间】:2020-10-04 16:11:48
【问题描述】:
public class Document {
    public Guid Id {get;set;}
    public string Name {get;set;}
    public DocumentAttribute[] Attributes {get;set;}
}

public class DocumentAttribute {
    public Guid AttributeId {get;set;}
    public string Type {get;set;}
    public object Value {get;set;}
}

DocumentAttribute.Type 包含值的类型(字符串、日期、...)

我可以这样映射它:

.Map<DocumentDto>(
                m=>m
                .Properties(p => p
                .Text(s => s.Name(DocumentDto.DefaultAttributes.Name))
                .Nested<DocumentAttribute>(da => da
                    .Name(DocumentDto.DefaultAttributes.Attributes)
                    .Properties(dap => dap
                        .Text(s => s.Name(n => n.AttributeId))
                        .Nested<object>(dav => dav.Name(n => n.Value))
                        )
                    )
                )
                );

如果我尝试索引包含多种类型(一个是日期)属性的文档,我会得到:

mapper cannot be changed from type [date] to [text]

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    简单来说,您要做的是动态映射具有不同类型的相同名称的 2 个字段。

    下面是等效的 ES 映射命令:

    PUT documents
    {
      "mappings": {
        "properties": {
          "key1":{
            "type": "text" 
          },
          "key1":{
            "type": "date"
          }
        }
      }
    }
    

    查询结果:给定的查询将导致异常=>“重复字段'name'”。

    但是:

    POST documents/_doc
    {
      "key1":1 //<======== (Success) value is an Integer
    }
    
    POST documents/_doc
    {
      "key1":"1" //<==== (Success) Value is a String but can be converted to integer just fine
    }
    
    POST documents/_doc
    {
      "key1":"Hello" //<==== (Fail) Value is a String but can't be converted to an integer
    }
    

    解决方案:遵循以下文档属性架构,以便无需尝试将同名键动态映射到不同类型。

       {
        "type":"" // <==== e.g Possible values are int, text, date 
        "val_int":1, // <==== here keyname is val_<typeValue>
        "val_text":"Hey",
        "val_date":"2020-01-01" 
       }
    

    【讨论】:

    • 如果没有其他人有击球手的想法,那将是我的下一次尝试
    猜你喜欢
    • 1970-01-01
    • 2018-09-13
    • 2021-12-03
    • 2017-01-11
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多