Elasticsearch NEST 默认索引的文档数

Elasticsearch NEST Document count for default index

我正在使用适用于 Elasticsearch 6 的 NEST 并希望获取默认索引的文档数。

documentation 指的是 API 的 1.1 版本,它似乎不再有效。

我已经使用默认索引创建了连接设置:

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

当我尝试 1.1 api 文档中的代码时:

var result = client.Count();

我收到以下错误:

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

当我提供类型时,它会附加到路径中。例如:

client.Count<TestDocument>();

生成 URL 的 http://localhost:9200/test_docs/testdocument/_count when what I really need is http://localhost:9200/test_docs/_count

您可以使用

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

这将调用 API

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

对于那些需要新方法的人(比如我自己)。我会使用以下内容从特定索引中获取计数。

var countRequest = new CountRequest(Indices.Index("videos"));
long count = (await _ecClient.CountAsync(countRequest)).Count;