1

我需要在执行查询时查找文档总数,而无需在 cloudant 中获取它们。

我正在使用 cloudant 客户端https://github.com/cloudant/java-cloudant。通常要获取文档,我使用“findByIndex()”方法。

现在我的要求是“仅”获取 findByIndex() 的第一个参数中指定的选择器可以获取的文档总数。

我不想获取所有文档并在客户端计算它们,因为这将占用所有网络带宽和内存。

从搜索中,我确实通过这里给出的 reduce 函数看到了它的可能性:http: //guide.couchdb.org/editions/1/en/cookbook.html#aggregate

但是如何在 Cloudant 的 java 客户端上使用这个 reduce 函数呢?还有其他方法可以实现这一目标吗?

4

2 回答 2

1

查询视图的文档是https://github.com/cloudant/java-cloudant#query-on-a-view

我使用 groovysh(使用 java api)针对 cloudant 教育帐户的 animaldb 快速运行了一个示例。

您可以通过使用浏览器点击此 URL 来查看示例查询的原始结果:https ://education.cloudant.com/animaldb/_design/views101/_view/diet_count?reduce=true&group=true&key=%22omnivore%22

{"rows":[
   {"key":"omnivore","value":3}
]}

groovy 程序(注意前两行使用 Grape 只是为了拉入 cloudant java 库):

groovy:000> import groovy.grape.Grape
groovy:000> Grape.grab(group:'com.cloudant', module:'cloudant-client', version:'1.0.1')
groovy:000> import com.cloudant.client.api.CloudantClient
groovy:000> client = new CloudantClient('education', 'education')
groovy:000> db = client.database('animaldb', false)
groovy:000> omnivores = db.view("views101/diet_count").key('omnivore').queryForInt()
===> 3

这是等效的java代码:

CloudantClient client = new CloudantClient('education', 'education');
Database db = client.database('animaldb', false);
int omnivores = db.view("views101/diet_count").key('omnivore').queryForInt();
于 2015-04-20T08:48:21.880 回答
0

findByIndex函数使用 Cloudant 查询索引。此索引类型目前不支持返回结果总数。

链接http://guide.couchdb.org/editions/1/en/cookbook.html#aggregate指的是 Views,Cloudant 上的一种不同类型的索引。仅减少视图而不是查询索引中的工作。正如 Chris Snow 建议的那样,您需要使用视图来通过减少有效地获取总数。

不幸的是,您不能在视图中使用正则表达式(您提到您的 Cloudant 查询选择器使用)。但是,也许您可​​以在视图的map函数中创建一个允许执行相同查询的键。

于 2015-09-15T09:31:57.520 回答