Imagine that you have a system where you store documents your users upload. In addition to this, your users can add other users to have access to the files they uploaded. Before Solr 4, you had to reindex the whole document to update it. With the release of Solr 4 and later versions, we are allowed to update a single field if we fulfill some basic requirements. This recipe will show you how to do this.
How to do it...
Let's look at the steps we need to take to update the document field:
For the purpose of the recipe, let's assume we have the following index structure (put the following entries into your schema.xml file):
So, we have a sample file and two usernames specifying which users in our system can access the file. However, what if we want to add another user called jack? Is it possible? To add the value to a field that has multiple values, we should send the following command:
Now, imagine that one of the users changed the name of the document, and we want to update the file field of this document to match the change. In order to do so, we should send the following command:
Again, the command works well. So, let's see how Solr does this.
How it works...
As you can see, the index structure is pretty simple; we have the document identifier and its name and users that can access the file. As you can see, all the fields in the index are marked as stored (stored="true"). This is required for the partial update functionality to work. This is because, under the hood, Solr takes all the values from the fields and updates the one we tell it to update. So, it is just a typical document indexing, but instead of you having to provide all the information, it's Solr's responsibility to get it from the index.
Another thing that is required for the atomic update functionality to work is the _version_ field. You don't have to set it during indexation; it is used internally by Solr. The example data we index is also very simple. It is a single document with two users defined.
The interesting stuff comes with the update command. As you can see, this command is run against a standard update handler you run indexation against. The commit=true parameter tells Solr to perform the commit operation right after update. The -H 'Content-type:application/json' part is responsible for setting the correct HTTP headers for the update request.
Next, we have the request contents. It is sent as a JSON object. We specify that we are interested in the document with identifier 1 ("id":"1"). We want to change the user field and add the jack value to this field (the add command). So, as you can see, the add command is used when we want to add a new value to a field that can hold multiple values.
The second command shows how to change the value of a single-valued field. It is very similar to what we had before, but instead of using the add command, we use the set command. Again, as you can see, it works perfectly.
The third command shown in the recipe illustrates how to increment a field. We can run this command against any numeric field. We need to use the inc command and specify a number that will be added to the value of the field in the index. In our case, we add 1.