Search code examples
elasticsearchnest

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


Solution

  • You can use

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

    which will call the API

    GET http://localhost:9200/test_docs/_count