Reputation: 1620
I'm using SolrCrudRepository for all CRUD Operations. It should save and commit upon calling SolrCrudRepository's save() method but that is not what it's doing. I need to use SolrTemplate's commit() method to completely save (e.g save & commit) the document. If I remove the line solrTemplateThree.getSolrServer().commit()
then it will not commit. Therefore I am unable to query the data (need to reload the core directly from solr admin page then query). Any pointers or comments on possible causes will be welcomed.
What other alternatives besides SolrCrudRepository is available to save/delete solr indexes?
@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);
}
}
Config 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());
}
}
Upvotes: 0
Views: 3132
Reputation: 1585
With Spring-Data-Solr-4.0.2.RELEASE auto commit of indexes can be achieved as below:
SolrOperation solrOperations; //instantiated it from Spring-Solr-Data package
solrOperations.saveBean("coreName",solrDoc);
solrOperations.commit("coreName");
Upvotes: 0
Reputation: 22555
When you index a document in Solr, it will not be available for searching until it has been committed to the index. This is why you must call the .commit()
method (or reload the core) in order to see this document when querying.
However, there was a recent issue DATASOLR-107 Added commitWithin Support that added an additional parameter to the .save()
method (along with some others) that will allow you to specify in milliseconds how long before the document is to be committed. Change your code as follows:
Update : Appears you will need to use the SolrTemplate to facilitate a commitWithin save.
// Will save the document and tell Solr to commit it within 3 seconds (3000 ms).
solrTemplateThree.save(solrDoc, 3000);
For more on commit strategies within Solr, please refer to the following:
Upvotes: 2