Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch NEST Document count for default index

I am using NEST for Elasticsearch 6 and would like to get the number of documents for the default index.

The documentation refers to the 1.1 version of the API which no longer seems to work.

I have created the connection settings using a default index:

var connectionSettings = new ConnectionSettings().DefaultIndex("test_docs");

When I try the code from the 1.1 api documentation:

var result = client.Count();

I get the following error:

The type arguments for method 'ElasticClient.Count(Func, ICountRequest>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

When I supply a type it is appended to the path. For example:

client.Count<TestDocument>();

Generates a URL of http://localhost:9200/test_docs/testdocument/_count when what I really need is http://localhost:9200/test_docs/_count

like image 348
Newm Avatar asked Sep 13 '25 04:09

Newm


2 Answers

For those needing the new way of doing this (like myself). I would use the following to get a count from a specific index.

var countRequest = new CountRequest(Indices.Index("videos"));
long count = (await _ecClient.CountAsync(countRequest)).Count;
like image 176
CaMiX Avatar answered Sep 16 '25 00:09

CaMiX


You can use

var countResponse = client.Count<TestDocument>(c => c.AllTypes());

which will call the API

GET http://localhost:9200/test_docs/_count
like image 44
Russ Cam Avatar answered Sep 16 '25 02:09

Russ Cam