Search code examples
javamapreducecouchdbcloudantnosql-aggregation

How to obtain count while using java client of cloudant?


I have a requirement of finding the total number of documents while performing a query without fetching them in cloudant.

I'm using the cloudant client https://github.com/cloudant/java-cloudant . Generally to fetch documents I use "findByIndex()" method.

Now my requirement is to get "only" the total count of documents that can fetched by the selector that is specified in first argument of findByIndex().

I don't want to fetch all the documents and count them on the client side as that will take up all the network bandwidth and the memory.

From the search I do see its possible via the reduce function given here: http://guide.couchdb.org/editions/1/en/cookbook.html#aggregate

But how do I use this reduce function on the java client for cloudant? Is there some other way to achieve this?


Solution

  • The findByIndex function uses Cloudant Query indexes. This index type doesn't support returning the total count of results right now.

    The link http://guide.couchdb.org/editions/1/en/cookbook.html#aggregate refers to Views, a different type of index on Cloudant. Reduces only work in Views and not Query indexes. You'll need to use a view, as Chris Snow suggests, to get the total count efficiently via a reduce.

    Unfortunately you can't use a regex in a view (which you mention your Cloudant Query selector uses). However, perhaps you can create a key in the view's map function which allows the same query to be performed.