【问题标题】:CouchDB mango ignoring index sort orderCouchDB芒果忽略索引排序顺序
【发布时间】:2019-04-24 21:48:39
【问题描述】:

我正在使用带有 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": "..."
}

【问题讨论】:

    标签: couchdb hyperledger-fabric couchdb-mango


    【解决方案1】:

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

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

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

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

    {
        "selector": {
            "assetType": "receivable",
            "originalCustomer.document": "1",
            "transactionDate": {
                "$gt":"1900-01-01"
            }
        },
        "fields": ["assetType", "originalCustomer.document", "transactionDate" ],
        "sort": [
            {"assetType": "desc"},
            {"originalCustomer.document":"desc"}, 
            {"transactionDate":"desc"}
        ]
     }
    

    【讨论】:

    • 感谢您的回答。但正如我所说,我无法在 _find 命令中指定排序顺序。这是 Hyperledger Fabric 的限制。根据CouchDB docs,我可以在索引创建中做到这一点:“索引对象是sort syntax 之后的字段名称的JSON数组......”。在排序语法链接中说我可以使用 {"fieldName1": "desc"}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多