锁定特定文档以在 Solr 中进行编辑

Lock a specific document for edits in Solr

有没有办法在 Solr6 或 Solr7 中为 edits/removals 锁定特定文档? 例如,我阅读了一份文件,根据其内容我认为应该更新或删除它。然后我发送 update/remove 查询。但是在我阅读文档和发送该查询的那一刻之间,我想确保该文档没有被其他进程更新或删除 - "lock" 它用于任何编辑。有办法吗?

您可以在 Solr 中使用 the optimistic concurrency feature

实际上,您在请求中包含了文档 _version_ 字段中的值。如果您包含的 _version_ 值与文档中的值不匹配(即文档在您检索后已更新),则更新失败并返回 409 HTTP 错误代码。

  • 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.

_version_ 字段中给出的值可以决定您想要的行为:

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.