Hyperledger Fabric CouchDB 索引:"no matching index found"

Hyperledger Fabric CouchDB index: "no matching index found"

我在 META-INF\statedb\couchdb\indexes 目录中的链代码旁边创建了一个索引。索引的 json 如下所示:

{
    "index": {
        "fields": ["id", "docType", "type"]
    },
    "ddoc" : "indexTestDoc",
    "name" : "indexTest",
    "type" : "json"
}

我可以通过查看日志确认索引正在正确创建和更新:

 Created CouchDB index [indexTest] in state database [integrity-channel_approvalcc] using design document [_design/indexTestDoc]\n","stream":"stderr","time":"2019-08-15T10:48:01.42015813Z"}

问题是查询不使用索引,尽管我在查询字符串中指定了 use_index 属性:

"{\"selector\":{\"docType\":\"foo\", \"type\":\"bar\"}, \"use_index\":[\"_design/indexTestDoc\", \"indexTest\"]}"

我将 id 作为索引中的附加字段是否有问题?

您的索引位于 ["id","docType","type"],但您没有在 id 上查询,因此无法使用该索引。

从您的索引中删除 id(推荐,因为很少(如果有的话)有任何理由显式索引 id):

{
    "index": {
        "fields": ["id", "docType", "type"]
    },
    "ddoc" : "indexTestDoc",
    "name" : "indexTest",
    "type" : "json"
}

或将其添加到您的查询中:

{
  "selector": {
    "docType": "foo",
    "type": "bar",
    "id": "baz"
  },
  "use_index": [
    "_design/indexTestDoc",
    "indexTest"
  ]
}

如果您确实需要所有三个字段的索引,但只想查询两个,请确保您查询的两个在前。 IE。将 id 放在最后:

{
    "index": {
        "fields": ["docType", "type", "id"]
    },
    "ddoc" : "indexTestDoc",
    "name" : "indexTest",
    "type" : "json"
}

如上所述,很少(如果有的话)有任何理由在 id 上创建手动索引,无论如何,因为它已经是唯一的,并且已经有一个内置索引。我想不出任何您想在 id 上创建索引的用例,所以只需将其从索引中删除即可。但其他解决方案仍然适用于其他索引。