Updating a document from a custom search index
There are two ways to update a Solr document in Martini. For your benefit, an example is written for each given method. And for each of these examples, we will assume that we need to update this particular entry in the index1:
1 2 3 4 5 6 7 8 9 10 11 12 13
Get the code!
The scripts mentioned in this guide are available in the
As bonus, you can find other services in the
examples package that demonstrate
the use of functions from the
as well as other Solr-related functionality.
If the update is only partial, one must:
- Create a
- Set the
idfield of the
- Populate the
SolrInputDocumentobject with fields and values that must be modified.
- Call the function method to index the
For example, to update the
director property of the original document, we'll do something like:
1 2 3 4 5
In the example snippet above, we are updating the field
director to have the value of
"Robert Lee Zemeckis".
You might notice that we passed a
Map as the second argument of the call to
SolrInputDocument#setField(String, Object), unlike what we did when setting the ID wherein we passed a
Map argument lets us define the modifier for the field that needs to be updated. In this case, our
set which allows us to "set or replace the field value with the specified value". Solr provides
other modifiers which you can use instead.
If however, you need to update all fields of the document:
- Create your bean object as usual.
- Set the bean object's
idproperty (unique key property) so we know which document to update.
- Populate all fields of the object.
- Call the function method to re-index the bean object.
1 2 3 4 5
After the changes have been committed, you will notice that the
director field has been updated, but the rest of
the fields are left blank. This is because with the snippet above, we have only specified the value of the
A call to the function method
SolrMethods.solr(String) returns a
SolrClient object which you can use to directly interact with
the Solr core tied to it (specified by passing the name of the core as the argument). However to use
one must be familiar with SolrJ and Groovy.
1 2 3 4 5 6 7 8 9 10 11
SolrInputDocument object is populated
with fields that need updating.
Document is represented in JSON for convenience. ↩