1

我正在使用 python-couchdb 库通过连续馈送来监听数据库中的变化。我想应用一个过滤器,它只给我那些键read等于true.

通过正常的 HTTP GET 请求,我得到了想要的结果。但我无法弄清楚如何通过couchdb-python图书馆做到这一点。这是我编写的自定义过滤器:

def read_true_filter():
    return """function(doc, req) {
            if(doc.read === true) {
                return true;
            }
            return false;
        }
        """

以下是我尝试聆听更改的方式:

db_changes = db.changes(
    feed='continuous',
    include_docs=True,
    heartbeat=1000,
    since=last_seq_id,
    filter=read_true_filter
)

但这给了我错误:

Traceback (most recent call last):
  File "../src/couch_pull_pipeline.py", line 87, in <module>
    db_changes = db.changes(
  File "/Users/sanyam/venv/lib/python3.5/site-packages/couchdb/client.py", line 1027, in _changes
    _, _, data = self.resource.get('_changes', **opts)
  File "/Users/sanyam/venv/lib/python3.5/site-packages/couchdb/http.py", line 546, in get
    return self._request('GET', path, headers=headers, **params)
  File "/Users/sanyam/venv/lib/python3.5/site-packages/couchdb/http.py", line 581, in _request
    credentials=self.credentials)
  File "/Users/sanyam/venv/lib/python3.5/site-packages/couchdb/http.py", line 421, in request
    raise ServerError((status, error))
couchdb.http.ServerError: (400, ('bad_request', 'filter parameter must be of the form `designname/filtername`'))

添加designname请求很简单,但我找不到使用 couchdb 客户端执行此操作的等效项。

是否可以使用 python 库或者我应该使用简单的 HTTP 请求,或者更好的想法是在 couchdb 服务器本身上安装过滤器?(根据我目前所阅读的内容,由于性能原因,在 couchdb 中使用该过滤器并不是一个好主意。)

有人可以告诉我我做错了什么/如何去做吗?

4

1 回答 1

1

我想通了。我在数据库中制作了一个设计文档,我想在其中过滤以下内容:

{
   "_id": "_design/read_validator",
   "_rev": "1-bd5fb337899a0eaf485b2112b439cc30",
   "filters": {
       "read_only_true": "function(doc, req) {if(doc.read === true) {return true;}return false;}"
   }
}

read_validator是包含要返回的过滤器的设计文档,该过滤器的docs属性read设置为true。在 couchdb python 客户端中,在获得连续提要时,我将过滤器资源路径作为字符串提供design_document_name/filter_name,其中design_document是在 db 中创建的设计文档的名称(在本例中为read_validator),并且filter_name是过滤器的名称(在本例中为是read_only_true)。所以,连接是这样的:

db_changes = db.changes(
    feed='continuous',
    include_docs=True,
    heartbeat=1000,
    since=last_seq_id,
    filter="read_validator/read_only_true"
)

db_changes将是一个连续的提要生成器,通过它可以通过迭代获取所有文档,其read键等于true.

于 2017-01-07T13:04:38.253 回答