On 10/11/2008, at 8:06 AM, Sho Fukamachi wrote:

Hi all,

This is probably a really simple question, but for some reason* I can't figure it out.

(* reason is most like extreme dumbness on my part)

I've got a bunch of views where i have something like this:

get_all_by_field:

map: if (doc.type == 'my_doc') emit(doc.field, doc);

count_all_by_field:

map: if (doc.type == 'my_doc') emit(doc.field, 1);
reduce: function(keys, values) { return sum(values) }

That works fine, but seems like it could be more elegant ..

I thought I'd be "clever" and try to combine the two, so I could get the "search" output by calling the map alone, and then getting the count by turning on reduce, something like this:

map: function(doc) {
 if (doc.type == 'my_doc') emit(doc.field, [doc, 1]);
   }
reduce: function(keys, values) { return sum(values[1]) }

In any case, this isn't valid logic - you need to do something like

theSum = 0;
for (var i in values)
  theSum = theSum + values[i][1];
return theSum;

Antony Blakey
--------------------------
CTO, Linkuistics Pty Ltd
Ph: 0438 840 787

What can be done with fewer [assumptions] is done in vain with more
  -- William of Ockham (ca. 1285-1349)



Reply via email to