Martini Updating Solr Documents
This section explains how to update a document in a Solr index within Martini. We provide two methods for updating documents, each illustrated with an example. These examples demonstrate updating the following document:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Access the Code
The scripts in this guide are available in the examples package available in the Lonti Marketplace.
Method 1: Using Functions
We recommend using the SolrMethods.writeToIndex(...) for ease of updating documents. For partial updates:
- Create a
SolrInputDocumentobject. - Set its
idfield. - Add fields and values that need modification.
- Use
writeToIndexto index the document.
For example, to update the director field:
1 2 3 4 5 | |
Why use [set:"$newValue"]?
This approach specifies a Map as the second argument in SolrInputDocument#setField, allowing us to define field modifiers. Here, set replaces the field value. Solr supports other modifiers for different update operations.
For complete document updates:
- Create and populate your bean object.
- Set its unique key property (e.g.,
id) for identifying the document. - Populate all relevant fields.
- Re-index the object using
writeToIndex.
Example for complete update:
1 2 3 4 5 | |
After committing, only the director field updates, leaving others unchanged.
Method 2: Using SolrClient
For direct interaction with Solr, use SolrMethods.solr(String) to obtain a SolrClient instance. This method requires familiarity with SolrJ and Groovy.
1 2 3 4 5 6 7 8 9 10 11 | |
In this method, a SolrInputDocument is populated with the necessary fields and then added to the Solr index.