如何使用. net/C#在ElasticSearch中将必填字段包含在聚合中

bhmjp9jg  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(103)

我正在尝试将这个elasticsearch原始查询转换为C#/. net。
但是仍然停留在聚合部分。我如何才能像在原始查询中所做的那样将字段包括在。net NEST库中。
我是新来的. net。我已经写了ElasticSearch查询,但有困难转换到. net。

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"
            ]
          }
        }
      }
    }
  }
}

下面我试着转换到.net使用NEST库:

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))

                                          )))));
mitkmikd

mitkmikd1#

您可以在包含属性中提及字段

aa.TopHits("_source",
                      th => th.Source(ssd=> ssd.Includes(fs => fs                                                                                 
                                                       .Field(p => p.FirstName)
                                                       .Field(p => p.LastName)
                                                        )
                                     )
                               .Size(1).Sort(so => so.Descending(
                                                         f => f.created_date))                                         
           )

相关问题