【问题标题】:Call SourceInclude without manually specifying all properties调用 SourceInclude 而不手动指定所有属性
【发布时间】: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


    【解决方案1】:

    不,您需要自己处理生成字段名称列表以检索。如果您不提及SourceInclude,则将检索整个_source。这就是 Elasticsearch 的行为。尽管如此,如果字段名称匹配,Nest 将能够从响应中创建一个 Product 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 2014-06-09
      • 2011-12-28
      • 2011-02-01
      相关资源
      最近更新 更多