首页 > 解决方案 > 默认索引的 Elasticsearch NEST 文档计数

问题描述

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

文档是指 API 的 1.1 版本,它似乎不再起作用。

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

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

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

var result = client.Count();

我收到以下错误:

无法从用法中推断出方法“ElasticClient.Count(Func, ICountRequest>)”的类型参数。尝试明确指定类型参数。

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

client.Count<TestDocument>();

当我真正需要的是http://localhost:9200/test_docs /_count 时,生成一个http://localhost:9200/test_docs/testdocument/_count的 URL

标签: elasticsearchnest

解决方案


您可以使用

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

这将调用 API

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

推荐阅读