Spring Boot and RESTful API(2)SOLR Repository
SpringBootandRESTfulAPI(2)SOLRRepository
InstallSOLRonlocal
>wgethttp://apache.mirrors.tds.net/lucene/solr/6.6.0/solr-6.6.0.zip
Unzipandplaceitintherightdirectory,StarttheServeronLocal
>bin/solrstart-p8984
Visitpage
http://localhost:8983/solr/#/
Butthereisnocorethere,soIfollowthislinkandtrytocreateonecorenamedcollection1
https://stackoverflow.com/questions/38424574/how-to-create-solr-6-cores
>cd/opt/solr/server/solr
>mkdirjob
>cp-rconfigsets/basic_configs/conf./job/conf
VisittheUI-clickonAddCore—PutnameandinstanceDir‘job'
http://localhost:8983/solr/#/~cores
ClickonSchemaand‘AddField’toaddfiled.
FetchContentfromSOLR
querydocuments
https://github.com/spring-projects/spring-data-solr
SomeDependenciesonpom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
POJOJob.java
packagecom.sillycat.jobsmonitorapi.domain;
importorg.apache.commons.lang3.builder.ToStringBuilder;
importorg.apache.solr.client.solrj.beans.Field;
importorg.springframework.data.annotation.Id;
importorg.springframework.data.solr.core.mapping.SolrDocument;
@SolrDocument(solrCorename="job")
publicclassJob{
@Id
@Field
privateStringid;
@Field("source_id")
privateIntegersourceID;
@Field("job_reference")
privateStringjobReference;
publicJob(){
}
publicJob(Stringid,IntegersourceID,StringjobReference){
super();
this.id=id;
this.sourceID=sourceID;
this.jobReference=jobReference;
}
publicStringgetId(){
returnid;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicIntegergetSourceID(){
returnsourceID;
}
publicvoidsetSourceID(IntegersourceID){
this.sourceID=sourceID;
}
publicStringgetJobReference(){
returnjobReference;
}
publicvoidsetJobReference(StringjobReference){
this.jobReference=jobReference;
}
publicStringtoString(){
returnToStringBuilder.reflectionToString(this);
}
}
JobRepositorySolr.javawhichisainterface
packagecom.sillycat.jobsmonitorapi.repository;
importjava.util.List;
importorg.springframework.data.solr.repository.Query;
importorg.springframework.data.solr.repository.SolrCrudRepository;
importcom.sillycat.jobsmonitorapi.domain.Job;
publicinterfaceJobRepositorySolrextendsSolrCrudRepository<Job,String>{
@Query(fields={"job_reference"})
List<Job>findBySourceID(IntegersourceID);
}
UnitTestsforthatRepository,JobRepositorySolrTest.java
packagecom.sillycat.jobsmonitorapi.repository;
importjava.util.Iterator;
importjava.util.List;
importorg.apache.commons.collections.IteratorUtils;
importorg.junit.Test;
importorg.junit.runner.RunWith;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.boot.test.context.SpringBootTest;
importorg.springframework.test.context.junit4.SpringRunner;
importorg.springframework.util.Assert;
importcom.sillycat.jobsmonitorapi.domain.Job;
@RunWith(SpringRunner.class)
@SpringBootTest
publicclassJobRepositorySolrTest{
@Autowired
JobRepositorySolrjobRepositorySolr;
@Test
publicvoidsave()throwsException{
Jobjob1=newJob("1",9527,"9527_java");
Jobjob2=newJob("2",9527,"9527_nodejs");
jobRepositorySolr.save(job1);
jobRepositorySolr.save(job2);
}
@Test
publicvoidqueryBasic()throwsException{
Iterator<Job>jobsIt=jobRepositorySolr.findAll().iterator();
@SuppressWarnings("unchecked")
List<Job>jobs=IteratorUtils.toList(jobsIt);
Assert.notEmpty(jobs,"FailtofetchanythingfromSOLR");
}
@Test
publicvoidqueryBySourceID()throwsException{
List<Job>jobs=jobRepositorySolr.findBySourceID(9527);
Assert.notEmpty(jobs,"FailtoqueryBySourceIDfromSOLR");
List<Job>jobEmptys=jobRepositorySolr.findBySourceID(9529);
Assert.isTrue(jobEmptys.isEmpty(),"FailtofilteroutothersourceID");
}
}
References:
http://projects.spring.io/spring-data-solr/
https://github.com/spring-projects/spring-data-solr
http://www.jianshu.com/p/e21fe5f3bd8c
http://www.kailing.pub/article/index/arcid/150.html
http://www.kailing.pub/article/index/arcid/150.html
http://docs.spring.io/spring-data/solr/docs/3.0.0.M3/reference/html/