如何让 Couchdb/Cloudant 设计文档准备好搜索所有字段?

How to get Couchdb/Cloudant design document ready for search all fields?

我有一个非常简单的数据库,其中包含一些存储在 Cloudant 中的数据模式。

{
  "_id": "units_modal_the_oaks_preliminary",
  "_rev": "1-b541013bc008680b706ea01969dedb7a",
  "type": "units_modal",
  "label": "Preliminary Modal",
  "notes": "Notes here...",
  "project": "the_oaks",
  "data": [...]
}

我将 Cloudant 与 PouchDB 连接起来。我的目标只是让这个数据库准备好通过除数据之外的所有字段进行查询。

我使用 PouchDB-Find 查询数据。我花了一整天的时间在创建设计文档和视图的文档上,但我不知道如何正确地做。所有文档都建议使用 map 函数和 reduce 函数。我尝试了各种方法来手动设计地图功能,但是当我 运行 查询

remoteDb.find({
    selector:{type:"units_modal"}
})

我收到 "no_usable_index" 错误。我只能使用 PouchDB-find 的 shipped 方法创建索引 & 然后我可以得到我想要的结果:

remoteDb.createIndex({
    index: {
        fields: ['type', 'project','label','notes']
    }
})

我看了设计文档。这是我得到的(我已经重命名了 id 和视图):

{
  "_id": "_design/project",
  "_rev": "8-c7e2b7c0e1dbaff8e5641a4f06075e14",
  "language": "query",
  "views": {
    "units_modal": {
      "map": {
        "fields": {
          "type": "asc",
          "project": "asc",
          "label": "asc",
          "notes": "asc"
        }
      },
      "reduce": "_count",
      "options": {
        "def": {
          "fields": [
            "type",
            "project",
            "label",
            "notes"
          ]
        },
        "w": 2
      }
    }
  }
}

所有这些都非常令人困惑。似乎除了地图函数之外,我找不到任何东西来解释地图。这里的地图功能在哪里?关于 map.fields、views.options.def 和 views.options.w 的文档和解释在哪里?

有人可以建议一种简单的方法来设计可以轻松查询有关特定类型文档的所有字段的视图吗?

任何人都可以建议一个地方,我可以得到更多关于 map.fields、view.options 和所有这些关于 CouchDB 的小东西的解释吗?

有没有简单的"if(type=='some type') indexAll();"解决方案?

非常感谢!

Where is the map function here?

映射函数用于在 couchdb 上创建二级索引,允许您通过特定键查询文档。默认情况下,couchdb 中只有一个索引可用,并且只能通过 _id 字段进行查询。

Cloudant 目前支持两种创建索引的方式。首先是借助 map 和 reduce 函数的传统方式。

然后他们有了一个新的 query api。在您的示例中,您使用的是查询 api[1]。

Could anybody suggest a simple way to design the views that can easily query all the fields about a certain type docs? Is there a simple "if(type=='some type') indexAll();" solution?

这是一个适合您的地图函数

function(doc){
for(var prop in Object.keys(doc))
if(prop!=="data") emit(doc[prop]);
}

这使得除 "data" 之外的所有字段都可查询。

这在设计文档的什么地方?这里

"views": {
    "units_modal": {
      "map": "function(doc){for(var prop in Object.keys(doc))if(prop!=="data"){emit(doc[prop])};
}"
      }

这里要注意的一点是,设计文档与任何其他存储 json 数据的 couchdb 文档一样。但是 json 中不允许使用函数。因此,您要么必须手动对它们进行字符串化,要么使用可以帮助您做到这一点的库,例如 couchapp

pouchdb 的索引将用于客户端数据库。因此,如果您在 pouchdb 数据库上创建索引,那么这些索引将无法与您的远程 cloudant/couchdb 数据库一起使用。

但是api查询和创建索引是完全一样的。

此外,如果您想在 pouchdb 中使用 cloudant "query syntax" 样式查询,可以使用 plugin 来帮助您做到这一点。

[1]在 couchdb 中,目前只允许使用 map reduce 函数(直到 1.6.1)。但他们也在努力在 2.0 中合并查询语法。

首先,很抱歉让您感到困惑。 Cloudant Query 是 Cloudant/CouchDB 中的一项相对较新的功能,实际上是对现有的低级别索引机制(map/reduce 和搜索)的包装。用户创建的 map/reduce 或 Cloudant 搜索索引不能用于服务 Cloudant 查询调用 - 它维护自己的索引。

Cloudant Query 要求存在合适的索引来为对 _find 的请求提供服务,因此您会收到错误消息。 Cloudant Query 支持(当前)两种不同类型的索引 - JSON(包装 map/reduce)和文本(包装 Cloudant Search/Lucene)。文本索引更灵活,JSON 索引性能更高。

文本索引的默认行为是索引每个字段,我们建议从这个开始,除非您知道您的数据库将变得非常大(100 GB)或复杂。您可以 create one 通过 PouchDB 使用:

remoteDB.createIndex({
   name: "myindex",
   index": {},
   type: "text"
}).then(function (result) {
   // yo, a result
}).catch(function (err) {
   // ouch, an error
});

或通过 CURL:

curl -u <username> 'https://<username>.cloudant.com/<db>/_index' -X POST -H'Content-Type:application/json' -d '{"index":{},"type":"text"}'

如果要更改索引以便省略"data",可以指定要索引的字段:

remoteDB.createIndex({
   name: "myindex",
   index": {
       "fields": ["_id","_rev","type","label","notes","project"]
   },
   type: "text"
}).then(function (result) {
   // yo, a result
}).catch(function (err) {
   // ouch, an error
});

创建该索引后,您的 PouchDB-Find 查询应该可以工作。

如果您想详细了解 Cloudant 中不同类型的查询机制(Cloudant 查询、Cloudant 搜索和 map/reduce),learning centre 是一个很好的起点。