我将 SolrCrudRepository 用于所有 CRUD 操作。它应该在调用 SolrCrudRepository 的 save() 方法时保存并提交,但这不是它正在做的事情。我需要使用 SolrTemplate 的 commit() 方法来完全保存(例如保存和提交)文档。如果我删除该行solrTemplateThree.getSolrServer().commit()
,那么它将不会提交。因此我无法查询数据(需要直接从 solr 管理页面重新加载核心然后查询)。欢迎任何关于可能原因的指示或评论。
除了 SolrCrudRepository 之外,还有哪些其他替代方法可用于保存/删除 solr 索引?
@Service("solrDocumentService")
@Repository
public class SolrDocumentServiceImpl implements SolrDocumentService{
private static final Logger logger = LoggerFactory.getLogger(SolrDocumentServiceImpl.class);
@Autowired @Qualifier("solrTemplateThree")
private SolrTemplate solrTemplateThree;
@Override
public SolrDocument save(SolrDocument solrDoc) {
SolrDocument saved = solrDocumentRepository().save(solrDoc);
try {
solrTemplateThree.getSolrServer().commit();
} catch (SolrServerException e) {
logger.info(e.getMessage());
} catch (IOException e) {
logger.info(e.getMessage());
}
return saved;
}
private SolrDocumentRepository solrDocumentRepository(){
return new SolrRepositoryFactory(solrTemplateThree).getRepository(SolrDocumentRepository.class);
}
}
配置类
@Configuration
@EnableSolrRepositories("repository")
@ComponentScan(basePackages={"..."})
//@Profile("production")
@PropertySource("classpath:solr.properties")
public class HttpSolrConfig {
@Autowired
private Environment environment;
@Bean
public HttpSolrServerFactoryBean solrServerFactoryBeanAutocomplete() {
HttpSolrServerFactoryBean factory = new HttpSolrServerFactoryBean();
factory.setUrl(environment.getRequiredProperty("solr.server.core.three.url"));
return factory;
}
@Bean
public SolrTemplate solrTemplateThree() throws Exception {
return new SolrTemplate(solrServerFactoryBeanAutocomplete().getObject());
}
@Bean
public SolrTemplate solrTemplate() throws Exception {
return new SolrTemplate(solrServerFactoryBeanUsers().getObject());
}
}