Sorting CouchDB Views By Value
        Posted  
        
            by Lee Theobald
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Lee Theobald
        
        
        
        Published on 2010-05-12T10:00:52Z
        Indexed on 
            2010/05/12
            10:04 UTC
        
        
        Read the original article
        Hit count: 213
        
Hi all,
I'm testing out CouchDB to see how it could handle logging some search results. What I'd like to do is produce a view where I can produce the top queries from the results. At the moment I have something like this:
Example document portion
{
  "query": "+dangerous +dogs",
  "hits": "123"
}
Map function (Not exactly what I need/want but it's good enough for testing)
function(doc) {
  if (doc.query) {
    var split = doc.query.split(" ");
    for (var i in split) {
      emit(split[i], 1);
    }
  }
}
Reduce Function
function (key, values, rereduce) {
  return sum(values);
}
Now this will get me results in a format where a query term is the key and the count for that term on the right, which is great. But I'd like it ordered by the value, not the key. From the sounds of it, this is not yet possible with CouchDB.
So does anyone have any ideas of how I can get a view where I have an ordered version of the query terms & their related counts? I'm very new to CouchDB and I just can't think of how I'd write the functions needed.
© Stack Overflow or respective owner