public class SolrjServiceTest {
private SolrjService solrjService;
@Before
public void setUp() throws Exception {
// 在url中指定core名称:jt
String url = "http://solr.jt.com/jt";
HttpSolrServer httpSolrServer = new HttpSolrServer(url); //定义solr的server
httpSolrServer.setParser(new XMLResponseParser()); // 设置响应解析器
httpSolrServer.setMaxRetries(1); // 设置重试次数,推荐设置为1
httpSolrServer.setConnectionTimeout(500); // 建立连接的最长时间
this.httpSolrServer = httpSolrServer;
solrjService = new SolrjService(httpSolrServer);
}
新增
运行测试方法cn.jt.solrj.service.SolrjServiceTest. testAdd()
@Test
public void testAdd() throws Exception {
Foo foo = new Foo();
foo.setId(1425955126820L);
foo.setTitle("new - 轻量级Java EE企业应用实战(第3版):Struts2+Spring3+Hibernate整合开发(附CD光盘)");
this.solrjService.add(foo);
}
执行成功后,在solr中可以查询到add的数据。
删除
按id删除。
@Test
public void testDelete() throws Exception {
this.solrjService.delete(Arrays.asList("1433151614240"));
}
修改
按id值,有值就修改,没值就新增。
@Test
public void testAdd() throws Exception {
Foo foo = new Foo();
foo.setId(1425955126820L);
foo.setTitle("new - 轻量级Java EE企业应用实战(第3版):Struts2+Spring3+Hibernate整合开发(附CD光盘)");
this.solrjService.add(foo);
}
查询
@Test
public void testSearch() throws Exception {
List<Foo> foos = this.solrjService.search("java", 1, 10);
for (Foo foo : foos) {
System.out.println(foo);