<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Posts about Couchdb</title>
    <link>http://wiki.futuretoby.com/tag/couchdb</link>
    <language>en-us</language>
    <item>
      <title>Taking an Average in CouchDB</title>
      <description>More fun with CouchDB, this time taking an average of something:&lt;pre&gt;// map
function(doc){
    emit(null, doc.info.size) // in place of doc.info.size, you'd put whatever&lt;/pre&gt;&lt;pre&gt;                               // value you want averaged here
}
// reduce
function(keys, values, rereduce) {
    if (!rereduce){
        var length = values.length
        return [sum(values) / length, length]
    }else{
        var length = sum(values.map(function(v){return v[1]}))
        var avg = sum(values.map(function(v){
            return v[0] * (v[1] / length)
            }))
        return [avg, length]
    }
}&lt;br&gt;&lt;/pre&gt;&lt;div&gt;The end result will be 2 values, the first is the average, the second is the total number of values that we took an average of. Phew! Who woulda thought taking an average would be so &lt;i&gt;involved!&lt;/i&gt;&lt;/div&gt;</description>
      <pubDate>Wed, 07 Oct 2009 18:31:02 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/Taking_an_Average_in_CouchDB</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Taking_an_Average_in_CouchDB</link>
    </item>
    <item>
      <title>Retrieve The Top N Tags in CouchDB</title>
      <description>I am reading up on how map/reduce works in CouchDB, think I am getting the hang of it now, thanks to&amp;nbsp;&lt;a href="http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html"&gt;this great tool&lt;/a&gt;. I tried the "Retrieve the top N tags" example on&amp;nbsp;&lt;a href="http://wiki.apache.org/couchdb/View_Snippets"&gt;this page&lt;/a&gt;. It didn't work for me at all. So I wrote my own as an exercise. Here's my code:&lt;pre&gt;// the map function
function(doc){
    for(var i in doc.tags)
    {
        emit(null, doc.tags[i]);
    }
}

// the reduce function&lt;/pre&gt;&lt;pre&gt;function(key, values, rereduce){
    var hash = {}
    if (!rereduce){
        for (var i in values){
            var tag = values[i]
            hash[tag] = (hash[tag] || 0) + 1
        }
    }else{
        for (var i in values){
            var topN = values[i]
            for (var i in topN){
                var pair = topN[i]
                var tag = pair[0]
                hash[tag] = (hash[tag] || 0) + pair[1]
            }
        }
    }
    var all = []
    for (var key in hash)
        all.push([key, hash[key]])
    return all.sort(function(one, other){
        return other[1] - one[1]
    }).slice(0, 3)&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;div&gt;The approach I took was different one from the one from the example page, but I believe it to be the more &lt;i&gt;correct&lt;/i&gt; one: rather than returning the results keyed by the tag in the&amp;nbsp;&lt;i&gt;map&lt;/i&gt;&amp;nbsp;step, I would emit every occurrence of every tag instead. Then in the &lt;i&gt;reduce&lt;/i&gt;&amp;nbsp;step, I would calculate the aggregation values grouped by tag using a hash, transform it into an array, sort it, and choose the top 3. For the rereduce case, I would combine a set of top 3 choices and then again pick the top 3 among them.&lt;/div&gt;</description>
      <pubDate>Wed, 07 Oct 2009 00:33:29 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/Retrieve_The_Top_N_Tags_in_CouchDB</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Retrieve_The_Top_N_Tags_in_CouchDB</link>
    </item>
  </channel>
</rss>

