Injected SolrTemplate Resource not connected to HttpSolrServer

599 views Asked by At

I am using Spring data Solr 1.4 with a custom repository feature to try to implement a count function as per this article: link

I am using Solr configured to use multiple cores. My regular repository interface gets and uses the correct SolrTemplate and SolrServer instances, but the custom repository does not get the same instance of the template. In addition to using the @Resource to inject the bean, I have also tried getting the spring application context and pulling the bean from it. In both cases I am getting a solrServer bean reference that does not know what my core is, so the query fails when it tries to reach solr at: http://localhost:8983/solr instead of http://localhost:8983/solr/myCore

The bean definition looks like this:

@Bean
@Scope(value=ConfigurableBeanFactory.SCOPE_SINGLETON)
public SolrServer solrServer() {
    SolrServer server = new HttpSolrServer(solrServerUrl);
    return server;
}

@Bean(name = "solrTemplate")
@Scope(value=ConfigurableBeanFactory.SCOPE_SINGLETON)
public SolrTemplate solrTemplate(SolrServer server) throws Exception {
    SolrTemplate template = new SolrTemplate(server);
    template.setSolrCore(mainCore);
    return template;
}

And my test looks like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SearchEngineApplication.class, loader=SpringApplicationContextLoader.class)

public class MainRepositoryTest {

@Autowired
private MainRepository mainRepository;
...
    @Test
    public void testCountResults(){
        long count= citcMainRepository.count("red");
        System.out.println("Found " + count + " results for the term red" );
        assertTrue(count > 0);
    }
}

The test always fails:

<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /solr/select

When the proper url it should be reaching is /solr/select/myCore

Any suggestions as to what I might have misconfigured here? Any/all replies appreciated!

-- Griff

1

There are 1 answers

0
Griff On

I was able to solve the problem myself by creating a second bean of the same type and adding a qualifier to it.

In my SolrContext I defined the bean factory and bean definition as such: @Bean public SolrServerFactory solrServerFactory() { return new MulticoreSolrServerFactory(new HttpSolrServer(solrServerUrl)); }

// SolrTemplate for /solrServerUrl/mainCore
@Bean(name = "mainTemplate")
public SolrTemplate mainTemplate() throws Exception {
    SolrTemplate solrTemplate = new SolrTemplate(solrServerFactory());
    solrTemplate.setSolrCore(mainCore);
    return solrTemplate;
}

@Bean(name="mainRepo")
public MainRepository mainRepository() throws Exception {
    return new SolrRepositoryFactory(mainTemplate())
            .getRepository(CITCMainRepository.class,
                    new MainRepositoryImpl(mainTemplate()));
}

Then by qualifying the Repository in my UnitTest as such:

public class CITCMainRepositoryTest {

@Autowired
@Qualifier(value="mainRepo")
private MainRepository mainRepository; 

@Test
public void testFindByLastName() {
    List<SearchResponse> searchItems= mainRepository.findByLastName("Hunt");
    assertTrue(searchItems.size() > 0);
}
...

}

Hope this helps someone else.

-- Griff