首页 > 解决方案 > 如何在超级账本结构中索引 couchdb

问题描述

我正在尝试按我定义了索引的发布日期对结果进行排序

{
    "index": {
        "fields": [{
            "PostingDate": "DESC"
        }]
    },
    "ddoc": "indexPostingDate",
    "name": "indexPostingDate",
    "type": "json"
}

根据超级账本结构文档,我必须将它放在 META-INF/statedb/couchdb/indexes

使用 Sap hyperledger fabric 平台,我尝试将这个文件夹放在供应商文件夹中以及直接放在 src 文件夹下。但没有一个对我有用。

这是我查询结果的代码

queryString := fmt.Sprintf("{\"selector\":{\"id\":\"%s\"},\"sort\": [{\"PostingDate\": \"desc\"}],\"use_index\": \"indexPostingDate\"}",
        id)
    logger.Infof("Getting data for %s", id)
    oResultsIterator, responseMetaData, oFetchErr := stub.GetQueryResultWithPagination(queryString,
        pageSize, bookmark)

在调用此函数时,我收到以下错误

{
  "error": {
    "message": "GET_QUERY_RESULT failed: transaction ID: 9b7c42c09069855758ccdfbc30f5d75d38b51569a78e101584051e0fa142ebc3: error handling CouchDB request. Error:no_usable_index,  Status Code:400,  Reason:No index exists for this sort, try indexing by the sort fields.",
    "code": "CustomError",
    "status": 500
  }
}

我该如何解决这个问题?

标签: saphyperledger-fabric

解决方案


无法在 Query with Fabric 中指定排序方向是一个已知限制。如果您需要有序搜索,您的索引创建应该指定方向,因此您可以在查询中简单地排除它。

现在我们支持并建议您使用官方的 CouchDB:3.1,这可能会起作用,但我需要对其进行测试。

顺便说一句,您可以通过使用反引号来简化查询,这样您就不必为了便于阅读和写作而转义引号:fmt.Sprintf(`{"selector":{"id":"%s"},"sort": [{"PostingDate": "desc"}],"use_index": "indexPostingDate"}`, id)


推荐阅读