atom feed1 message in org.apache.couchdb.userRe: Ordering results from reduce func...
FromSent OnAttachments
Chris AndersonJan 8, 2010 9:51 am 
Subject:Re: Ordering results from reduce function
From:Chris Anderson (jch@apache.org)
Date:Jan 8, 2010 9:51:28 am
List:org.apache.couchdb.user

On Fri, Jan 8, 2010 at 6:10 AM, Borja Martín <bor@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