Elasticsearch使用NEST从TopHits聚合获取id



因此,为了获得文档的内部id,我这样做:

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

但是,当我进行聚合时,我如何返回与上面相同的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;
}));

最新更新