简体   繁体   English

默认索引的 Elasticsearch NEST 文档计数

[英]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.我正在为 Elasticsearch 6 使用 NEST,并希望获取默认索引的文档数。

The documentation refers to the 1.1 version of the API which no longer seems to work.文档是指 API 的 1.1 版本,它似乎不再起作用。

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:当我尝试 1.1 api 文档中的代码时:

var result = client.Count();

I get the following error:我收到以下错误:

The type arguments for method 'ElasticClient.Count(Func, ICountRequest>)' cannot be inferred from the usage.无法从用法中推断出方法“ElasticClient.Count(Func, ICountRequest>)”的类型参数。 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当我真正需要的是http://localhost:9200/test_docs/_count时,生成一个http://localhost:9200/test_docs/testdocument/_count的 URL

You can use 您可以使用

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

which will call the API 它将调用API

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

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM