2017-10-11 68 views
1

我有一個問題持續了幾天,我無法解決。Cloudant:此類索引不存在,請嘗試按排序字段索引

我有我的數據庫一個簡單的文件,它看起來像這樣:

{ 
    "_id": "asd123", 
    "_rev": "revidasd123", 
    "type": "CUSTOM_TYPE", 
    "position": 8, 
    "title": "Short custom title" 
} 

當我試圖通過位置做一個排序,即使我已經創造了這方面的指標,我總是得到同樣的錯誤:

Error: no_usable_index. Reason: No index exists for this sort, try indexing by the sort fields.

這裏是指數:

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

這裏是我的查詢造成此錯誤:

{ 
    "selector": { 
    "type": "CUSTOM_TYPE", 
    "_id":{ 
     "$gt": null 
    } 
    }, 
    "fields": [ 
    "_id", 
    "type", 
    "position" 
    ], 
    "sort": [ 
    { 
     "_id": "asc" 
    }, 
    { 
     "position": "asc" 
    } 
    ] 
} 

結果:沒有索引存在這種排序,按排序領域嘗試建立索引。

幫助!在此先感謝

回答

2

如果您想通過位置進行排序,然後嘗試此查詢:

{ 
    "selector": { 
    "type": "CUSTOM_TYPE", 
    "position": {"$gte": 0} 
    }, 
    "fields": [ 
    "_id", 
    "type", 
    "position" 
    ], 
    "sort": [ 
    { 
     "position": "asc" 
    } 
    ] 
} 

position字段需要選擇的一部分。我將它設置爲匹配任何> = 0的位置,所以它應該匹配所有位置,除非它爲空。

此外,你排序_id,然後position。這會使position排序失去意義。我從排序中刪除了_id字段。

+0

非常感謝@markwatsonatx。 這解決了我的問題。 我也包括在選擇器中的位置,但我沒有刪除_id,所以可能也會給我一個錯誤。 – Alin