0

我正在使用带有 CouchDB 的 Hyperledger Fabric(版本 2.2.0,Hyperledger/fabric-couchdb docker 映像附带的版本)。Fabric 有一个限制,它不允许为 mango 查询指定排序数组,因此必须使用索引来完成。

我面临的问题是,无论我为索引字段指定排序“asc”还是“desc”,查询结果总是以相同的顺序出现。

我试图创建的索引(我也宁愿使用assetType作为部分索引选择器,但也没有成功):

{
    "index": {
      "fields": [
        {"assetType": "desc"},
        {"originalCustomer.document":"desc"}, 
        {"transactionDate":"desc"}
      ]
    },
    "ddoc": "date-index",
    "name": "date-index",
    "type": "json"
 }

查询我正在运行

{
    "selector": {
        "assetType": "receivable",
        "originalCustomer.document": "1",
        "transactionDate": {
            "$gt":"1900-01-01"
        }
    },
    "use_index": ["date-index"]
 }

_解释结果

{
    "dbname": "testdb",
    "index": {
        "ddoc": "_design/date-index",
        "name": "date-index",
        "type": "json",
        "def": {
            "fields": [{"assetType": "asc"},{"originalCustomer.document":"asc"},{"transactionDate": "asc"}]
        }
    },
    "selector": {
        "$and": [
            {"assetType": {"$eq": "receivable"}},
            {"originalCustomer.document": {"$eq": "1"}},
            {"transactionDate": {"$gt": "1900-01-01"}}
        ]
    },
    "opts": {
        ...
    },
    "limit": 25,
    "skip": 0,
    "fields": "all_fields",
    "mrargs": {
        "include_docs": true,
        "view_type": "map",
        "reduce": false,
        "start_key": [
            "receivable",
            "1",
            "1900-01-01"
        ],
        "end_key": [
            "receivable",
            "1",
            "<MAX>"
        ],
        "direction": "fwd",
        "stable": false,
        "update": true,
        "conflicts": "undefined"
    }
}

无论我在索引上使用“asc”还是“desc”,都会产生相同的 _find 结果。鉴于降序排列,我希望 transactionDate“2019-01-02”成为列表中的第一个(为简洁起见,我删除了不相关的字段)

{
    "docs": [
        {
            "assetType": "receivable",
            "originalCustomer": {"document": "1"},
            "transactionDate": "2019-01-01"
        },
        {
            "assetType": "receivable",
            "originalCustomer": {"document": "1"},
            "transactionDate": "2019-01-01"
        },
        {
            "assetType": "receivable",
            "originalCustomer": {"document": "1"},
            "transactionDate": "2019-01-01"
        },
        {
            "assetType": "receivable",
            "originalCustomer": {"document": "1"},
            "transactionDate": "2019-01-02"
        },
        {
            "assetType": "receivable",
            "originalCustomer": {"document": "1"},
            "transactionDate": "2019-01-02"
        }
    ],
    "bookmark": "..."
}
4

1 回答 1

0

仔细查看/db/_index 的 CouchDB API 参考,我发现它index object应该是遵循排序语法的字段名称的 JSON 数组。但是index object,在同一参考页面上的示例请求中不包括排序方向。在这种情况下,将应用默认的“asc”。

由于您使用索引进行双向排序,因此无需指定方向。它看起来如下。

{
    "index": {
      "fields": ["assetType", "originalCustomer.document", "transactionDate" ]
    },
    ...
}

通过/db/_find请求数据时,需要指定单个结果的所需排序。您的查询必须包含一个遵循排序语法的排序对象。例如,为了获得降序排序的结果,查询可能如下所示。

{
    "selector": {
        "assetType": "receivable",
        "originalCustomer.document": "1",
        "transactionDate": {
            "$gt":"1900-01-01"
        }
    },
    "fields": ["assetType", "originalCustomer.document", "transactionDate" ],
    "sort": [
        {"assetType": "desc"},
        {"originalCustomer.document":"desc"}, 
        {"transactionDate":"desc"}
    ]
 }
于 2019-06-13T08:52:25.430 回答