SolrTemplate was the central class of Spring Data Solr: a thin template over a SolrClient that let you index beans, run queries, commit and delete documents without writing SolrJ plumbing yourself. It played the same role for Solr that JdbcTemplate plays for SQL. Spring Data Solr was discontinued by the Spring team in 2020 and no longer ships in current Spring Data release trains, so on a modern Spring Boot 3 project you use SolrJ's SolrClient directly instead.
This article explains what SolrTemplate did, shows how it was used in older Spring Boot applications, and gives the SolrJ replacement you should write today.
SolrTemplate implemented the SolrOperations interface and wrapped a SolrClient (for example HttpSolrClient or CloudSolrClient). Its responsibilities were:
@SolrDocument, @Indexed, @Id) to SolrInputDocument objects and back.Query abstractions (SimpleQuery, FacetQuery, HighlightQuery) and mapping results to Page objects.DataAccessException hierarchy.SolrCrudRepository was built on top of SolrTemplate. Use of the template directly was the choice when a repository method name could not express the query, for example faceting, highlighting or custom filter queries.
| Aspect | SolrCrudRepository | SolrTemplate |
|---|---|---|
| Style | Declarative, derived query methods | Imperative, explicit calls |
| Typical use | Simple CRUD and finders | Facets, highlighting, bulk operations |
| Collection | Taken from the entity annotation | Passed per call |
| Control | Limited | Full access to query options |
In an old Spring Boot 2.x project with Spring Data Solr on the classpath, the template was created from a SolrClient bean:
Indexing and querying then looked like this:
Building the query with Criteria instead of concatenating "name:" + term matters: it escapes special Solr characters, whereas string concatenation allows query injection from user input.
For new code, add SolrJ and inject a SolrClient. The client talks to Solr over HTTP and has no dependency on Spring Data.
Pick a SolrJ version that matches your Solr server's major version. A minimal replacement for the service above:
ClientUtils.escapeQueryChars escapes Solr's reserved characters so user input cannot change the query structure. SolrJ also offers addBean and getBeans with @Field annotations if you want object mapping without a separate document class.
Avoid calling commit after every document in production. Configure autoCommit and autoSoftCommit in solrconfig.xml, or pass commitWithin milliseconds when adding documents, so Solr batches commits.
No. The Spring team discontinued the project in 2020 and it was removed from the Spring Data release train, so it is not available for current Spring Boot versions.
Use the SolrJ SolrClient directly, wrapped in your own service class. If you only need search and not Solr specifically, Elasticsearch or OpenSearch with Spring Data are maintained alternatives.
You can keep it on an old Spring Boot 2.x line, but it receives no fixes. Plan a migration to SolrJ before upgrading Spring Boot or Java.
Add documents in batches with solr.add(collection, documents) and rely on commitWithin or auto-commit rather than committing per call.
SolrTemplate was the low-level template of Spring Data Solr, wrapping SolrClient with bean mapping, query objects and exception translation. The project is discontinued, so new applications should inject a SolrJ SolrClient, escape user input with ClientUtils.escapeQueryChars, and let Solr handle commits through commitWithin or auto-commit settings.