`
sillycat
  • 浏览: 2551940 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring Boot and RESTful API(2)SOLR Repository

    博客分类:
  • JAVA
 
阅读更多
Spring Boot and RESTful API(2)SOLR Repository


Install SOLR on local
>wget http://apache.mirrors.tds.net/lucene/solr/6.6.0/solr-6.6.0.zip

Unzip and place it in the right directory, Start the Server on Local
>bin/solr start -p 8984

Visit page
http://localhost:8983/solr/#/

But there is no core there, so I follow this link and try to create one core named collection1
https://stackoverflow.com/questions/38424574/how-to-create-solr-6-cores

>cd /opt/solr/server/solr
>mkdir job
>cp -r configsets/basic_configs/conf ./job/conf

Visit the UI - click on Add Core — Put name and instanceDir ‘job'
http://localhost:8983/solr/#/~cores

Click on Schema and ‘Add Field’ to add filed.

Fetch Content from SOLR
query documents
https://github.com/spring-projects/spring-data-solr

Some Dependencies on pom.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>

POJO Job.java

package com.sillycat.jobsmonitorapi.domain;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.SolrDocument;

@SolrDocument(solrCoreName = "job")
public class Job {

@Id
@Field
private String id;

@Field("source_id")
private Integer sourceID;

@Field("job_reference")
private String jobReference;

public Job() {
}

public Job(String id, Integer sourceID, String jobReference) {
super();
this.id = id;
this.sourceID = sourceID;
this.jobReference = jobReference;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public Integer getSourceID() {
return sourceID;
}

public void setSourceID(Integer sourceID) {
this.sourceID = sourceID;
}

public String getJobReference() {
return jobReference;
}

public void setJobReference(String jobReference) {
this.jobReference = jobReference;
}

public String toString() {
return ToStringBuilder.reflectionToString(this);
}

}

JobRepositorySolr.java which is a interface
package com.sillycat.jobsmonitorapi.repository;

import java.util.List;

import org.springframework.data.solr.repository.Query;
import org.springframework.data.solr.repository.SolrCrudRepository;

import com.sillycat.jobsmonitorapi.domain.Job;

public interface JobRepositorySolr extends SolrCrudRepository<Job, String> {

@Query(fields = { "job_reference" })
List<Job> findBySourceID(Integer sourceID);
}

Unit Tests for that Repository, JobRepositorySolrTest.java
package com.sillycat.jobsmonitorapi.repository;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.IteratorUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.Assert;

import com.sillycat.jobsmonitorapi.domain.Job;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JobRepositorySolrTest {
@Autowired
    JobRepositorySolr jobRepositorySolr;
@Test
    public void save() throws Exception {
        Job job1 = new Job("1", 9527, "9527_java");
        Job job2 = new Job("2", 9527, "9527_nodejs");
        jobRepositorySolr.save(job1);
        jobRepositorySolr.save(job2);
    }
@Test
public void queryBasic() throws Exception{
Iterator<Job> jobsIt = jobRepositorySolr.findAll().iterator();
@SuppressWarnings("unchecked")
List<Job> jobs = IteratorUtils.toList(jobsIt);
Assert.notEmpty(jobs, "Fail to fetch anything from SOLR");
}
@Test
public void queryBySourceID() throws Exception{
List<Job> jobs = jobRepositorySolr.findBySourceID(9527);
Assert.notEmpty(jobs, "Fail to queryBySourceID from SOLR");
List<Job> jobEmptys = jobRepositorySolr.findBySourceID(9529);
Assert.isTrue(jobEmptys.isEmpty(), "Fail to filter out other sourceID");
}

}



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/


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics