Sorting CouchDB Views By Value

前端 未结 7 1549
独厮守ぢ
独厮守ぢ 2020-12-04 08:52

I\'m testing out CouchDB to see how it could handle logging some search results. What I\'d like to do is produce a view where I can produce the top queries from the results

7条回答
  •  情话喂你
    2020-12-04 09:03

    Based on Avi's answer, I came up with this Couchdb list function that worked for my needs, which is simply a report of most-popular events (key=event name, value=attendees).

    ddoc.lists.eventPopularity = function(req, res) {
      start({ headers : { "Content-type" : "text/plain" } });
      var data = []
      while(row = getRow()) {
        data.push(row);
      }
      data.sort(function(a, b){
        return a.value - b.value;
      }).reverse();
      for(i in data) {
        send(data[i].value + ': ' + data[i].key + "\n");
      }
    }
    

    For reference, here's the corresponding view function:

    ddoc.views.eventPopularity = {
      map : function(doc) {
        if(doc.type == 'user') {
          for(i in doc.events) {
            emit(doc.events[i].event_name, 1);
          }
        }
      },
      reduce : '_count'
    }
    

    And the output of the list function (snipped):

    165: Design-Driven Innovation: How Designers Facilitate the Dialog
    165: Are Your Customers a Crowd or a Community?
    164: Social Media Mythbusters
    163: Don't Be Afraid Of Creativity! Anything Can Happen
    159: Do Agencies Need to Think Like Software Companies?
    158: Customer Experience: Future Trends & Insights
    156: The Accidental Writer: Great Web Copy for Everyone
    155: Why Everything is Amazing But Nobody is Happy
    

提交回复
热议问题