Tamwyn
Tamwyn

Reputation: 320

Cloudant add lucene index via curl

So having created an index function and tested it, it should be integrated to the database initialisation steps. I currently have the following:

indexRequest.json

{
"indexes": {
    "mySearch": {
      "index": "function (doc) {
        index('default', doc.name);
        if (doc.description) {
          index('default', doc.description);
        }
      }"
    }
  }
}

Now I'm trying to send this file to cloudant via:

curl -X PUT https://$username:$password@$myurl.cloudant.com/myDatabase/_design/myTest  -H 'Content-Type: application/json' -d @indexRequest.json

Which fails with

{"error":"conflict","reason":"Document update conflict."}

myTest already contains an index function, with a different name. What am I doing wrong here?

Upvotes: 0

Views: 49

Answers (1)

Glynn Bird
Glynn Bird

Reputation: 5637

When updating or deleting document in Cloudant, you must supply the new document body with the previous version's _rev token. The procedure is

1) read the existing document

curl -X PUT https://$username:$password@$myurl.cloudant.com/myDatabase/_design/myTest

This will give you the existing body. Modify the body to your specifications. Then

2) write the document back

curl -X PUT https://$username:$password@$myurl.cloudant.com/myDatabase/_design/myTest  -H 'Content-Type: application/json' -d @indexRequest.json

where the original _id and _rev are in the json file.

You should then get back a confirmation:

{
    "ok":true,
    "id":"_design/myTest",
    "rev":"2-9176459034"
}

Here's the Cloudant Docs on updating documents and a more detailed write-up on Design Document management

Upvotes: 1

Related Questions