CouchDB/Couchbase视图按键数排序 [英] CouchDB / Couchbase view ordered by number of keys

查看:99
本文介绍了CouchDB/Couchbase视图按键数排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个视图,向我显示系统中使用的前10个标签.在reduce函数中使用_count来获取金额是相当容易的,但这并不能通过数字对列表进行排序.有什么办法吗?

I'm trying to write a view which shows me the top 10 tags used in my system. It's fairly easy to get the amount with _count in the reduce function, but that does not order the list by the numbers. Is there any way to do this?

function(doc, meta) {
  if(doc.type === 'log') {
    emit(doc.tag, 1);
  }
}
_count

因此,我希望拥有:

  • Tag3 10
  • Tag1 7
  • Tag2 3
  • ...

代替

  • Tag1 7
  • Tag2 3
  • Tag3 10

最重要的是,我不想将全套转移到我的应用程序服务器上并在那里进行处理.

Most importantly, I do not want to transfer the full set to my application server and handle it there.

推荐答案

在沙发床中,您无法在reduce中/之后对结果进行排序,因此您无法直接获得某些东西的前10名".在榻榻米视图中,值始终按键排序.最好的方法是:

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:

  1. 查询返回键-值对的视图:tag_name - count_valuetag_name
  2. 排序
  3. 创建每N分钟运行一次的作业,该作业从[1]获取结果,对结果进行排序,然后将排序后的结果写入单独的键(即"Top10Tags").
  4. 在您的应用中,您查询关键的Top10Tags.
  1. Query your view that returns key-value pair: tag_name - count_value ordered by tag_name
  2. Create job that runs every N minutes, that gets results from [1], sorts them, and writes sorted results to separate key (i.e. "Top10Tags").
  3. In your app you query key Top10Tags.

这可以减少流量,但结果可能过时.另外,您也可以在沙发床运行的同一台服务器上创建该作业"(即编写小型的node.js应用或其他内容),并且它仅计算回送流量和少量的CPU,即可每隔N分钟进行排序.

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.

此外,如果您使用_count reduce函数,则不需要发出任何数字,只需使用null:

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"]
}

您的地图功能应为:

function(doc, meta) {
  if(meta.type === "json" && doc.type === 'log') {
    for(var i = 0; i < doc.tags.length; i++){
      emit(doc.tags[i], null);
    }
  }
}

关于前10名列表的另一件事.如果您不想将其存储在磁盘上,可以将其存储在内存缓存存储区中.

One more thing about that top10 list. You can store it in memcache bucket if you don't want to store it on disk.

这篇关于CouchDB/Couchbase视图按键数排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆