You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET/C#的Elasticsearch聚合中指定所需字段

问题:Elasticsearch原生查询转NEST C#代码时聚合字段指定问题

我是.NET新手,正尝试将以下Elasticsearch原生查询转换为C#/.NET代码,但在聚合部分遇到问题,不知如何像原生查询那样在NEST库的聚合中指定所需字段。

原生Elasticsearch查询

Raw_Query = {
  "size": 10000,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "created_date": {
              "gte": "2021-09-18",
              "lte": "2022-09-20"
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "created_date": {
        "order": "desc"
      }
    }
  ],
  "aggs": {
    "pono_aggs": {
      "terms": {
        "field": "POno.keyword",
        "size": 10000
      },
      "aggs": {
        "docs": {
          "top_hits": {
            "size": 1,
            "_source": [
              "purchase_unique_id",
              "tenant_id",
              "created_date",
              "modified_date",
              "modified_userid"
            ]
          }
        }
      }
    }
  }
}

我尝试转换的NEST库C#代码

var searchResponse = elasticClient.Search<Source>(
    s => s.From(0)
             .Size(10000)
             .Query(
                 q => q.Bool(
                     qb => qb.Must(
                         qbm => qbm.DateRange(
                             r => r.Field(f => f.created_date)
                                      .GreaterThanOrEquals(
                                          Convert.ToDateTime(startdate))
                                      .LessThanOrEquals(
                                          Convert.ToDateTime(enddate)))
                             )))
             .Aggregations(
                 a => a.Terms(
                     "pono_aggs",
                     t => t.Field("POno.keyword")
                              .Size(int.MaxValue)
                              .Aggregations(
                                  aa => aa.TopHits(
                                      "_source", th => th.Size(1).Sort(
                                                     so => so.Descending(
                                                         f => f.created_date))

                                          )))));

解决方案

你需要在TopHits聚合中通过Source()方法指定返回字段,同时保持聚合命名与原生查询一致。修改后的完整代码如下:

var searchResponse = elasticClient.Search<Source>(
    s => s.From(0)
             .Size(10000)
             .Sort(sort => sort.Descending(f => f.created_date)) // 补充原生查询中的全局排序
             .Query(
                 q => q.Bool(
                     qb => qb.Must(
                         qbm => qbm.DateRange(
                             r => r.Field(f => f.created_date)
                                      .GreaterThanOrEquals(Convert.ToDateTime(startdate))
                                      .LessThanOrEquals(Convert.ToDateTime(enddate))
                         )
                     )
                 )
             )
             .Aggregations(
                 a => a.Terms(
                     "pono_aggs",
                     t => t.Field("POno.keyword")
                              .Size(10000) // 与原生查询size保持一致,替换int.MaxValue
                              .Aggregations(
                                  aa => aa.TopHits(
                                      "docs", // 与原生查询的子聚合名称一致
                                      th => th.Size(1)
                                              .Sort(so => so.Descending(f => f.created_date))
                                              .Source(source => source.Include(
                                                  fields => fields
                                                      .Field(p => p.purchase_unique_id)
                                                      .Field(p => p.tenant_id)
                                                      .Field(p => p.created_date)
                                                      .Field(p => p.modified_date)
                                                      .Field(p => p.modified_userid)
                                              ))
                                  )
                              )
                 )
             )
);

关键修改点:

  • 补充了全局的Sort配置,和原生查询中的排序对应
  • 将TopHits聚合的名称从_source改为docs,与原生查询的子聚合键匹配
  • 使用Source().Include()方法指定需要返回的字段,对应原生查询的_source数组
  • 将Terms聚合的Size从int.MaxValue改为10000,和原生查询保持一致

内容的提问来源于stack exchange,提问作者Hemant Sah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 13:50:23