在不手动指定所有属性的情况下调用 SourceInclude
Call SourceInclude without manually specifying all properties
我正在使用 NEST 1.7 查询 Elasticsearch。我在 Get
操作中使用 SourceInclude
,因为我只需要返回部分源代码。我有一个 DTO class 来表示我想从 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 class 的属性包含在源代码中。
我试过完全省略 SourceInclude
调用,希望 NEST 能够从 Get
的通用类型参数中推断出它需要什么,但在调试器中检查 Elasticsearch 请求似乎告诉我 整个 文档已检索到。
是否有标准 NEST 功能的中间地带?或者我是否需要滚动我自己的方法来动态列出所有 dto 属性?
PS。我 asked a similar question (/ feature request) on Github.
不,您需要处理生成字段名称列表以自行检索。如果您不提及 SourceInclude
,则将检索整个 _source
。这就是 Elasticsearch 的行为。尽管如此,如果字段名称匹配,Nest 将能够从响应中创建一个 Product
对象。
我正在使用 NEST 1.7 查询 Elasticsearch。我在 Get
操作中使用 SourceInclude
,因为我只需要返回部分源代码。我有一个 DTO class 来表示我想从 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 class 的属性包含在源代码中。
我试过完全省略 SourceInclude
调用,希望 NEST 能够从 Get
的通用类型参数中推断出它需要什么,但在调试器中检查 Elasticsearch 请求似乎告诉我 整个 文档已检索到。
是否有标准 NEST 功能的中间地带?或者我是否需要滚动我自己的方法来动态列出所有 dto 属性?
PS。我 asked a similar question (/ feature request) on Github.
不,您需要处理生成字段名称列表以自行检索。如果您不提及 SourceInclude
,则将检索整个 _source
。这就是 Elasticsearch 的行为。尽管如此,如果字段名称匹配,Nest 将能够从响应中创建一个 Product
对象。