Search code examples
javasolrsolrjspring-data-solr

What is the difference between SolrTemplate and SolrClient?


I was documenting my code that i wrote following some tutorials on Spring data for solr apache, and i realised i didn't know the diference between a solrTemplate and a SolrClient ?

i was documenting the following code :

@Configuration
@EnableSolrRepositories(basePackages = {"com.anouar.solr.nomenclaturespringdatasolr.repository", 
"com.anouar.solr.nomenclaturespringdatasolr.dataImportHandler"},
                    namedQueriesLocation = "classpath:solr-named-queries.properties")

public class SolrConfig {


@Value("${spring.data.solr.host}")
String solrURL;

/**
 * returns the bean that establishes the connection with Solr through port 8983
 *
 * @return SolrClient
 *
 * **/

@Bean
public SolrClient solrClient() {
    return new HttpSolrClient.Builder(solrURL).build();
}

/**
 *
 * @param client the bean that is connected to Solr through port 8983
 *
 * **/

@Bean
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
    return new SolrTemplate(client);
   }
}

Solution

  • Below is the description from apache documentation for SolrClient

    Abstraction through which all communication with a Solr server may be routed
    

    which means all your solr calls will be route via solrClient so we need to configure solr server address, port(few other also) to solrClient .

    where as solrTemplate is for solr operations like query, count, etc.. solrTemplate will use solrClient that's why while configuring solrTemplate , solrClient is passed.