3

With a configuration as follows

@Configuration
@EnableSolrRepositories(basePackages={"com.foo"}, multicoreSupport=true)
public class SolrConfig {

    @Value("${solr.host}") String solrHost;

    @Bean
    public SolrClient solrClient() {
        return new HttpSolrClient(solrHost);
    }

    @Bean
    public SolrTemplate solrTemplate() {
        return new SolrTemplate(solrClient());
    }
}

I have a simple entity:

@SolrDocument(solrCoreName = "core1")
public class MyEntity implements Serializable {

If using SolrTemplate to execute queries, it does not use the coreName annotation on the document:

Page results = solrTemplate.queryForPage(search, MyEntity.class);

I get exception:

org.springframework.data.solr.UncategorizedSolrException: Error from server at http://localhost:8983/solr: Expected mime type application/octet-stream but got text/html.
[..]
Problem accessing /solr/select
[...]
<title>Error 404 Not Found</title>

Changing the SolrTemplate bean to:

@Bean
public SolrTemplate solrTemplate() {
    return new SolrTemplate(solrClient(), "core1");
}

works

sokie
  • 1,966
  • 22
  • 37

1 Answers1

3

The guys over at spring-data confirmed this is expected behaviour and the template won't read the core from the entity annotation.
So in a multicoreSupport=true environment, if you want to use both the repository and the template you'll have to create 2 beans: For the repository the base template:

    @Bean
    public SolrTemplate solrTemplate() {
        return new SolrTemplate(solrClient());
    }

and for injecting you will have another one:

    @Bean
    public SolrTemplate customTemplate() {
        return new SolrTemplate(solrClient(), "core1");
    }

Obviously if you don't need multicoreSupport=true none is needed!

sokie
  • 1,966
  • 22
  • 37