You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cloudant Search Index排序问题:如何按createdAt、user_id实现通配查询排序

Cloudant Search索引排序问题

存储的JSON文档

我在Cloudant中存储了以下JSON文档:

{
 "createdAt": "2022-10-26T09:16:29.472Z",
 "user_id": "4499c1c2-7507-4707-b0e4-ec83e2d2f34d",
 "_id": "606a4d591031c14a8c48fcb4a9541ff0"
}
{
 "createdAt": "2022-10-24T11:15:24.269Z",
 "user_id": "c4bdcb54-3d0a-4b6a-a8a9-aa12e45345f3",
 "_id": "fb24a15d8fb7cdf12feadac08e7c05dc"
}
{
 "createdAt": "2022-10-24T11:08:24.269Z",
 "user_id": "06d67681-e2c4-4ed4-b40a-5a2c5e7e6ed9",
 "_id": "2d277ec3dd8c33da7642b72722aa93ed"
}

已创建的索引

JSON索引

{
 "type": "json",
 "partitioned": false,
 "def": {
  "fields": [
   {
    "createdAt": "asc"
   },
   {
    "user_id": "asc"
   }
  ]
 }
}

Text索引

{
 "type": "text",
 "partitioned": false,
 "def": {
  "default_analyzer": "keyword",
  "default_field": {},
  "selector": {},
  "fields": [
   {
    "_id": "string"
   },
   {
    "createdAt": "string"
   },
   {
    "user_id": "string"
   }
  ],
  "index_array_lengths": true
 }
}

可用的Cloudant Query选择器

我创建了以下Cloudant Query选择器,可正常工作:

{
   "selector": {
      "$and": [
         {
            "createdAt": {
               "$exists": true
            }
         },
         {
            "user_id": {
               "$exists": true
            }
         }
      ]
   },
   "fields": [
      "createdAt",
      "user_id",
      "_id"
   ],
   "sort": [
      {
         "createdAt": "desc"
      }
   ],
   "limit": 10,
   "skip": 0
}

Search索引问题

上述内容在Cloudant环境中均可正常工作,但我遇到了Search Index的问题:

我编写了如下可运行的索引函数:

function (doc) {
  index("specialsearch", doc._id);
  if(doc.createdAt){
    index("createdAt", doc.createdAt, {"store":true})
  }
  if(doc.user_id){
    index("user_id", doc.user_id, {"store":true})
  }
}

通过URL https://[user]-bluemix.cloudant.com/[database]/_design/attributes/_search/by_all?q=*:*&counts=["createdAt"]&limit=2 查询后,返回结果未按createdAt和user_id字段排序。我希望无需指定具体值,通过通配符类型查询获取按这两个字段有序排列的结果,请问我哪里出错了?

内容的提问来源于stack exchange,提问作者Alfred Koen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 13:45:39