Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solr conditional adds/updates?

Tags:

I have a fairly simple need to do a conditional update in Solr, which is easily accomplished in MySQL.

For example,

  • I have 100 documents with a unique field called <id>
  • I am POSTing 10 documents, some of which may be duplicate <id>s, in which case Solr would update the existing records with the same <id>s
  • I have a field called <dateCreated> and I would like to only update a <doc> if the new <dateCreated> is greated than the old <dateCreated> (this applies to duplicate <id>s only, of course)

How would I be able to accomplish such a thing?

The context is trying to combat race conditions resulting in multiple adds for the same ID but executing in the wrong order.

Thanks.

like image 265
Artem Russakovskii Avatar asked Aug 14 '09 22:08

Artem Russakovskii


2 Answers

As of solr 4.0, optimistic concurrency is enabled via the _version_ field.

http://yonik.com/solr/optimistic-concurrency/

To enable, you need to make sure your schema.xml contains

<field name="_version_" type="long" indexed="true" stored="true"/>

and in solrconfig.xml

<updateHandler class="solr.DirectUpdateHandler2">
    <updateLog>
      <str name="dir">${solr.data.dir:}</str>
    </updateLog>
</updateHandler>
like image 23
Bob B Avatar answered Sep 20 '22 02:09

Bob B


I can think of two ways:

  1. Write your own UpdateHandler and override addDoc to implement that checking.
  2. Put the appropriate locks (critical sections) in your client code in order to fetch the stored document, compare the dates, and conditionally add the new document in a thread-safe manner.

Remember that Solr is not a database, comparing it to MySQL is comparing apples and oranges.

like image 78
Mauricio Scheffer Avatar answered Sep 24 '22 02:09

Mauricio Scheffer