solr java demo 基础入门

时间:2022-08-27 17:12:16
 
    <!--solr的maven依赖-->
<dependencies>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>

//pojo

package com.j1.solrj.pojo;

import org.apache.solr.client.solrj.beans.Field;

public class Foo {

    @Field("id")
private String id; @Field("title")
private String title; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} @Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Foo [id=");
builder.append(id);
builder.append(", title=");
builder.append(title);
builder.append("]");
return builder.toString();
} }
//Service

package com.j1.solrj.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map; import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse; import cn.itcast.solrj.pojo.Foo; public class SolrjService { // 定义http的solr服务
private HttpSolrServer httpSolrServer; public SolrjService(HttpSolrServer httpSolrServer) {
this.httpSolrServer = httpSolrServer;
} /**
* 新增数据到solr服务
*
* @param foo
* @throws Exception
*/
public void add(Foo foo) throws Exception {
this.httpSolrServer.addBean(foo); //添加数据到solr服务器
this.httpSolrServer.commit(); //提交
} public void delete(List<String> ids) throws Exception {
this.httpSolrServer.deleteById(ids);
this.httpSolrServer.commit(); //提交
} public List<Foo> search(String keywords, Integer page, Integer rows) throws Exception {
List<Foo> foos=new ArrayList <Foo> ();
SolrQuery solrQuery = new SolrQuery(); //构造搜索条件
solrQuery.setQuery("title:" + keywords); //搜索关键词
// 设置分页 start=0就是从0开始,,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了。
solrQuery.setStart((Math.max(page, 1) - 1) * rows);
solrQuery.setRows(rows); //是否需要高亮
boolean isHighlighting = !StringUtils.equals("*", keywords) && StringUtils.isNotEmpty(keywords); if (isHighlighting) {
// 设置高亮
solrQuery.setHighlight(true); // 开启高亮组件
solrQuery.addHighlightField("title");// 高亮字段
solrQuery.setHighlightSimplePre("<em>");// 标记,高亮关键字前缀
solrQuery.setHighlightSimplePost("</em>");// 后缀
} // 执行查询
QueryResponse queryResponse = this.httpSolrServer.query(solrQuery);
try{
foos = queryResponse.getBeans(Foo.class);
}catch(Exception e){
e.printStackTrace();
} if (isHighlighting) {
// 将高亮的标题数据写回到数据对象中
Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
for (Map.Entry<String, Map<String, List<String>>> highlighting : map.entrySet()) {
for (Foo foo : foos) {
if (!highlighting.getKey().equals(foo.getId().toString())) {
continue;
}
foo.setTitle(StringUtils.join(highlighting.getValue().get("title"), ""));
break;
}
}
} return foos;
} }
//test 测试

package com.j1.solrj.service;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List; import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.junit.Before;
import org.junit.Test; import cn.itcast.solrj.pojo.Foo; public class SolrjServiceTest { private SolrjService solrjService; private HttpSolrServer httpSolrServer; @Before
public void setUp() throws Exception {
// 在url中指定core名称:taotao
// String url = "http://solr.taotao.com/taotao";
String url = "http://solr.taotao.com/taotao";
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);
} @Test
public void testAdd() throws Exception {
Foo foo = new Foo();
foo.setId("1425954987819");
foo.setTitle("new - 轻量级Java EE企业应用实战(第3版):Struts2+Spring3+Hibernate整合开发(附CD光盘)");
try{
this.solrjService.add(foo);
}catch(Exception e){
e.printStackTrace();
} } @Test
public void testDelete() throws Exception {
this.solrjService.delete(Arrays.asList("1425954987818"));
} @Test
public void testSearch() throws Exception {
List<Foo> foos = this.solrjService.search("new", 1, 10);
for (Foo foo : foos) {
System.out.println(foo);
}
} @Test
public void testDeleteByQuery() throws Exception{
httpSolrServer.deleteByQuery("title:轻量级Java");
httpSolrServer.commit();
} }

测试结果:

Foo [id=1474391928, title=ip6-测试-<em>new</em>2]

Foo [id=1473655354, title=<em>new</em>19 - 小米 红米Note 8G内存移动4G合约增强版 不含合约计划 白色 移动4G]

Foo [id=1473655354, title=new19 - 小米 红米Note 8G内存移动4G合约增强版 不含合约计划 白色 移动4G]

Foo [id=1425955126820, title=<em>new</em> - 轻量级Java EE企业应用实战(第3版):Struts2+Spring3+Hibernate整合开发(附CD光盘)]

solr java demo 基础入门