11.05.2016 Views

Apache Solr Reference Guide Covering Apache Solr 6.0

21SiXmO

21SiXmO

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

{"id":"mydoc",<br />

"price":99,<br />

"popularity":62,<br />

"categories":["kids","toys","games"],<br />

"tags":["buy_now","clearance"]<br />

}<br />

Optimistic Concurrency<br />

Optimistic Concurrency is a feature of <strong>Solr</strong> that can be used by client applications which update/replace<br />

documents to ensure that the document they are replacing/updating has not been concurrently modified by<br />

another client application. This feature works by requiring a _version_ field on all documents in the index, and<br />

comparing that to a _version_ specified as part of the update command. By default, <strong>Solr</strong>'s Schema includes a<br />

_version_ field, and this field is automatically added to each new document.<br />

In general, using optimistic concurrency involves the following work flow:<br />

1.<br />

2.<br />

3.<br />

4.<br />

A client reads a document. In <strong>Solr</strong>, one might retrieve the document with the /get handler to be sure to<br />

have the latest version.<br />

A client changes the document locally.<br />

The client resubmits the changed document to <strong>Solr</strong>, for example, perhaps with the /update handler.<br />

If there is a version conflict (HTTP error code 409), the client starts the process over.<br />

When the client resubmits a changed document to <strong>Solr</strong>, the _version_ can be included with the update to<br />

invoke optimistic concurrency control. Specific semantics are used to define when the document should be<br />

updated or when to report a conflict.<br />

If the content in the _version_ field is greater than '1' (i.e., '12345'), then the _version_ in the<br />

document must match the _version_ in the index.<br />

If the content in the _version_ field is equal to '1', then the document must simply exist. In this case, no<br />

version matching occurs, but if the document does not exist, the updates will be rejected.<br />

If the content in the _version_ field is less than '0' (i.e., '-1'), then the document must not exist. In this<br />

case, no version matching occurs, but if the document exists, the updates will be rejected.<br />

If the content in the _version_ field is equal to '0', then it doesn't matter if the versions match or if the<br />

document exists or not. If it exists, it will be overwritten; if it does not exist, it will be added.<br />

If the document being updated does not include the _version_ field, and atomic updates are not being used,<br />

the document will be treated by normal <strong>Solr</strong> rules, which is usually to discard the previous version.<br />

When using Optimistic Concurrency, clients can include an optional versions=true request parameter to<br />

indicate that the new versions of the documents being added should be included in the response. This allows<br />

clients to immediately know what the _version_ is of every documented added with out needing to make a<br />

redundant /get request.<br />

For example...<br />

<strong>Apache</strong> <strong>Solr</strong> <strong>Reference</strong> <strong>Guide</strong> <strong>6.0</strong><br />

228

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!