Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为elasticsearch日期字段提供空值_C#_.net_Class_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Nest - Fatal编程技术网 elasticsearch,nest,C#,.net,Class,elasticsearch,Nest" /> elasticsearch,nest,C#,.net,Class,elasticsearch,Nest" />

C# 为elasticsearch日期字段提供空值

C# 为elasticsearch日期字段提供空值,c#,.net,class,elasticsearch,nest,C#,.net,Class,elasticsearch,Nest,我只是想知道是否有人知道如何为elasticsearch日期字段提供空值 您可以在下面的屏幕截图中看到,可以使用DateTime作为空值,但当我尝试时,它不接受。生成错误消息: “'NullValue'不是有效的命名属性参数,因为它不是有效的属性参数类型。” 只是使用了以下代码,而不是在class date属性中声明: .Properties(pr=>pr .日期(dt=>dt .Name(n=>n.dateOfBirth) .NullValue(新日期时间(0001,01,01)))只使用了

我只是想知道是否有人知道如何为elasticsearch日期字段提供空值

您可以在下面的屏幕截图中看到,可以使用DateTime作为空值,但当我尝试时,它不接受。生成错误消息:

“'NullValue'不是有效的命名属性参数,因为它不是有效的属性参数类型。”


只是使用了以下代码,而不是在class date属性中声明:

.Properties(pr=>pr
.日期(dt=>dt
.Name(n=>n.dateOfBirth)

.NullValue(新日期时间(0001,01,01)))
只使用了以下代码,而不是在类日期属性中声明它:

.Properties(pr=>pr
.日期(dt=>dt
.Name(n=>n.dateOfBirth)

.NullValue(新日期时间(0001,01,01))
因为
DateAttribute
NullValue
是一个
DateTime
,所以不能在应用于POCO属性的属性上设置它,因为设置的值需要是编译时常量。这是使用属性方法进行映射的限制之一

NullValue
可以通过以下几种方式进行设置:

使用fluent API

fluent映射可以完成属性映射所能做的一切,还可以处理空值、多个字段等功能

公共类MyDocument
{
公共日期时间出生日期{get;set;}
}
var fluentMappingResponse=client.Map(m=>m
.索引(“索引名称”)
.AutoMap()
.Properties(p=>p
.日期(d=>d
.Name(n=>n.DateOfBirth)
.NullValue(新的日期时间(1970,1,1,0,0,0,DateTimeKind.Utc))
)
)
);
使用访客模式

定义一个访问POCO中所有属性的访问者,并使用它设置空值。visitor模式对于将约定应用于映射非常有用,例如,所有字符串属性都应该是带有未分析原始子字段的multi_字段

公共类MyPropertyVisitor:NoopPropertyVisitor
{
公共覆盖无效访问(IDateProperty类型、PropertyInfo PropertyInfo、ElasticsearchPropertyAttributeBase属性)
{
if(propertyInfo.DeclaringType==typeof(MyDocument)&&
propertyInfo.Name==nameof(MyDocument.DateOfBirth))
{
type.NullValue=新的日期时间(1970,1,1,0,0,0,DateTimeKind.Utc);
}
}
}
var visitorAppingResponse=client.Map(m=>m
.索引(“索引名称”)
.AutoMap(新的MyPropertyVisitor())
);
fluent映射和visitor都会生成以下请求

{
  "properties": {
    "dateOfBirth": {
      "null_value": "1970-01-01T00:00:00Z",
      "type": "date"
    }
  }
}

因为
DateAttribute
NullValue
是一个
DateTime
,所以不能在应用于POCO属性的属性上设置它,因为设置的值需要是编译时常量。这是使用属性方法进行映射的限制之一

NullValue
可以通过以下几种方式进行设置:

使用fluent API

fluent映射可以完成属性映射所能做的一切,还可以处理空值、多个字段等功能

公共类MyDocument
{
公共日期时间出生日期{get;set;}
}
var fluentMappingResponse=client.Map(m=>m
.索引(“索引名称”)
.AutoMap()
.Properties(p=>p
.日期(d=>d
.Name(n=>n.DateOfBirth)
.NullValue(新的日期时间(1970,1,1,0,0,0,DateTimeKind.Utc))
)
)
);
使用访客模式

定义一个访问POCO中所有属性的访问者,并使用它设置空值。visitor模式对于将约定应用于映射非常有用,例如,所有字符串属性都应该是带有未分析原始子字段的multi_字段

公共类MyPropertyVisitor:NoopPropertyVisitor
{
公共覆盖无效访问(IDateProperty类型、PropertyInfo PropertyInfo、ElasticsearchPropertyAttributeBase属性)
{
if(propertyInfo.DeclaringType==typeof(MyDocument)&&
propertyInfo.Name==nameof(MyDocument.DateOfBirth))
{
type.NullValue=新的日期时间(1970,1,1,0,0,0,DateTimeKind.Utc);
}
}
}
var visitorAppingResponse=client.Map(m=>m
.索引(“索引名称”)
.AutoMap(新的MyPropertyVisitor())
);
fluent映射和visitor都会生成以下请求

{
  "properties": {
    "dateOfBirth": {
      "null_value": "1970-01-01T00:00:00Z",
      "type": "date"
    }
  }
}

这是一个很好的答案,谢谢!我开始认为这可能是因为我使用的方法,所以我继续使用fluentapi设置空值。我想记住这一点很有用,以备将来参考。谢谢你的支持help@GSkidmore-不用担心,很乐意帮忙:)这是个很好的答案谢谢!我开始认为这可能是因为我使用的方法,所以我继续使用fluentapi设置空值。我想记住这一点很有用,以备将来参考。谢谢你的支持help@GSkidmore-不用担心,乐意帮忙:)