databasemongodbnosqlibm-cloudcloudant

Cloudant query error: missing search index when trying to sort


I've followed the Cloudant tutorial from IBM on how to run a query to retrieve a selection of documents, and sort these based on a field's value. The instructions start about 2/3rd way down the page at "Running a query with two fields".

I'm getting an error which I can't find a solution for.

Unknown Error: mango_idx :: {no_usable_index,missing_sort_index}

The query is:

{
"selector": {
  "lastname": "Brown",
  "location": "New York City, NY"
},
"fields": [
  "firstname",
  "lastname",
  "location"
],
"sort": [
  {
    "lastname": "asc"
  },
  {
    "firstname": "asc"
  }
]
}

I have a query index added to the design documents, as given:

{
"index": {
  "fields": [
    "lastname",  
    "location",
    "age"
  ]
},
"name": "query-index",
"type": "json"
}

The documents themselves are variants of:

{
"firstname": "John",
"lastname": "Brown",
"age": 21,
"location": "New York City, NY",
"_id": "doc2"
}

Am I doing something wrong? Did something change in the Cloudant syntax that the tutorial wasn't updated on?

I'm aware of "Unknown Error: mango_idx :: {no_usable_index,missing_sort_index}"} of which the suggested answers don't help me.


Solution

  • It does look like this is an issue with that tutorial. There is no index to support sorting by lastname and firstname. You can create another index like this:

    {
      "index": {
        "fields": [
          "lastname",
          "firstname"
        ]
      },
      "type": "json"
    }
    

    Alternatively you could sort only by lastname (take out firstname). This will be supported by the original index. Query would look like this:

    {
      "selector": {
        "lastname": "Brown",
        "location": "New York City, NY"
      },
      "fields": [
        "firstname",
        "lastname",
        "location"
      ],
      "sort": [
        {
          "lastname": "asc"
        }
      ]
    }