Skip to content

Martini Searching Solr Documents

Searching for documents in a Solr index within Martini can be accomplished in three ways: using functions, utilizing a SolrClient object, or leveraging the Solr Search API. Each method caters to different use cases and levels of complexity.

Get the Code!

Scripts and examples related to this guide are available in the examples package available in the Lonti Marketplace. This includes demonstrations using functions from the SolrMethods class and other Solr-related functionality.

Using Functions

Functions provide a straightforward approach to search queries in Solr. The SolrMethods class offers convenient functions for this purpose. For instance:

1
2
3
4
5
6
7
8
// Query to fetch all documents
SolrMethods.query(coreName, packageName, [q:"*:*"]).getResults()

// Query to search documents by a title substring
SolrMethods.query(coreName, packageName, [q:"movieTitle:*${titleSubstring}*"]).getResults()

// Query to find a document by its ID
SolrMethods.query(coreName, packageName, [q:"id:${id}"]).getResults()

Using SolrClient

For a more direct interaction with the Solr core, the SolrMethods.solr(String) function returns a SolrClient object. This method is ideal for those familiar with SolrJ and Groovy.

1
2
3
4
5
// Creating a SolrClient instance
SolrClient solrClient = SolrMethods.solr(coreName)

// Retrieving documents by IDs
SolrDocumentList documents = solrClient.getById(ids)

Using the Solr Search API

The Solr Search API provides an extended interface for querying documents. This method involves using Solr's SearchHandlers through a Solr-derived endpoint.

1
2
3
4
// Example of using the Solr Search API
curl -X GET \
'http://<host>:<port>/esbapi/v1/solr/<package name>/<core name>/select?q=id:<id>' \
-H 'Authorization: Bearer <access token>'