In couchbase you can't sort result in/after reduce, so you can't directly get "Top 10" of something. In couchbase views values are always sorted by key. The best way is:
- Query your view that returns key-value pair:
tag_name - count_value
ordered by tag_name
- Create job that runs every N minutes, that gets results from [1], sorts them, and writes sorted results to separate key (i.e. "Top10Tags").
- In your app you query key Top10Tags.
This could reduce traffic, but results can be outdated. Also you can create that "job" on same server that couchbase runs (i.e. write small node.js app or something else) and it counsume just loopback traffic and small cpu amount for sorting every N mins.
Also, if you're using _count reduce function, you don't need to emit any numbers, use just null:
function(doc, meta) {
if(meta.type === "json" && doc.type === 'log') {
emit(doc.tag, null);
}
}
And if you want to have docs tagged by multiple tags like
{
"type": "log",
"tags": ["tag1","tag2","tag3"]
}
Your map function should be:
function(doc, meta) {
if(meta.type === "json" && doc.type === 'log') {
for(var i = 0; i < doc.tags.length; i++){
emit(doc.tags[i], null);
}
}
}
One more thing about that top10 list. You can store it in memcache bucket if you don't want to store it on disk.