如何找到 Elastic NEST 查询的总命中数?
How can I find the total hits for an Elastic NEST query?
在我的应用程序中,我有一个查询将返回的命中数限制为 50,如下所示
var response = await client.SearchAsync<Episode>(s => s
.Source(sf => sf
.Includes(i => i
.Fields(
f => f.Title,
f => f.PublishDate,
f => f.PodcastTitle
)
)
.Excludes(e => e
.Fields(f => f.Description)
)
)
.From(request.Skip)
.Size(50)
.Query(q => q
.Term(t => t.Title, request.Search) || q
.Match(mq => mq.Field(f => f.Description).Query(request.Search))));
I am interested in the total number of hits for the query (i.e. not limited to the size), so that I can deal with pagination on the front-end. Does anyone know how I can do this?
您在搜索响应对象上查找 Total
属性。 Have a look.
因此在您的特定情况下,这将是 response.Total
。
对于那些正在处理超过 10000 个文档的索引的人,Elasticsearch 将默认计算最多 10000 个总命中。要解决这个问题,请在您的查询中包含 .TrackTotalHits(true)
:
var resp = client.Search<yourmodel>(s => s
.Index(yourindexname)
.TrackTotalHits(true)
.Query(q => q.MatchAll()));
在我的应用程序中,我有一个查询将返回的命中数限制为 50,如下所示
var response = await client.SearchAsync<Episode>(s => s
.Source(sf => sf
.Includes(i => i
.Fields(
f => f.Title,
f => f.PublishDate,
f => f.PodcastTitle
)
)
.Excludes(e => e
.Fields(f => f.Description)
)
)
.From(request.Skip)
.Size(50)
.Query(q => q
.Term(t => t.Title, request.Search) || q
.Match(mq => mq.Field(f => f.Description).Query(request.Search))));
I am interested in the total number of hits for the query (i.e. not limited to the size), so that I can deal with pagination on the front-end. Does anyone know how I can do this?
您在搜索响应对象上查找 Total
属性。 Have a look.
因此在您的特定情况下,这将是 response.Total
。
对于那些正在处理超过 10000 个文档的索引的人,Elasticsearch 将默认计算最多 10000 个总命中。要解决这个问题,请在您的查询中包含 .TrackTotalHits(true)
:
var resp = client.Search<yourmodel>(s => s
.Index(yourindexname)
.TrackTotalHits(true)
.Query(q => q.MatchAll()));