1
votes

I'm trying to update only existing documents in Solr 4.

Suppose I have these documents:

  • {id: 1, name:"product 1", price:10}
  • {id: 2, name:"product 2", price:15}
  • {id: 3, name:"product 3", price:20}

Then I want to add a field, but only for existent documents. So I send these documents to Solr:

  • {id: 2, special:true}
  • {id: 3, special:true}
  • {id: 7, special:true}

The resulting indexed documents I want are:

  • {id: 1, name:"product 1", price:10}
  • {id: 2, name:"product 2", price:15, special:true}
  • {id: 3, name:"product 3", price:20, special:true}

In order to achieve this I'm using the _version_ field (for Atomic Updates, which are already working fine, btw), so my documents for update looks like these:

  • {id: 2, special:true, _version_:1}
  • {id: 3, special:true, _version_:1}
  • {id: 7, special:true, _version_:1}

Using it I expected to get only ids 2 and 3 updated, but I get a 409 Status Code from Solr and none of the docs get updated, because I commit the index after adding all docs.

If I use version=0, then I get something like this:

  • {id: 1, name:"product 1", price:10}
  • {id: 2, name:"product 2", price:15, special:true}
  • {id: 3, name:"product 3", price:20, special:true}
  • {id: 7, special:true, _version_:1}

Any ideas?

1

1 Answers

0
votes

Your solution of using version:1 is correct. But beware that if you send your atomic updates in a batch then any failure will cause the subsequent updates in the batch to be discarded.

In the example that you have given, id:7 will cause a 409 status code (version conflict) but the documents with id:2 and id:3 should be updated.