solrspring-data-solr

No Association Found Error while indexing data to Solr using Spring-Data-Solr


I am trying out a sample service application of spring data mongoDB + spring data solr where MongoDB is used to persist the data and solr for indexing and searching.

The save operation to MongoDB happens successfully in the service class. But on calling the SolrOperation save() method the service crashes with the error log as below:

SEVERE [com.sun.jersey.spi.container.ContainerResponse] (defaulttask-1)The 
RuntimeException could not be mapped to a response, re-throwing the HTTP 
container:org.springframework.data.solr.UncategorizedSolrException:No
association fond!; nested exception is java.lang.IllegalStateException: No 
association found! at     org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:171)

As I analyse the log further deep it says:

Caused by: java.lang.IllegalStateException:No association found!
at org.springframework.data.mapping.PersistentProperty.getRequiredAssociation(PersistentProperty.java:166)

The line getConverter().write(bean, document) inside convertBeanToSolrInputDocument () inside SolrTemplate is throwing the error.

The DAO method

public String addToRepo(MyEntity myEntity){
mongoOperation.save(myEntity); //works fine data saved to MongoDB
solrOperation.save("collectionName",myEntity); //generates above exception
return "success";
}

I am using Spring 5 + solrj-6.1.0 + spring-data-solr-4.0.2.

The solroperation has been correctly loaded as:

ApplicationContext SOLR_CONFIG_APP_CTX = new AnnotationConfigApplicationContext(SpringSolrConfig.class);
SolrOperations solrOperation = (SolrOperations)ctx.getBean("solrTemplate");

public static final SolrOperations SOLR_OPS=
(SolrOperations)SOLR_CONFIG_APP_CTX.getBean("solrTemplate");

SpringSolrConfig.java

@Configuration
public class SpringSolrConfig extends AbstractSolrConfig {
public SolrClientFactory solrClientFactory (){
SolrClient solrClient = new HttpSolrClient.Builder(solrUrl).build();
HttpSolrClientFactory solrClientFactory = new HttpSolrClientFactory (solrClient);
return solrClientFactory;
}
}

The SpringConfig.xml file looks like this:

<mongo:mongo host="195.168.1.140" port="27017"/>
<mongo:dbfactory dbname="myDB"/>
<bean id="mongoTemplate"
class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg-name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
<repositories base-package="sample.package.repositories"/>
<bean id="myEntityRepo" class="sample.package..repositories.MyEntityRepositoryInterface"/>
<solr:repositories base-package="sample.package.repositories"/>
<solr:sorl-server id="solrServer" url="http://localhost:8983/solr"/>
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg index="0" ref="solrServer"/>
</bean>

Thanks in advance for helping me troubleshoot this!


Solution

  • I updated my SpringSolrConfig file as below to fix the problem. Courtesy: https://jira.spring.io/browse/DATASOLR-394

        @Configuration
        public class SpringSolrConfig extends AbstractSolrConfig {
    
        String solrUrl = "http://localhost:8983/solr/"; // TODO read this ideally from spring-configuration.xml file
        public SolrClientFactory solrClientFactory (){
        SolrClient solrClient = new HttpSolrClient.Builder(solrUrl).build();
        HttpSolrClientFactory solrClientFactory = new HttpSolrClientFactory (solrClient);
        return solrClientFactory;
        }
    
        @Bean
        public SolrTemplate solrTemplate () {
        SolrTemplate solrTemplateObj = new SolrTemplate(solrClientFactory));
        // This ensures that the default MappingSolrConverter.java is not used for converting the bean to a Solr Document before indexing
        solrTemplateObj.setSolrConverter(new SolrJConverter());
        return solrTemplateObj;
        }
    
        }