Spring Boot and RESTful API(2)SOLR Repository

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/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值