You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Nadav Samet <th...@gmail.com> on 2009/06/04 07:54:19 UTC
Finding how many items correspond to a key in a view
Hi,
Given a key I'd like to find how many items in a permanent view correspond
to that key (without retrieving the items themselves, because there can be
too many).
I currently achieve it by having another mapreduce just to compute this
count, but it sounds like there must be a better way.
--
Sincerely yours,
Nadav
Re: Finding how many items correspond to a key in a view
Posted by Chris Anderson <jc...@apache.org>.
On Fri, Jun 5, 2009 at 1:00 PM, Brian Candler<B....@pobox.com> wrote:
> On Wed, Jun 03, 2009 at 10:54:19PM -0700, Nadav Samet wrote:
>> Given a key I'd like to find how many items in a permanent view correspond
>> to that key (without retrieving the items themselves, because there can be
>> too many).
>>
>> I currently achieve it by having another mapreduce just to compute this
>> count, but it sounds like there must be a better way.
>
> Sounds like the right way to me. If you have an existing view which does
>
> emit(key,anything);
>
> and as long as this view doesn't already have a reduce function, you can add
> a counter reduce function(*) to it. Then you just have to remember to query
> it with reduce=false when you *don't* want the counts. (I'd prefer it if you
> had to say reduce=true to get the reduction, but that's probably just me :-)
>
you can also (in trunk) specify you're reduce function as:
_count
which is equivalent of Brian's reduce but in ran as native Erlang.
> Regards,
>
> Brian.
>
> (*) e.g. this one:
>
> function(ks, vs, co) {
> if (co) {
> return sum(vs);
> } else {
> return vs.length;
> }
> }
>
--
Chris Anderson
http://jchrisa.net
http://couch.io
Re: Finding how many items correspond to a key in a view
Posted by Brian Candler <B....@pobox.com>.
On Wed, Jun 03, 2009 at 10:54:19PM -0700, Nadav Samet wrote:
> Given a key I'd like to find how many items in a permanent view correspond
> to that key (without retrieving the items themselves, because there can be
> too many).
>
> I currently achieve it by having another mapreduce just to compute this
> count, but it sounds like there must be a better way.
Sounds like the right way to me. If you have an existing view which does
emit(key,anything);
and as long as this view doesn't already have a reduce function, you can add
a counter reduce function(*) to it. Then you just have to remember to query
it with reduce=false when you *don't* want the counts. (I'd prefer it if you
had to say reduce=true to get the reduction, but that's probably just me :-)
Regards,
Brian.
(*) e.g. this one:
function(ks, vs, co) {
if (co) {
return sum(vs);
} else {
return vs.length;
}
}