[英]Providing null value for elasticsearch date field
我只是想知道是否有人知道如何為elasticsearch date字段提供空值。
您可以在下面的屏幕截圖中看到可以將DateTime用作空值,但是當我嘗試使用它時,它不接受它。 產生錯誤消息:
““ NullValue”不是有效的命名屬性參數,因為它不是有效的屬性參數類型。”
由於NullValue
的DateAttribute
是DateTime
,因此無法在應用於POCO屬性的屬性上進行設置,因為設置值必須是編譯時間常數。 這是使用屬性方法進行映射的限制之一。
NullValue
可以通過以下兩種方式設置:
使用流暢的API
流利的映射可以完成屬性映射可以完成的所有工作,還可以處理諸如空值,multi_fields等功能。
public class MyDocument
{
public DateTime DateOfBirth { get; set; }
}
var fluentMappingResponse = client.Map<MyDocument>(m => m
.Index("index-name")
.AutoMap()
.Properties(p => p
.Date(d => d
.Name(n => n.DateOfBirth)
.NullValue(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc))
)
)
);
使用訪客模式
定義一個訪問者,該訪問者將訪問POCO中的所有屬性,並使用它來設置一個空值。 訪客模式對於將約定應用到映射很有用,例如,所有字符串屬性都應該是一個multi_field,其中未分析的原始子字段。
public class MyPropertyVisitor : NoopPropertyVisitor
{
public override void Visit(IDateProperty type, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute)
{
if (propertyInfo.DeclaringType == typeof(MyDocument) &&
propertyInfo.Name == nameof(MyDocument.DateOfBirth))
{
type.NullValue = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
}
}
}
var visitorMappingResponse = client.Map<MyDocument>(m => m
.Index("index-name")
.AutoMap(new MyPropertyVisitor())
);
流利的地圖繪制和訪問者都會產生以下請求
{
"properties": {
"dateOfBirth": {
"null_value": "1970-01-01T00:00:00Z",
"type": "date"
}
}
}
只是使用了以下代碼,而不是在class date屬性上聲明了它:
.Properties(pr => pr .Date(dt => dt .Name(n => n.dateOfBirth) .NullValue(new DateTime(0001, 01, 01))))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.