如何在Couchbase中按值对_View_的结果进行排序? [英] How do you sort results of a _View_ by value in the in Couchbase?

查看:74
本文介绍了如何在Couchbase中按值对_View_的结果进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,根据我在Couchbase中的理解,可以通过使用

So from what I understand in Couchbase is that one can sort keys* by using

descending=true

但是在我的情况下,我想按值排序.考虑json格式的Twitter数据,我的问题是最受欢迎的用户是什么?

but in my case I want to sort by values instead. Consider the Twitter data in json format, my question is What it the most popular user mentioned?

每条推文都具有以下结构:

Each tweet has the structure of:

{
    "text": "",
    "entities" : {
        "hashtags" : [ ... ],
        "user_mentions" : [ ...],
        "urls" : [ ... ]
}

因此在重用Map函数之前使用过MongoDB,并对其进行了如下修改以使其可在Couchbase中使用:

So having used MongoDB before I reused the Map function and modified it slightly to be usable in Couchbase as follows:

function (doc, meta) {
  if (!doc.entities) { return; }

  doc.entities.user_mentions.forEach(
    function(mention) {
      if (mention.screen_name !== undefined) {
        emit(mention.screen_name, null);
      }
    }
  )
}

然后,我使用化简函数_count来计数所有screen_name出现的次数.现在我的问题是如何按计数值而不是键进行排序?

And then I used the reduce function _count to count all the screen_name occurrences. Now my problem is How do I sort by the count values, rather than the key?

谢谢

推荐答案

最简单的答案是您无法按值对查看结果进行排序.您只能按键排序.

The short answer is you cannot sort by value the result of you view. You can only sort by key.

某些解决方法将是:

  • 在将数据插入Couchbase之前分析数据,并为您感兴趣的值创建计数器(针对您的情况)

  • analyze the data before inserting them into Couchbase and create a counter for the values you are interested by (mentions in your case)

使用视图,如果客户端的排序可接受,则必须根据应用程序的大小对其进行排序.

use the view you have to sort on the application size if the size of the view is acceptable for a client side sort.

以下JS代码调用一个视图,对结果进行排序,并打印出10个最热门的主题(标签):

The following JS code calls a view, sorts the result, and prints the 10 hottest subjects (hashtags):

var http =  require('http');

var options = {
    host: '127.0.0.1',
    port: 8092,
    path: '/social/_design/dev_tags/_view/tags?full_set=true&connection_timeout=60000&group=true',
    method: 'GET'
}

http.request(
    options, 
    function(res) {
        var buf = new Buffer(0);
        res.on('data', function(data) {
            buf += data;
        });
        res.on('end', function() {
            var tweets = JSON.parse(buf);
            var rows = tweets.rows;
            rows.sort( function (a,b){ return b.value - a.value } 
            );


            for ( var i = 0;  i < 10; i++ ) {
                console.log( rows[i] );
            }
        });
    }
    ).end();

与此同时,我正在寻找实现这一目标的其他选择

In the same time I am looking at other options to achieve this

这篇关于如何在Couchbase中按值对_View_的结果进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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