使用Spring-Data-Solr将数据索引到Solr时没有关联发现错误

时间:2018-11-26 07:20:40

标签: solr spring-data-solr

我正在尝试一个spring数据mongoDB + spring数据solr的示例服务应用程序,其中MongoDB用于持久化数据和solr以进行索引和搜索。

对MongoDB的保存操作在服务类中成功完成。但是,在调用SolrOperation save()方法时,服务崩溃,并显示以下错误日志:

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)

当我更深入地分析日志时,它说:

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

getConverter().write(bean, document)convertBeanToSolrInputDocument ()内的行SolrTemplate引发错误。

DAO方法

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

我正在使用Spring 5 + solrj-6.1.0 + spring-data-solr-4.0.2。

solroperation已正确加载为:

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;
}
}

SpringConfig.xml 文件如下所示:

<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>

预先感谢您帮助我解决此问题!

1 个答案:

答案 0 :(得分:0)

我如下更新了我的SpringSolrConfig文件以解决此问题。礼貌: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;
    }

    }