代码之家  ›  专栏  ›  技术社区  ›  Jester

具有不同类型的弹性搜索映射属性

  •  0
  • Jester  · 技术社区  · 4 年前
    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;}
    }
    

    文件属性。类型包含值的类型(字符串、日期等)

    我可以这样映射它:

    .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]
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Sahil Gupta    4 年前

    简单地说,您要做的是动态地将两个同名的字段映射为不同的类型。

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

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

    查询结果: 给定的查询将导致异常=>“重复字段‘名称’”。

    然而:

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