nicolapaoli
nicolapaoli

Reputation: 55

CouchDB Index Error using ORDER BY in queries from Hyperledger Composer v0.18.0 to v0.19.0

I have an issue with CouchDB Index on a query with the ORDER BY statement while moving from Hyperledger Composer v0.18.0 to v0.19.0.

Context:

A Simple Asset called Event:

asset Event identified by id {
    o String id
    o String name
    o EventType eventType
    o DateTime scheduledTime
    o EventStatus status
}

A Query with an ORDER BY statement:

query getScheduledEvents {
    description: "Select all the SCHEDULED Events"
    statement:
        SELECT event.network.assets.Event
            WHERE (status == 'SCHEDULED')
                ORDER BY [scheduledTime ASC]
}

HL Composer v0.18.0

Using Composer v0.18.0 everything works fine. I can query the Events from the REST API simply creating this Index on CouchDB:

{
    "type": "json",
    "def": {
    "fields": [
            {
                "scheduledTime": "asc"
            }
        ],
        "partial_filter_selector": {}
     }
}

HL Composer v0.19.0

From Composer v0.19.0 (And Fabric 1.1.0) the Index will be automatically generated on the deployment, and the new index looks like this:

{
    "type": "json",
    "def": {
        "fields": [
            {
                "\\$class": "asc"
            },
            {
                "\\$registryType": "asc"
            },
            {
                "\\$registryId": "asc"
            },
            {
                "status": "asc"
            },
            {
                "scheduledTime": "asc"
            }
        ],
        "partial_filter_selector": {}
    }
}

And now, using Composer v0.19.0, querying the same endpoint I get the following error :

{
"error": {
    "statusCode": 500,
    "name": "Error",
    "message": "2 UNKNOWN: error executing chaincode: transaction returned with failure: Error: Couch DB Error:no_usable_index,  Status Code:400,  Reason:No index exists for this sort, try indexing by the sort fields.",
    "code": 2,
    "metadata": {
        "_internal_repr": {}
    },
    "details": "error executing chaincode: transaction returned with failure: Error: Couch DB Error:no_usable_index,  Status Code:400,  Reason:No index exists for this sort, try indexing by the sort fields.",
"stack": "Error: 2 UNKNOWN: error executing chaincode: transaction returned with failure: Error: Couch DB Error:no_usable_index,  Status Code:400,  Reason:No index exists for this sort, try indexing by the sort fields.\n    at new createStatusError (/home/user/.nvm/versions/node/v8.11.1/lib/node_modules/composer-rest-server/node_modules/grpc/src/client.js:64:15)\n    at /home/user/.nvm/versions/node/v8.11.1/lib/node_modules/composer-rest-server/node_modules/grpc/src/client.js:583:15"
    }
}

Removing the generated index and creating the old one won't solve the issue, as I still receive the same error.

Any thoughts? Thank you!

Upvotes: 3

Views: 893

Answers (1)

Andrew Coleman
Andrew Coleman

Reputation: 1559

I was able to recreate this with a similar query, and found that I could get it working by rearranging the fields in the index document. In your case it would be arranged as follows:

{
    "type": "json",
    "def": {
        "fields": [
            {
                "\\$class": "asc"
            },
            {
                "\\$registryType": "asc"
            },
            {
                "\\$registryId": "asc"
            },
            {
                "scheduledTime": "asc"
            },
            {
                "status": "asc"
            }
        ],
        "partial_filter_selector": {}
    }
}

I think this is related to the explanation in bullet 3 of this post

Upvotes: 2

Related Questions