I am getting a Hyperledger Fabric CouchDB index not found error...
2023-09-17 23:48:58.084 UTC [chaincode] HandleTransaction -> ERRO 051 [ce5a14fc] Failed to handle GET_QUERY_RESULT. error: error handling CouchDB request. Error:no_usable_index, Status Code:400, Reason:No index exists for this sort, try indexing by the sort fields.
I am using Java chaincode:
QueryResultsIterator<KeyValue> results = stub
.getQueryResult("{\"selector\":{\"$not\":{\"createdDate\":null}},\"sort\":[{\"createdDate\":\"desc\"}],\"use_index\":\"createdDateIndexDoc\"}");
The query is to return all entries sorted by createdDate in reverse order. That is why I add the selector with not null. (BTW, the selector is mandatory... can I ignore it some how?)
The Index file I have in META-INF/statedb/couchdb/indexCreatedDateDesc.json is
{
"index": {
"fields": [{"createdDate": "desc"}]
},
"name" : "createdDateIndex",
"ddoc" : "createdDateIndexDoc",
"type" : "json"
}
(Note: I am looking in peer logs, couchdb logs and docker running the chaincode logs but I don't see any confirmation that the index has been installed in the CouchDB database. In which log exactly i should look for that entry to make sure the index is deployed?)
I'm pretty sure it should be in the CouchDB logs.
I typically use the Fauxton UI. (couchdb-url:port/_utils)
The path should be META-INF/statedb/couchdb/indexes
https://hyperledger-fabric.readthedocs.io/en/release-2.2/couchdb_tutorial.html#cdb-add-index
A selector statement is required to query. An index is required for sort in a query.
https://docs.couchdb.org/en/stable/api/database/find.html#sort-syntax