开发者

rewrite rules that converts tokens to integer parameters

After much wrestling with the idea of ranking records, I finally settled on numeric based scores for my documents, which I emit to have them sorted based on these scores.

Now these numbers have meaning, where the 1st 2 digits represent a specific type of document.

Therefore, to get documents of type 22 sorted based on their scores, I simply query the view with start key being 220000 and end key being 229999

This is all great and works, my problems occur when I try to use url rewrites.

I'm basically trying to reroute:

/_rewrite/rankings/{doctype}

to

/_list/rankings?startkey=xx0000&endkeyxx9999

where xx is the {doctype}

my issue is with specifying rewrite rule:

[
    { "from":"rankings/:doctype",
      "to":"_list/rankings",
      "query": ??? //what will this be?
]
  • How can I construct the start and end keys by appending 0000 and 9999 respectively?

  • how can I specify a numeric value? since using place holder ":doctype" will 开发者_如何学运维result in a string type rather than a numberic type, resulting in a failed query even if I were to modify my pretty url to input both start and end keys.

  • I worked around the issue by filtering the results in my list view (ignoring docs im not interested in from getRow()), my concern here, should I worry about efficiency of list function now?

feel free to comment also on my sorting strategy .. would be interested to know how others solved their sorting and slicing problems with couchdb


Solution

First, you should emit the type and the score separately in an array instead of concatenating them:

emit([doc.type, doc.score], doc);

Then you can rewrite like this

[
  {
    "from"  : "rankings/:doctype",
    "to"    : "_list/rankings/rankings",
    "query" : {
      "startkey" : [":doctype", 0],
      "endkey"   : [":doctype", 9999]
    },
    "formats": {
      "doctype" : "int"
    }
  }
]

I tested it on CouchDB 1.1.1 and it works.

Reference

The relevant documentation is buried in this issue on JIRA: COUCHDB-1074

As you can see, the issue was resolved on April 2011, so it should work in CouchDB 1.0.3 and above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜