重写将标记转换为整数参数的规则
经过一番思考对记录进行排名的想法后,我最终决定为我的文档选择基于数字的分数,我发出这些分数是为了根据这些分数对它们进行排序。
现在这些数字有意义,其中前 2 位数字代表特定类型的文档。
因此,要获取根据分数排序的类型 22 的文档,我只需查询起始键为 220000 且结束键为 229999 的视图。
这一切都很好并且有效,当我尝试使用 url 重写时,我的问题出现了。
我基本上正在尝试重新路由:
/_rewrite/rankings/{doctype}
到
/_list/rankings?startkey=xx0000&endkeyxx9999
xx 是 {doctype} 的位置,
我的问题是指定重写规则:
[
{ "from":"rankings/:doctype",
"to":"_list/rankings",
"query": ??? //what will this be?
]
如何通过分别附加 0000 和 9999 来构造开始和结束键?
如何指定数值?因为使用占位符“:doctype”将导致字符串类型而不是数字类型,从而导致查询失败,即使我修改漂亮的网址以输入开始键和结束键也是如此。
我通过过滤列表视图中的结果解决了这个问题(忽略 getRow() 中我不感兴趣的文档),我在这里担心,我现在应该担心列表函数的效率吗?
请随意评论我的排序策略..有兴趣知道其他人如何使用 couchdb 解决排序和切片问题
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案
首先,您应该在数组中分别发出 type 和 score 而不是连接它们:
然后您可以像这样重写
我在 CouchDB 1.1.1 上测试了它,它作品。
参考
相关文档隐藏在JIRA上的这个问题中: COUCHDB-1074
可以看到,该问题已于 2011 年 4 月得到解决,因此它应该可以在 CouchDB 1.0.3 及更高版本中运行。
Solution
First, you should emit the type and the score separately in an array instead of concatenating them:
Then you can rewrite like this
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.