Skip to content

Toro Cloud Dev Center


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
{
    "movieTitle": "Forrest Gump",
    "director": "Robert Zemeckis",
    "cast": [
        "Tom Hanks",
        "Robin Wright",
        "Gary Sinise",
        "Mykelti Williamson",
        "Sally Field"
    ],
    "id": "4fc1a64f-7226-4aad-bf27-5c399e2c453c",
    "_version_": 1617640267263770624
}

Get the code!

The scripts mentioned in this guide are available in the examples package. As bonus, you can find other services in the examples package that demonstrate the use of functions from the SolrMethods class, as well as other Solr-related functionality.

Using functions

TORO recommends using SolrMethods.writeToIndex(...) function methods for your indexing needs. True to its purpose, using a function is the easiest way to update an existing document.

If the update is only partial, one must:

  1. Create a SolrInputDocument object.
  2. Set the id field of the SolrInputDocument object.
  3. Populate the SolrInputDocument object with fields and values that must be modified.
  4. Call the function method to index the SolrInputDocument object.

For example, to update the director property of the original document, we'll do something like:

1
2
3
4
5
def document = new SolrInputDocument()
document.setField('id', '4fc1a64f-7226-4aad-bf27-5c399e2c453c')
document.setField('director', [set:'Robert Lee Zemeckis'])

'movie-core'.writeToIndex(null, document)
Why use [set:"$newValue"]?

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 String.

The Map argument lets us define the modifier for the field that needs to be updated. In this case, our modifier is 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:

  1. Create your bean object as usual.
  2. Set the bean object's id property (unique key property) so we know which document to update.
  3. Populate all fields of the object.
  4. Call the function method to re-index the bean object.

For example:

1
2
3
4
5
MovieDocument movie = new MovieDocument()
movie.setId('4fc1a64f-7226-4aad-bf27-5c399e2c453c')
movie.setDirector('Robert Lee Zemeckis')

'movie-core'.writeToIndex(null, movie)

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 director property.

Using SolrClient

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 SolrClient, one must be familiar with SolrJ and Groovy.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import org.apache.solr.common.SolrInputDocument

// ...

SolrClient solrClient = SolrMethods.solr( coreName )
SolrInputDocument document = new SolrInputDocument()

document.setField('id', '4fc1a64f-7226-4aad-bf27-5c399e2c453c')
document.setField('director', [set:'Robert Lee Zemeckis'])

solrClient.add( solrInputDocument )

Similarly, a SolrInputDocument object is populated with fields that need updating.


  1. Document is represented in JSON for convenience.