Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 781045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:10:16+00:00 2026-05-14T20:10:16+00:00

I’m testing out CouchDB to see how it could handle logging some search results.

  • 0

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. At the moment I have something like this:

Example document portion

{
  "query": "+dangerous +dogs",
  "hits": "123"
}

Map function
(Not exactly what I need/want but it’s good enough for testing)

function(doc) {
  if (doc.query) {
    var split = doc.query.split(" ");
    for (var i in split) {
      emit(split[i], 1);
    }
  }
}

Reduce Function

function (key, values, rereduce) {
  return sum(values);
}

Now this will get me results in a format where a query term is the key and the count for that term on the right, which is great. But I’d like it ordered by the value, not the key. From the sounds of it, this is not yet possible with CouchDB.

So does anyone have any ideas of how I can get a view where I have an ordered version of the query terms & their related counts? I’m very new to CouchDB and I just can’t think of how I’d write the functions needed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:10:16+00:00Added an answer on May 14, 2026 at 8:10 pm

    It is true that there is no dead-simple answer. There are several patterns however.

    1. http://wiki.apache.org/couchdb/View_Snippets#Retrieve_the_top_N_tags. I do not personally like this because they acknowledge that it is a brittle solution, and the code is not relaxing-looking.

    2. Avi’s answer, which is to sort in-memory in your application.

    3. couchdb-lucene which it seems everybody finds themselves needing eventually!

    4. What I like is what Chris said in Avi’s quote. Relax. In CouchDB, databases are lightweight and excel at giving you a unique perspective of your data. These days, the buzz is all about filtered replication which is all about slicing out subsets of your data to put in a separate DB.

      Anyway, the basics are simple. You take your .rows from the view output and you insert it into a separate DB which simply emits keyed on the count. An additional trick is to write a very simple _list function. Lists “render” the raw couch output into different formats. Your _list function should output

      { "docs":
          [ {..view row1...},
            {..view row2...},
            {..etc...}
          ]
      }
      

      What that will do is format the view output exactly the way the _bulk_docs API requires it. Now you can pipe curl directly into another curl:

      curl host:5984/db/_design/myapp/_list/bulkdocs_formatter/query_popularity \
       | curl -X POST host:5984/popularity_sorter/_design/myapp/_view/by_count
      
    5. In fact, if your list function can handle all the docs, you may just have it sort them itself and return them to the client sorted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.