在沙发床重写规则中追加到变量 [英] append to variable in couchdb rewrite rule
问题描述
我正在尝试创建一个重写规则,以读取一个以字符串开头的键的视图。
I am trying to create a rewrite rule, to read a view with keys that start with a string.
我可以在不进行重写的情况下做到这一点,但很费劲
I can do this without the rewrite, but am struggling to do it with the rewrite.
curl -g -H "Accept: application/json" \
'http://127.0.0.3:80/bookmarks/_design/design/_view/webmarks-by-url?include_docs=true&reduce=false&startkey=["www.sciencemuseum.org.uk"]&endkey=["www.sciencemuseum.org.uk/\ufff0"]' | json_pp
然后我尝试使用rewrites.json
Then I try to do it with a rewrites.json
{
"from": "bookmarks_by_url/:~name:",
"to": "_view/webmarks-by-url",
"query": {
"reduce": "false",
"include_docs": "true",
"descending" : "false",
"startkey": [":~name:"],
"endkey": [":~name:\ufff0"]
}
},
我想要的东西,但这显然行不通。有没有办法说这是变量的结尾?还是写规则的另一种方式?
What I want, but this is obviously not going to work. Is there a way to say this is the end of a variable? Or another way to write the rule?
{
"from": "bookmarks_by_url/:~name:",
"to": "_view/webmarks-by-url",
"query": {
"reduce": "false",
"include_docs": "true",
"descending" : "false",
"startkey": [":~name:"],
"endkey": [":~name:" + "\ufff0"]
}
},
有关重写规则的文档非常糟糕,如果您知道更好的规则,请链接。
The documentation for rewrite rules are terrible, If you know of better ones then please link.
推荐答案
如果您使用的是Couch 2.1+,您可以使用 JS重写使用普通JS函数进行请求路由。
If you are using Couch 2.1+, you may employ JS rewrites to use plain JS function for request routing.
具有 .rewrites
部分,其中包含路由器函数的字符串表示形式,您可以处理非常复杂的路由。此外,您甚至可以直接通过重写功能提供响应。
Having .rewrites
section, containing a string representation of a router function, you can handle very complex routing. Moreover, you can even serve a response directly from rewrite function.
但是,使用JS重写,您还会受到所有JS查询服务器的处罚。它们是:
However, using JS rewrites you also have all JS query server penalties. They are:
- 为每个请求延迟添加几毫秒。
- 您的重写fn是单线程瓶颈,您无法处理超出重写fn能够处理的请求。
- 由于Couch中的每个JS引擎实例都只有64Mb的RAM用于编译代码和数据,您无法处理大型POST请求。
- Couch中的所有QS功能都很难调试,重写也不例外。
- 您可以
- You add several ms to each request latency.
- Your rewrite fn is a single-threaded bottleneck, you can’t handle more requests than your rewrite fn is able to process.
- Since each JS engine instance in Couch has only 64Mb RAM for both compiled code and data, you can’t process large POST requests.
- All QS functions in Couch are hard to debug, rewrite is not an exception.
- You can’t use both old array syntax (which is waaay faster), and a function in one design doc.
因为我亲自雇用JS重写,所以既要使用旧的数组语法(速度更快),又要在一个设计文档中使用函数。在很大程度上,我可以说优点比缺点要重得多。 有一篇文章,其中包含一些与JS重写有关的性能细节。
Since I personally employ JS rewrite heavily, I can say pros are much heavier than cons. There is an article with some perf details related to JS rewrites.
这篇关于在沙发床重写规则中追加到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!