同时或随后在 Couchdb 中插入和索引

inserting and indexing in Couchdb at the same time or consequently

我是 couchdb 的新手,正在尝试保存一些文档,同时为它们编制索引。

目前我做的是:

app.post("/api/result-store/v1/results/:searchID", (req, res) => {
  const dbName = "test_" + req.params.searchID;
  database.checkAndCreateDatabase(dbName).then(
    db => {
      const docs = req.body.objects;
      db.bulk({ docs }).then(
        body => {
          res.send({
            writes: body.reduce((total, currentValue) => {
              return total + (currentValue.ok ? 1 : 0);
            }, 0)
          });
        },
        err => {
          res.send(err);
        }
      );
    },
    err => {
      console.log(err);
    }
  );
});

所以我上面所做的只是保存但没有索引。现在,如果我像这样查询数据库:

{{url}}/api/result-store/v1/results/jmeter_test_db_size_90_k?q=*:*&limit=200&counts=["qid_name", "datasource"]

然后索引将开始。但这为时已晚,因为索引需要时间,客户需要等到索引完成才能得到结果。 我正在考虑在插入文档后立即开始索引数据,以便同时或随后保存和索引。有可能吗?任何见解表示赞赏

由于您正在使用 nano,您可以通过调用 db.createIndex just after creating the database (creates an index on database fields, as specified in CouchDB doc).

创建索引