You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Borja Martín <bo...@dagi3d.net> on 2010/01/08 15:10:09 UTC

Ordering results from reduce function

Hi,
suppose I have the typical view used to index the tags of a document:

// map
function(doc) {
  for (var idx in doc.tags) {
   emit(doc.tags[idx], 1);
 }
}

// and then I have this reduce function to count how many documents I have
for each given tag:
function(key,values) {
 return sum(values);
}

and with the following sample documents:
{ "tags" : ["foo", "foo bar"] },
{ "tags" : ["foo", "bar", "foo bar"]}

I get these results:
{"rows":[
 {"key":"bar","value":1},
 {"key":"foo","value":3},
 {"key":"foo bar","value":2}
]}

What I want to do is to get the results ordered by its value so I can know
which are the most used tags for example. I know that the results are
ordered by the key so I guess this could be done if I could use the value
returned by the reduce on it while calling the emit function with something
like emit([reduce_value, tag], null); Is there any way to accomplish that?

Has it sense or am I misunderstanding something?

Thanks in advance

Regards

-- 
def dagi3d(me)
 case me
   when :web then  "http://dagi3d.net"
   when :twitter then "http://twitter.com/dagi3d"
 end
end

Re: Ordering results from reduce function

Posted by Chris Anderson <jc...@apache.org>.
On Fri, Jan 8, 2010 at 6:10 AM, Borja Martín <bo...@dagi3d.net> wrote:
> Hi,
> suppose I have the typical view used to index the tags of a document:
>
> // map
> function(doc) {
>  for (var idx in doc.tags) {
>   emit(doc.tags[idx], 1);
>  }
> }
>
> // and then I have this reduce function to count how many documents I have
> for each given tag:
> function(key,values) {
>  return sum(values);
> }
>
> and with the following sample documents:
> { "tags" : ["foo", "foo bar"] },
> { "tags" : ["foo", "bar", "foo bar"]}
>
> I get these results:
> {"rows":[
>  {"key":"bar","value":1},
>  {"key":"foo","value":3},
>  {"key":"foo bar","value":2}
> ]}
>
> What I want to do is to get the results ordered by its value so I can know
> which are the most used tags for example. I know that the results are
> ordered by the key so I guess this could be done if I could use the value
> returned by the reduce on it while calling the emit function with something
> like emit([reduce_value, tag], null); Is there any way to accomplish that?
>

This is a common request, but not supported directly by CouchDB's
views -- to do this you'll need to copy the group-reduce query to
another database, and build a view to sort by value.

This is a tradeoff we make in favor of dynamic range queries and
incremental indexes.

Chris

> Has it sense or am I misunderstanding something?
>
> Thanks in advance
>
> Regards
>
> --
> def dagi3d(me)
>  case me
>   when :web then  "http://dagi3d.net"
>   when :twitter then "http://twitter.com/dagi3d"
>  end
> end
>



-- 
Chris Anderson
http://jchrisa.net
http://couch.io