Elasticsearch 使用 NEST 从 TopHits 聚合中获取 id

Elasticsearch get id from TopHits Aggregation using NEST

因此,要获取文档的内部 ID,我会这样做:

var hits = response
    .Hits
    .Select(h =>
    {
        h.Source.id = h.Id;
        return h.Source;
    })
    .ToList();

但是当我进行聚合时,我如何 return 得到与上面相同的 id?

var agg = response.Aggregations.Terms("inactive_pages");
var hits = agg.Buckets.Select(x => x.TopHits("top_versions").Documents<WebPage>());

查看 TopHits 源代码 https://github.com/elastic/elasticsearch-net/blob/master/src/Nest/Aggregations/Metric/TopHits/TopHitsAggregate.cs#L39-L44,您可以调用两种方法:

  • Documents
  • Hits

现在您正在调用 Documents 并且显式获取文档源,如果您尝试执行与上述类似的方法并改用 Hits 会怎样?

var hits = agg
    .Buckets
    .Select(x => x.TopHits("top_versions").Hits<WebPage>.Select(h =>
    {
        h.Source.id = h.Id;
        return h.Source;
    }));