Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your answer is nice, and as I said in the comments, if it works for you, that's all you should care about. Here is an alternative implementation in case you ever bump into performance problems.</p> <p>CouchDB likes tall lists, not fat lists. Instead of view rows keeping an array with every previous tag ever seen, this solution keeps the "sibling" tags in the <strong>key</strong> of the view rows, and then <a href="http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Grouping" rel="nofollow">group them together</a> to guarantee one unique sibling tag per row. Every row is just two tags, but there could be thousands or millions of rows: a tall list, which CouchDB prefers.</p> <p>The main idea is to emit a 2-array of tag pairs. Suppose we have one doc, tagged <code>fruit, apple, plant</code>.</p> <pre><code>// Pseudo-code visualization of view rows (before reduce) // Key , Value [apple, fruit ], 1 [apple, plant ], 1 // Basically this is every combination of 2 tags in the set. [fruit, apple ], 1 [fruit, plant ], 1 [plant, apple ], 1 [plant, fruit ], 1 </code></pre> <p>Next I tag something <code>apple, banana</code>.</p> <pre><code>// Pseudo-code visualization of view rows (before reduce) // Key , Value [apple, banana], 1 // This is from my new doc [apple, fruit ], 1 [apple, plant ], 1 // This is also from my new doc [banana, apple], 1 [fruit, apple ], 1 [fruit, plant ], 1 [plant, apple ], 1 [plant, fruit ], 1 </code></pre> <p>Why is the value always <code>1</code>? Because I can make a very simple built-in reduce function: <code>_sum</code> to tell me the count of all tag pairs. Next, query with <code>?group_level=2</code> and CouchDB will give you unique pairs, with a count of their total.</p> <p>A map function to produce this kind of view might look like this:</p> <pre><code>function(doc) { // Emit "sibling" tags, keyed on tag pairs. var tags = doc.tags || [] tags.forEach(function(tag1) { tags.forEach(function(tag2) { if(tag1 != tag2) emit([tag1, tag2], 1) }) }) } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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