solrlucenesolrjsolrcloudsolr6

Lock a specific document for edits in Solr


Is there a way to lock a specific document for edits/removals in Solr6 or Solr7? For example, I read a document, based on its contents I see that it should be updated or removed. Then I send update/remove query. But between the moment I read the doc and the moment I send that query, I wanna make sure that document is not updated or removed by some other process - "lock" it for any edits. Is there a way to do that?


Solution

  • You can use the optimistic concurrency feature in Solr.

    In effect, you include the value from the _version_ field of your document in your request. If the _version_ value you included doesn't match the one in the document (i.e. the document has been updated since you retrieved it), the update fails and an 409 HTTP error code is returned instead.

    • A client reads a document. In Solr, one might retrieve the document with the /get handler to be sure to have the latest version.

    • A client changes the document locally.

    • The client resubmits the changed document to Solr, for example, perhaps with the /update handler.

    • If there is a version conflict (HTTP error code 409), the client starts the process over.

    The value given in the _version_ field can dictate which behavior you want:

    If the content in the _version_ field is greater than '1' (i.e., '12345'), then the _version_ in the document must match the _version_ in the index.

    If the content in the _version_ field is equal to '1', then the document must simply exist. In this case, no version matching occurs, but if the document does not exist, the updates will be rejected.

    If the content in the _version_ field is less than '0' (i.e., '-1'), then the document must not exist. In this case, no version matching occurs, but if the document exists, the updates will be rejected.

    If the content in the _version_ field is equal to '0', then it doesn’t matter if the versions match or if the document exists or not. If it exists, it will be overwritten; if it does not exist, it will be added.