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


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。类型包含值的类型(字符串、日期…(

我可以这样映射:

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

查询结果:给定的查询将导致异常=>quot;重复字段"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" 
}

最新更新