Sorting CouchDB Views By Value

前端 未结 7 1506
独厮守ぢ
独厮守ぢ 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:12

    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.

提交回复
热议问题