elasticsearch,nest,C#,elasticsearch,Nest" /> elasticsearch,nest,C#,elasticsearch,Nest" />

C# 自动映射时可以展平对象的部分

C# 自动映射时可以展平对象的部分,c#,elasticsearch,nest,C#,elasticsearch,Nest,我不熟悉使用Elasticsearch,我在一个服务上使用search,我得到的部分结果格式如下(名称翻译自其他语言): 我可以自动将其映射到这样的对象,而不会出现问题: class MyBaseObject { public Accounting Accounting { get; set; } //...some other values on base object } class Accounting { public AccountingInterval Ac

我不熟悉使用Elasticsearch,我在一个服务上使用search,我得到的部分结果格式如下(名称翻译自其他语言):

我可以自动将其映射到这样的对象,而不会出现问题:

class MyBaseObject
{
    public Accounting Accounting { get; set; }
    //...some other values on base object
}

class Accounting
{
    public AccountingInterval AccountingInterval { get; set; }
}

class AccountingInterval
{
    [Date(Format = "dateOptionalTime")]
    public DateTime? StartDate { get; set; }
    [Date(Format = "dateOptionalTime")]
    public DateTime? EndDate { get; set; }
}
有没有办法让它映射到这样一个简单的对象:

class MyBaseObject
{
    [Date(Format = "dateOptionalTime")]
    public DateTime? AccountingStartDate { get; set; }
    [Date(Format = "dateOptionalTime")]
    public DateTime? AccountingEndDate { get; set; }
    //...some other values on base object
}
我尝试设置name属性,但似乎不起作用

class MyBaseObject
{
    [Date(Name ="accounting.accountingInterval.startDate", Format = "dateOptionalTime")]
    public DateTime? AccountingStartDate { get; set; }
    [Date(Name ="accounting.accountingInterval.endDate", Format = "dateOptionalTime")]
    public DateTime? AccountingEndDate { get; set; }
    //...some other values on base object
}

正如panchicore在评论中所说,可以在索引时使用执行此平坦化,索引中的类型映射将反映此结构


如果你不负责索引,那么做起来就更难了。NEST中的映射用于Elasticsearch文档的输入和输出。通过连接和,并为
MyBaseObject
类型定义自定义
JsonConverter
,可以控制JSON如何反序列化到
MyBaseObject
。如果你只想做类型美学,努力可能比价值更重要

我对c#一无所知,但我了解elasticsearch,在那里,这个问题可以通过“摄取管道”轻松解决。@panchicore感谢您的输入,很遗憾,我只是这项服务的消费者,所以我无法设置上限谢谢您的回答,这主要是为了美观,我选择了一个简单的解决方案,将以下内容添加到MyBaseObject
PublicDateTime?AccountingStartDate=>Accounting?.AccountingInterval?.AccountingStartDate
class MyBaseObject
{
    [Date(Name ="accounting.accountingInterval.startDate", Format = "dateOptionalTime")]
    public DateTime? AccountingStartDate { get; set; }
    [Date(Name ="accounting.accountingInterval.endDate", Format = "dateOptionalTime")]
    public DateTime? AccountingEndDate { get; set; }
    //...some other values on base object
}