"Unknown Error: mango_idx :: {no_usable_index,missing_sort_index}"}

"Unknown Error: mango_idx :: {no_usable_index,missing_sort_index}"}

我有以下查询:

{'type': 'text', 
 'name': 'album-rating-text', 
 'index': {'fields': [
                      {'type': 'string', 'name': 'user_id'}, 
                      {'type': 'string', 'name': 'album_id'}, 
                      {'type': 'number', 'name': 'timestamp'}
]}}

这里是查询:

{'sort': [
           {'user_id': 'desc'}, 
           {'album_id': 'desc'}, 
           {'timestamp': 'desc'}
         ], 
 'limit': 1, 
 'fields': ['user_id', 'album_id', 'timestamp'], 
 'selector': {
              '$and': [
                        {'user_id': {'$eq': 'a@a.com'}},         
                        {'album_id': {'$in': ['bf129f0d', '380e3a05'
                      ]
}}]}}

错误:

{ 
 "error":"unknown_error",
 "reason":"Unknown Error: mango_idx :: {no_usable_index,missing_sort_index}"
}

我已经看到 similar question 但是,我索引的所有字段都在我的排序列表中。


更新:

作为解决方法,我尝试通过删除时间戳字段来简化:

{"type": "text", 
 "name": "album-rating-text", 
 "index": {"fields": [
               {"type": "string", "name": "user_id"}, 
               {"type": "string", "name": "album_id"}
 ]}}

并这样查询...

{"selector": {"$and": [
                   {"user_id": {"$eq": "a@a.com"}}, 
                   {"album_id": {"$in": ["bf129f0d", "380e3a05"]}
              }]}, 
 "fields": ["user_id", "album_id"]}

我收到以下错误:

 {"warning":"no matching index found, create an index to optimize query time",
"docs":[
]}

尝试使用 JSON 索引而不是文本索引:

{
    "type": "json", 
    "name": "album-rating-text", 
    "index": {
        "fields": ["user_id", "album_id", "timestamp"]
    }
}

要对自定义字段使用排序功能,该字段需要手动注册到"Query-index"。

Cloudant 不会这样做,因为它会消耗资源:

"The example in the editor shows how to index the field "foo" using the json type index. You can automatically index all the fields in all of your documents using a text type index with the syntax '{ "index": {}, "type": "text" }', Note that indexing all fields can be resource consuming on large data sets."

您可以使用 Cloudant 仪表板执行此操作。转到您的数据库并查找 "Queryable indexes"。单击“编辑”。

将您的字段添加到默认模板:

{
  "index": {
    "fields": [
      "user_id"
    ]
  },
  "type": "json"
}

按"Create index"

字段 "user_id" 现在可以查询,您现在可以对其使用排序功能。 所有字段都需要手动添加,或者您可以将所有字段注册为查询索引: { "index": {}, "type": "text" }

创建查询索引的视频说明: https://www.youtube.com/watch?v=B3ZkxSFau8U

如果我没记错的话,我的查询要求发生了变化,我选择使用标准的 Cloudant Search index 而不是 Mango 索引。