Vinyl Warmth
Vinyl Warmth

Reputation: 2516

How can I find the total hits for an Elastic NEST query?

In my application I have a query which brings limits the number of hits returned to 50 as follows

            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?

Upvotes: 1

Views: 2115

Answers (2)

Rob
Rob

Reputation: 9979

You are looking for Total property on the search response object. Have a look.

So in your particular case that will be response.Total.

Upvotes: 7

Kerem
Kerem

Reputation: 1553

For those who are working on indices with more than 10000 documents, Elasticsearch will calculate total hits up to 10000 by default. To get around that, include .TrackTotalHits(true) in your query:

var resp = client.Search<yourmodel>(s => s
    .Index(yourindexname)
    .TrackTotalHits(true)
    .Query(q => q.MatchAll()));

Upvotes: 6

Related Questions