在Hyperledger Composer v0.18.0到v0.19.0的查询中使用ORDER BY的CouchDB索引错误

时间:2018-04-04 21:17:57

标签: couchdb hyperledger-fabric hyperledger hyperledger-composer

从Hyperledger Composer v0.18.0迁移到v0.19.0时,我在使用ORDER BY语句的查询中遇到CouchDB索引问题。

  

上下文:

名为Event的简单资产:

asset Event identified by id {
    o String id
    o String name
    o EventType eventType
    o DateTime scheduledTime
    o EventStatus status
}

使用ORDER BY语句的查询:

query getScheduledEvents {
    description: "Select all the SCHEDULED Events"
    statement:
        SELECT event.network.assets.Event
            WHERE (status == 'SCHEDULED')
                ORDER BY [scheduledTime ASC]
}
  

HL Composer v0.18.0

使用Composer v0.18.0一切正常。我可以从REST API查询事件,只需在CouchDB上创建这个索引:

{
    "type": "json",
    "def": {
    "fields": [
            {
                "scheduledTime": "asc"
            }
        ],
        "partial_filter_selector": {}
     }
}
  

HL Composer v0.19.0

从Composer v0.19.0(和Fabric 1.1.0)开始,将在部署中自动生成索引,新索引如下所示:

{
    "type": "json",
    "def": {
        "fields": [
            {
                "\\$class": "asc"
            },
            {
                "\\$registryType": "asc"
            },
            {
                "\\$registryId": "asc"
            },
            {
                "status": "asc"
            },
            {
                "scheduledTime": "asc"
            }
        ],
        "partial_filter_selector": {}
    }
}

现在,使用Composer v0.19.0,查询同一个端点,我收到以下错误:

{
"error": {
    "statusCode": 500,
    "name": "Error",
    "message": "2 UNKNOWN: error executing chaincode: transaction returned with failure: Error: Couch DB Error:no_usable_index,  Status Code:400,  Reason:No index exists for this sort, try indexing by the sort fields.",
    "code": 2,
    "metadata": {
        "_internal_repr": {}
    },
    "details": "error executing chaincode: transaction returned with failure: Error: Couch DB Error:no_usable_index,  Status Code:400,  Reason:No index exists for this sort, try indexing by the sort fields.",
"stack": "Error: 2 UNKNOWN: error executing chaincode: transaction returned with failure: Error: Couch DB Error:no_usable_index,  Status Code:400,  Reason:No index exists for this sort, try indexing by the sort fields.\n    at new createStatusError (/home/user/.nvm/versions/node/v8.11.1/lib/node_modules/composer-rest-server/node_modules/grpc/src/client.js:64:15)\n    at /home/user/.nvm/versions/node/v8.11.1/lib/node_modules/composer-rest-server/node_modules/grpc/src/client.js:583:15"
    }
}

删除生成的索引并创建旧索引无法解决问题,因为我仍然收到相同的错误。

有什么想法? 谢谢!

1 个答案:

答案 0 :(得分:2)

我能够通过类似的查询重新创建它,并发现我可以通过重新排列索引文档中的字段来使其工作。在您的情况下,它将安排如下:

{
    "type": "json",
    "def": {
        "fields": [
            {
                "\\$class": "asc"
            },
            {
                "\\$registryType": "asc"
            },
            {
                "\\$registryId": "asc"
            },
            {
                "scheduledTime": "asc"
            },
            {
                "status": "asc"
            }
        ],
        "partial_filter_selector": {}
    }
}

我认为这与this post

的子弹3中的解释有关