重写将标记转换为整数参数的规则

发布于 2024-11-11 16:37:06 字数 851 浏览 4 评论 0原文

经过一番思考对记录进行排名的想法后,我最终决定为我的文档选择基于数字的分数,我发出这些分数是为了根据这些分数对它们进行排序。

现在这些数字有意义,其中前 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七分※倦醒 2024-11-18 16:37:06

解决方案

首先,您应该在数组中分别发出 typescore 而不是连接它们:

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

然后您可以像这样重写

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

我在 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文