问题
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.
Access with direct query.
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
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.
回答1:
If you are using Couch 2.1+, you may employ JS rewrites to use plain JS function for request routing.
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.
However, using JS rewrites you also have all JS query server penalties. They are:
- 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.
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.
来源:https://stackoverflow.com/questions/48978344/append-to-variable-in-couchdb-rewrite-rule