对不是主索引的字段执行排序

时间:2016-04-26 14:41:33

标签: indexing couchdb cloudant

错误:

No index exists for this sort, try indexing by the sort fields.

我尝试在anotherValue_id+anotherValue上创建索引,但没有区别。

这是我的疑问:

{
  "selector": {
    "_id": { "$gt": null },
    "$or": [
          { "_id": "10" },
          { "value": "10", "anotherValue": "1234" }]
  },
  "sort": [{"anotherValue": "desc"}]
}

索引设置:

Your available Indexes:
special: _id

1 个答案:

答案 0 :(得分:1)

尝试在anotherValue上添加desc索引:

{
  "index": {
    "fields": [
      {"anotherValue":"desc"}
    ]
  },
  "type": "json"
}

并将您的查询更改为:

{
  "selector": {
    "anotherValue": { "$gt": null },
    "$or": [
      { "_id": "10" },
      { "value": "10", "anotherValue": "1234" }
    ]
  },
  "sort": [{"anotherValue": "desc"}]
}

注意:如果您在所有字段上添加了文本索引,原始查询也会起作用:

{
  "index": {},
  "type": "text"
}