Skip to content

Toro Cloud Dev Center


Deleting a document from a custom search index

There are two ways you can delete a Solr document in Martini: using functions and using a SolrClient object.

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

True to its purpose, using a function is the easiest way to delete an existing document. There are three function methods you can use to delete a Solr document, all from SolrMethods:

The example below shows how to delete a document with the ID 4fc1a64f-7226-4aad-bf27-5c399e2c453c:

1
2
'movie-core'.deleteById(null, ['4fc1a64f-7226-4aad-bf27-5c399e2c453c'])
'movie-core'.deleteByQueryString(null, 'id:4fc1a64f-7226-4aad-bf27-5c399e2c453c')

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.

The example below shows how to delete a document with the ID 4fc1a64f-7226-4aad-bf27-5c399e2c453c:

1
2
3
SolrClient solrClient = 'movie-core'.solr()
solrClient.deleteById(['4fc1a64f-7226-4aad-bf27-5c399e2c453c'])
solrClient.deleteByQuery('id:4fc1a64f-7226-4aad-bf27-5c399e2c453c')