Cloudant: No index exists for this sort, try index

2019-04-15 16:18发布

I have a problem that persists for a few days and I cannot solve it.

I have a simple document in my database which looks like this:

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

When I try to make a sort by position, even if I have created an index for this field, I always get the same error:

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

Here is the index:

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

And here is my query which cause this error:

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

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

Please HELP! Thanks in advance

1条回答
何必那么认真
2楼-- · 2019-04-15 16:47

If you want to sort by position then try this query:

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

The position field needs to be part of the selector. I set it to match any position that is >= 0, so it should match all unless it's null.

Also, you were sorting by _id and then position. This would render the position sort meaningless. I removed the _id field from the sort.

查看更多
登录 后发表回答