具有不同类型的Elastic Search地图属性

问题描述

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]

解决方法

简单来说,您想要做的是动态映射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" 
   }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...