Note that there are some explanatory texts on larger screens.

plurals
  1. POMap-Reduce Query to Count Tags
    text
    copied!<p>I have a database of documents which are tagged with keywords. I am trying to find (and then count) the unique tags which are used alongside each other. So for any given tag, I want to know what tags have been used alongside that tag.</p> <p>For example, if I had one document which had the tags <code>[fruit, apple, plant]</code> then when I query <code>[apple]</code> I should get <code>[fruit, plant]</code>. If another document has tags <code>[apple, banana]</code> then my query for <code>[apple]</code> would give me <code>[fruit, plant, banana]</code> instead.</p> <p>This is my map function which emits all the tags and their neighbours:</p> <pre><code>function(doc) { if(doc.tags) { doc.tags.forEach(function(tag1) { doc.tags.forEach(function(tag2) { emit(tag1, tag2); }); }); } } </code></pre> <p>So in my example above, it would emit</p> <pre><code>apple -- fruit apple -- plant apple -- banana fruit -- apple fruit -- plant ... </code></pre> <p>My question is: what should my reduce function be? The reduce function should essentially filter out the duplicates and group them all together.</p> <p>I have tried a number of different attempts, but my database server (CouchDB) keeps giving me a <strong>Error: reduce_overflow_error. Reduce output must shrink more rapidly</strong>.</p> <hr> <p><strong>EDIT: I've found something that seems to work, but I'm not sure why.</strong> I see there is an optional "rereduce" parameter to the reduce function call. If I ignore these special cases, then it stops throwing reduce_overflow_errors. Can anyone explain why? And also, should I just be ignoring these, or will this bite me in the ass later?</p> <pre><code>function(keys, values, rereduce) { if(rereduce) return null; // Throws error without this. var a = []; values.forEach(function(tag) { if(a.indexOf(tag) &lt; 0) a.push(tag); }); return a; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload