0

I'm having issues upgrading my solr configuration from 1.5.4 to 3.0.0.M4 (along with a Spring Boot update to 2.0.0.M2).

Previous config file (spring-data-solr 1.5.4.RELEASE):

@Configuration
@EnableSolrRepositories(value = "com.bar.foo.repository.solr", multicoreSupport = true)
public class SolrConfiguration implements EnvironmentAware{

    private RelaxedPropertyResolver propertyResolver;
    private Environment             environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
        this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.data.solr.");
    }

    @Bean
    public SolrServer solrServer() {
        String solrHost = propertyResolver.getProperty("host");
        return new HttpSolrServer(solrHost);
    }


    @Bean(name = "core1SolrTemplate")
    public SolrOperations core1SolrTemplate() {
        HttpSolrServer httpSolrServer = new HttpSolrServer(propertyResolver.getProperty("host"));
        return new SolrTemplate(httpSolrServer, "Core1");
    }

    @Bean(name = "core2SolrTemplate")
    public SolrOperations core2SolrTemplate() {
        HttpSolrServer httpSolrServer = new HttpSolrServer(propertyResolver.getProperty("host"));
        return new SolrTemplate(httpSolrServer, "Core2");
    }
    ...
}

We are then using these solrtemplate in the code as

@Resource SolrTemplate core1SolrTemplate;
@Resource SolrTemplate core2SolrTemplate;

Trying to update, I see that HttpSolrServer is no longer available, and we should use SolrClient, but with the removal of multicoreSupport and the ability to declare the template's core I don't get how to have a template per core (or a single one able to detect which core to query ?)

Here's my current non-working config :

@Configuration
@EnableSolrRepositories(value = "com.bar.foo.repository.solr")
public class SolrConfiguration {

    @Inject
    private Environment             environment;

    @Bean
    public SolrClient solrClient() {
        return new HttpSolrClient.Builder(environment.getProperty("spring.data.solr.host")).build();
    }

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

}

Our Pojos are configured as :

@SolrDocument(solrCoreName = "Core1")
public class Foo{
    @Id
    private String                     id;

    ...
}

and the call throwing the error

core1SolrTemplate.queryForPage(simpleQuery, Foo.class)

"Caused by: org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://192.168.99.100:8983/solr: Expected mime type application/octet-stream but got text/html. "

It looks like the solrtemplate is only calling the baseurl without selecting any core.

I also tried creating multiple SolrTemplate as before and passing a parameter to the solrClient() to construct a specific url per core but was unsuccessful to make it work (I had errors where the 2nd template was apparently using the first solrtemplate bean, pointing to the wrong core).

How should I configure Solr to be able to query multiple cores ?

Current (2.1.4) solr doc : http://docs.spring.io/spring-data/data-solr/docs/current/reference/html/#solr.multicore

Multicore is not described in the 3.0.0.M4 doc : http://docs.spring.io/spring-data/data-solr/docs/3.0.0.M4/reference/html/#solr.multicore

Christophe Douy
  • 813
  • 1
  • 11
  • 27

2 Answers2

0

I think you need to add the @SolrDocument annotation to your pojos to use the multicore support. See issue below for discussion. Depends on how you are setting it up though.

Spring Data Solr multiple cores and repository

Chad
  • 166
  • 7
  • I've updated my question with a pojo & solrTemplate call generating the issue. We already have these annotations, and it was working OK with solr 1.5.4 where we could define a template per core. But SolrTemplate does not have a constructor with a coreName anymore : http://docs.spring.io/spring-data/solr/docs/1.5.4.RELEASE/api/index.html?org/springframework/data/solr/core/SolrTemplate.html vs http://docs.spring.io/spring-data/solr/docs/3.0.0.M4/api/index.html?org/springframework/data/solr/core/SolrTemplate.html & the config on http://projects.spring.io/spring-data-solr/ is obsolete for 3.0 – Christophe Douy Jul 21 '17 at 20:36
0

Looks like I was tired, the following configuration seems to be working fine - feel free to improve it anytime

@Configuration
@EnableSolrRepositories(basePackages={"com.example.demo.repository"})
public class SolrContext {

    static final String SOLR_HOST = "solr.host";

    @Autowired
    private Environment environment;

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

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

    @Bean
    public SolrTemplate core1Template() {
        return new SolrTemplate(new HttpSolrClient.Builder(environment.getProperty(SOLR_HOST)+"/core1").build());
    }

    @Bean
    public SolrTemplate core2Template() {
        return new SolrTemplate(new HttpSolrClient.Builder(environment.getProperty(SOLR_HOST)+"/core2").build());
    }
}

With examples: https://github.com/Farael49/spring-solr-config/blob/master/demo/src/main/java/com/example/demo/web/CoreController.java

Christophe Douy
  • 813
  • 1
  • 11
  • 27