I have been working on a project where we need to update a field within a document should that piece of information be added or updated. Until now I thought the only way to do it would be to re-build the document and commit it back to solr. This is not the case if you’re using Solr 4.0. Off I went and fired up a default Solr Server and started to play about to see if I could get this working and it’s very straight forward.
Index Contents
This is the document I am interested in updating:
I need to use the document unique identifier in order to find the document I want to update and in this case the field is called “id”.
The document field update
Please note I am using a Windows curl client so the format of the following line will be different if running in Unix/Linux. What I am doing is changing the genre from “fantasy” to “sci-fi” and saying that it’s no longer in stock on the document with a unique identifier of “978-0641723445”
curl "http://localhost:8983/solr/entity/update?commit=true" -H "Content-type:application/json" -d "[{\"id\":\"978-0641723445\",\"genre_s\":{\"set\":\"sci-fi\"},\"inStock\":{\"set\":false}}]"
Running the above command gives me an error free response and if I check the document in the index:
Everything updated as expected. More on the set field attribute can be found : http://wiki.apache.org/solr/UpdateXmlMessages#Optional_attributes_for_.22field.22