Cloudant: How to create an index for “Sort” function?

浪子不回头ぞ 提交于 2019-12-02 02:01:05

问题


The problem I am facing is on creating the correct index to query through my Cloudant database. The JSON data structure I am using looks similar to below.

{
  "customer" : "123",
  "time" : "2014-11-20"
}

I want to sort the documents based on the time. The index query that I used is:

curl -X POST 'https://<user>:<pass>@<user>.cloudant.com/<DB-name>/_index' -d '
{
  "index": { 
    "fields": [
      "customer", 
      "time"
     ] 
  } 
}'

And the Query that I am using is:

curl -X POST 'https://<user>:<pass>@<user>.cloudant.com/<DB-name>/_find' -d '
{
  "selector": { 
    "customer" : "123"
  }, 
  "sort": [
    "time"
  ]
}'

The error code I am getting is "no_usable_index". Can anyone provide some insight into this problem?

Also, what would be different if the time were in the format: "2014-11-20 11:50:00"? Essentially, I am trying to sort based on date and time. Is this possible?


回答1:


The error message is telling you that there is no index to perform the sorting, or at least it can't find one. To help it find one, sort on customer and then on time, like so:

curl -X POST 'https://<user>:<pass>@<user>.cloudant.com/<DB-name>/_find' -d '
{
  "selector": { 
    "customer" : "123"
  }, 
  "sort": [
    "customer",
    "time"
  ]
}'

This query is functionally identical, but now Cloudant Query will find the index.

Regarding your question about the other time format, the time field would still be treated as a string field for the purposes of sorting. In your case, that means you'll get the expected result.



来源:https://stackoverflow.com/questions/27300741/cloudant-how-to-create-an-index-for-sort-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!