Manu
Manu

Reputation: 43

Elasticsearch 5.4 Pagination with NEST for .NET

I know, I'm so stupid that I cannot see how, but I'm new with elasticsearch.

I want to know how can i do a simple pagination.

Like: 1 To 10 Of 123,456 entries

I need to know the total hits for the query, have sense right?

    public long GetCount(SearchModel model)
    {
        return _elasticClient.Search<Document>(s => s
            .Query(q => GetWhere(q, model))
        ).Total;

    }

I'm not using From/Size because I want to get the total records for the query (that simple).

I tried ISearchResponse.Total and that ignores the query filters

some advice will be very appreciated, thanks

Upvotes: 4

Views: 5256

Answers (1)

MichaC
MichaC

Reputation: 13380

When you do a simple document search you should probably just use From and Size for paging. The returned result should have a hits.total representation which is the total number of documents matching your query. The hits collection though will only have the 10 documents or whatever you define in (size).

Example for From/Size:

var response = client.Search<Tweet>(s => s
.From(0)
.Size(10)
.Query(q =>
        q.Term(t => t.User, "kimchy")
        || q.Match(mq => mq.Field(f => f.User).Query("nest"))
    )
);

response.HitsMetaData.Total should have the total number of docs found.

Upvotes: 5

Related Questions