How do you sort _View_ results by value in Couchbase?

So, from what I understand in Couchbase, you can sort the keys * using

descending=true 

but in my case I want to sort by values. View Twitter data in json format, my question is What did he call the most popular user?

Each tweet has the structure:

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

So, using MongoDB, before reusing the Map function and modifying it a bit, you can use it 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 _count decrease _count to count all occurrences of screen_name . Now my problem is How to sort by counter values, and not by key?

thanks

+4
source share
2 answers

Short answer: you cannot sort by value the result of your viewing. You can sort by key only.

Some works will either:

  • analyze the data before inserting it into Couchbase and create a counter for the values ​​you are interested in (mentioned in your case)

  • use the view that you want to sort by size of the application if the size of the view is acceptable for sorting on the client side.

The following JS code invokes a view, sorts the result, and prints the 10 hottest items (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(); 

At the same time, I am considering other options to achieve this.

+1
source

I solved this with a complex key.

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

Urls:

 &startkey=["jim",5]&endkey=["jim",10]&descending=true 
+1
source

Source: https://habr.com/ru/post/1444854/


All Articles