我正在将Hyperledger Fabric与CouchDB(版本2.2.0,hyperledger / fabric-couchdb docker映像随附的版本)一起使用。 Fabric上有一个限制,不允许为芒果查询指定排序数组,因此必须使用索引来完成。
我面临的问题是,无论我为索引字段指定排序“ 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": "..."
}
答案 0 :(得分:0)
仔细观察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"}
]
}