索引派生的 [ElasticType] 仅序列化基础 class' 属性
Indexing a derived [ElasticType] only serializes the base class' properties
对于我的生活,我无法弄清楚为什么 Nest 在索引实例时只序列化下面的基础 class' 属性,即使我告诉它索引派生的 class .
基地class:
[ElasticType(Name = "activity")]
public class Activity
{
[ElasticProperty(Name = "name")]
public string Name { get; set; }
[ElasticProperty(OptOut = true)]
public DateTimeOffset Timestamp
{
get { return DateTimeOffset.ParseExact(TimestampAsString, "yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture).ToUniversalTime(); }
set { TimestampAsString = value.ToString("yyyyMMddTHHmmssZ"); }
}
[Obsolete("Use Timestamp, instead.")]
[ElasticProperty(Name = "timestamp")]
public string TimestampAsString { get; set; }
[ElasticProperty(Name = "application")]
public string Application { get; set; }
[ElasticProperty(Name = "application_version")]
public string ApplicationVersion { get; set; }
[ElasticProperty(OptOut = true)]
public IPAddress RemoteIpAddress
{
get { return IPAddress.Parse(RemoteIpAddressAsString); }
set { RemoteIpAddressAsString = value.ToString(); }
}
[Obsolete("Use RemoteIpAddress, instead.")]
[ElasticProperty(Name = "remote_ip_address")]
public string RemoteIpAddressAsString { get; set; }
}
派生class:
private class SearchCountsRetrievedActivity : Activity
{
[ElasticProperty(OptOut = true)]
public PunctuationlessGuid? PrincipalIdentityId
{
set { PrincipalIdentityIdAsString = value; }
}
[ElasticProperty(Name = "principal_identity_id")]
public string PrincipalIdentityIdAsString { get; set; }
}
我的索引包装方法:
public Task IndexActivityAsync<TActivity>(TActivity activity)
where TActivity : Activity
{
return _client.IndexAsync(activity);
}
无论我做什么,通过网络发送的序列化 JSON 仅包含 Activity
class' 属性。我尝试过的:
- 制作派生的 classes public
- 将
[ElasticType]
添加到派生的 classes
- 检查 Nest 源代码(这非常困难,因为源代码非常复杂,我引用的 NuGet 包,即使它是最新的,似乎与最新的源代码不向前兼容)
显然,底层默认的 JSON 序列化程序 而不是 序列化具有 null
值的属性。在我的例子中,我的 属性 值是 null
,所以这就是它们没有被序列化的原因。当值不是 null
.
时,序列化器可以正常工作
对于我的生活,我无法弄清楚为什么 Nest 在索引实例时只序列化下面的基础 class' 属性,即使我告诉它索引派生的 class .
基地class:
[ElasticType(Name = "activity")]
public class Activity
{
[ElasticProperty(Name = "name")]
public string Name { get; set; }
[ElasticProperty(OptOut = true)]
public DateTimeOffset Timestamp
{
get { return DateTimeOffset.ParseExact(TimestampAsString, "yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture).ToUniversalTime(); }
set { TimestampAsString = value.ToString("yyyyMMddTHHmmssZ"); }
}
[Obsolete("Use Timestamp, instead.")]
[ElasticProperty(Name = "timestamp")]
public string TimestampAsString { get; set; }
[ElasticProperty(Name = "application")]
public string Application { get; set; }
[ElasticProperty(Name = "application_version")]
public string ApplicationVersion { get; set; }
[ElasticProperty(OptOut = true)]
public IPAddress RemoteIpAddress
{
get { return IPAddress.Parse(RemoteIpAddressAsString); }
set { RemoteIpAddressAsString = value.ToString(); }
}
[Obsolete("Use RemoteIpAddress, instead.")]
[ElasticProperty(Name = "remote_ip_address")]
public string RemoteIpAddressAsString { get; set; }
}
派生class:
private class SearchCountsRetrievedActivity : Activity
{
[ElasticProperty(OptOut = true)]
public PunctuationlessGuid? PrincipalIdentityId
{
set { PrincipalIdentityIdAsString = value; }
}
[ElasticProperty(Name = "principal_identity_id")]
public string PrincipalIdentityIdAsString { get; set; }
}
我的索引包装方法:
public Task IndexActivityAsync<TActivity>(TActivity activity)
where TActivity : Activity
{
return _client.IndexAsync(activity);
}
无论我做什么,通过网络发送的序列化 JSON 仅包含 Activity
class' 属性。我尝试过的:
- 制作派生的 classes public
- 将
[ElasticType]
添加到派生的 classes - 检查 Nest 源代码(这非常困难,因为源代码非常复杂,我引用的 NuGet 包,即使它是最新的,似乎与最新的源代码不向前兼容)
显然,底层默认的 JSON 序列化程序 而不是 序列化具有 null
值的属性。在我的例子中,我的 属性 值是 null
,所以这就是它们没有被序列化的原因。当值不是 null
.