4

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

descending=true

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" : [ ... ]
}

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);
      }
    }
  )
}

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?

Thanks

Pavel Paulau
  • 786
  • 2
  • 7
  • 19
chutsu
  • 13,612
  • 19
  • 65
  • 86

2 Answers2

1

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

Some work around will be to either:

  • 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.

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

boycaught
  • 15
  • 4
Tug Grall
  • 3,410
  • 1
  • 14
  • 16
  • Hmmm I'm very reluctant in accepting this answer, although I know "you cannot sort by value", I think its a problem that will eventually be solved by couchbase developers, so for the longevity of this question... – chutsu Nov 15 '12 at 11:02
  • @chutsu, the answer that I gave is for Couchbase 2.0 (and you are right in the future product Couchbase will probably provide way to achieve this)Pavel's question is a classic one when working with Couchbase view this is why I wanted to make ti clear. – Tug Grall Nov 16 '12 at 08:56
  • "Pavel's question is a classic one", you mean my question? :p – chutsu Nov 16 '12 at 13:35
  • I am using 2.2 and still same problem, does anyone solve this problem? – Prakash Thapa Jan 23 '14 at 14:26
1

I solved this by using a compound key.

function (doc, meta) {
  emit([doc.constraint,doc.yoursortvalue]);
}

url elements:

&startkey=["jim",5]&endkey=["jim",10]&descending=true
enko
  • 615
  • 6
  • 20