Couchbase
  • Why NoSQL?
  • Couchbase Server
  • Download
  • Resources
  • Careers
Home | Forums | Couchbase | Couchbase Server 2.0

How do you sort results of a "View" by value in the in Couchbase?

2 replies [Last post]
  • Login or register to post comments
Fri, 11/09/2012 - 05:18
chutus
Offline
Joined: 11/09/2012
Groups: None

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

Top
  • Login or register to post comments
Sat, 11/10/2012 - 13:13
mikew
Offline
Joined: 03/14/2011
Groups:

The short answer here is that you can't sort reduced results. I have seen this question before and I think it is something we need to have documented. I am going to follow up with some of our engineers that work on our view engine to see if there is a way we can advise people to rewrite their map and reduce functions in order to get the proper behavior. The issue is linked below and you can follow the progress there.

http://www.couchbase.com/issues/browse/MB-7151

Top
  • Login or register to post comments
Sun, 11/11/2012 - 05:08
tgrall
Offline
Joined: 09/05/2012
Groups: None

To add to Mikew's answer...

Since you cannot sort by value, you may have to change a little bit your application (for now). For ths you have 2 options:

- analyze the data before inserting them into Couchbase and create a counter for the values your 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 call a view and sort the result, and print 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

__________________

Tug
@tgrall

Top
  • Login or register to post comments
  • Login or register to post comments
  • Login
  • Register

Company

  • About Us
  • Leadership
  • Customers
  • Partners
  • Contact Us

Product

  • Couchbase Server
  • Couchbase SDKs
  • Use Cases
  • Documentation
  • Forums

Open Source

  • Couchbase Project
  • Couchbase vs. CouchDB

Commercial

  • Subscriptions & Support
  • Training & Services

News

  • Blog
  • Newsletter
  • Press Releases
  • Buzz

Follow Us

    
  • Customer Login
  • Terms of Service
  • Privacy Policy
  • Trademark Policy
  • Site Map

© 2013 COUCHBASE All rights reserved.

Sign in to Couchbase Community

close
  • Create new account
  • Request new password
You are logging into the Forums, Wiki and Issue Tracker