如果数组中的项目存在,则创建一个 couchdb 视图以进行索引

Creating a couchdb view to index if item in an array exists

我的 couchdb 中有以下示例文档。生产中的原始 table 有大约 2M 条记录。

{ 
  {
      "_id": "someid|goes|here",
      "collected": {
          "tags": ["abc", "def", "ghi"]
      }
  },
  {
      "_id": "someid1|goes|here",
      "collected": {
           "tags": ["abc", "klm","pqr"]
      },
  },
  {
      "_id": "someid2|goes|here",
      "collected": {
           "tags": ["efg", "hij","klm"]
      },
  }
}

根据我之前在这里的问题,how to search for values when the selector is an array, 我目前为 collected.tags 字段添加了索引,但搜索仍然需要很长时间。这是我的搜索查询。

{
  "selector": {
    "collected.tags": {
      "$elemMatch": {
        "$regex": "abc"
      }
    }
  }
}

符合上述条件的记录大约有30万条,搜索起来好像要花很长时间。所以,我想创建一个索引视图来更快地检索和查找,而不是 find/search。我是 couchdb 的新手,不确定如何设置地图函数来创建索引视图。

自己想出了地图功能。现在所有文档都已编入索引并且检索速度更快

function (doc) {
  if(doc.collected.tags.indexOf('abc') > -1){
    emit(doc._id, doc);
  }
}