【发布时间】:2018-01-29 14:05:06
【问题描述】:
我正在使用 NEST 1.7 来查询 Elasticsearch。我在Get 操作中使用SourceInclude,因为我只需要返回部分源代码。我有一个 DTO 类来表示我想从 Elasticsearch 中获取的属性。但是,我需要手动指定 SourceInclude 方法的所有属性参数。
这是我正在做的最小复制:
[ElasticType(Name = "productDoc")]
public class Product
{
[ElasticProperty(Name = "doc_id")]
public string Id { get; set; }
[ElasticProperty(Name = "fullname")]
public string Name { get; set; }
[ElasticProperty(Name = "desc")]
public string Description { get; set; }
// Et cetera...
}
public class SearchRepo
{
public Product RetrieveProduct(string id)
{
IElasticClient client = new ElasticClient();
var getResponse = client.Get<Product>(p => p
.Id(id)
.SourceInclude(
i => i.Id,
i => i.Name,
i => i.Description
// Et cetera...
)
);
return getResponse.Source;
}
}
如果 dto 上有许多属性,这会耗费大量人力且容易出错。 我想以某种方式指定只有我的 DTO 类的属性包含在源代码中。
我已经尝试将 SourceInclude 调用全部省略,希望 NEST 能够从泛型类型参数推断出它需要什么到 Get,但在调试器中检查 Elasticsearch 请求似乎告诉我 整个文档被检索。
标准 NEST 功能是否有中间立场?还是我需要滚动自己的方法来动态列出所有 dto 属性?
PS。我有asked a similar question (/ feature request) on Github。
【问题讨论】:
标签: c# elasticsearch nest