一、Solrj简介
SolrJ是操作Solr的JAVA客户端,它提供了增加、修改、删除、查询Solr索引的JAVA接口。SolrJ针对 Solr提供了Rest 的HTTP接口进行了封装, SolrJ底层是通过使用httpClient中的方法来完成Solr的操作。
二、示例演示
1、创建一个Maven工程,引入依赖如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kang.solrj</groupId>
<artifactId>kang-solrj</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.1</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>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
</dependency>
</dependencies>
</project>
2、定义一个pojo用于测试
package com.kang.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();
}
}
@Field的作用
作用1:指定Bean的一个字段为Field
@Field
private String url;
schema.xml配置必需有url这个field,不然会报错。
<field name="url" type="string" indexed="true" stored="true" multiValued="false" default=""/>
当bean的字段为url,而实际schema.xml中的字段为s_url,其使用方法为:
@Field("s_url")
private String url;
schema.xml配置必需有url这个field,不然会报错。
<field name="s_url" type="string" indexed="true" stored="true" multiValued="false" default=""/>
3、针对该pojo的编写Solr增删改查操作
package com.kang.solrj.service;
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 com.kang.solrj.pojo.Foo;
public class SolrjService {
// 定义http的solr服务
private HttpSolrServer httpSolrServer;
public SolrjService(HttpSolrServer httpSolrServer) {
this.httpSolrServer = httpSolrServer;
}
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 {
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);
List<Foo> foos = queryResponse.getBeans(Foo.class);
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;
}
}
4、编写单元测试用例
4、编写单元测试用例
package com.kang.solrj.service;
import java.util.Arrays;
import java.util.List;
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 com.kang.solrj.pojo.Foo;
public class SolrjServiceTest {
private SolrjService solrjService;
private HttpSolrServer httpSolrServer;
@Before
public void setUp() throws Exception {
// 在url中指定core名称:enjoyshop
String url = "http://solr.enjoyshop.com/enjoyshop";
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(System.currentTimeMillis() + "");
foo.setTitle("轻量级Java EE企业应用实战(第3版):Struts2+Spring3+Hibernate整合开发(附CD光盘)");
this.solrjService.add(foo);
}
@Test
public void testDelete() throws Exception {
this.solrjService.delete(Arrays.asList("1416537175446"));
}
@Test
public void testSearch() throws Exception {
List<Foo> foos = this.solrjService.search("linux", 1, 10);
for (Foo foo : foos) {
System.out.println(foo);
}
}
@Test
public void testDeleteByQuery() throws Exception{
httpSolrServer.deleteByQuery("*:*");
httpSolrServer.commit();
}
}
三、使用Solrj完成数据的批量导入
系统接口的URL为:
http://manage.enjoyshop.com/rest/item?page={page}&rows=100
其中的page为当前页数。
返回的数据格式如下:
{"total":3093,"rows":[{"created":1495638218000,"updated":1495638218000,"id":1474391933,"title":"魅族","sellPoint":"按时到岗","price":2222200,"num":2222,"barcode":"222222","image":"http://image.enjoyshop.com/images/2017/05/24/2017052411030281105445.jpg","cid":560,"status":1},{"created":1495611140000,"updated":1495630686000,"id":1474391932,"title":"sadsadghhs","sellPoint":"testtetstts","price":22220,"num":333,"barcode":"34444","image":"http://image.enjoyshop.com/images/2017/05/24/2017052403321398508674.jpg","cid":3,"status":1},{"created":1495549388000,"updated":1495549388000,"id":1474391931,"title":"ssdas","sellPoint":"asdasd","price":22200,"num":222,"barcode":"222","image":"","cid":12,"status":1},{"created":1493037017000,"updated":1493037017000,"id":1474391929,"title":"test","sellPoint":"good","price":22200,"num":333,"barcode":"4444","image":"http://image.enjoyshop.com/images/2017/04/24/2017042408295162105014.jpg","cid":76,"status":1},{"created":1492591744000,"updated":1492591829000,"id":1474391928,"title":"java编程思想哈哈哈哈","sellPoint":"好好好","price":22200,"num":333,"barcode":"66666","image":"http://image.enjoyshop.com/images/2017/04/19/2017041904480598606613.jpg","cid":3,"status":1}]}
批量导入代码:
package com.kang.solrj.service;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
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 com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kang.solrj.pojo.Item;
public class ItemDataImportTest {
private HttpSolrServer httpSolrServer;
private static final ObjectMapper MAPPER = new ObjectMapper();
@Before
public void setUp() throws Exception {
// 在url中指定core名称:enjoyshop
// http://solr.enjoyshop.com/#/enjoyshop -- 界面操作
String url = "http://solr.enjoyshop.com/enjoyshop"; // 服务地址
HttpSolrServer httpSolrServer = new HttpSolrServer(url); // 定义solr的server
httpSolrServer.setParser(new XMLResponseParser()); // 设置响应解析器
httpSolrServer.setMaxRetries(1); // 设置重试次数,推荐设置为1
httpSolrServer.setConnectionTimeout(500); // 建立连接的最长时间
this.httpSolrServer = httpSolrServer;
}
@Test
public void testData() throws Exception {
// 通过后台系统的接口查询商品数据
String url = "http://manage.enjoyshop.com/rest/item?page={page}&rows=100";
int page = 1;
int pageSzie = 0;
do {
String u = StringUtils.replace(url, "{page}", "" + page);
System.out.println(u);
String jsonData = doGet(u);
JsonNode jsonNode = MAPPER.readTree(jsonData);
String rowsStr = jsonNode.get("rows").toString();
List<Item> items = MAPPER.readValue(rowsStr,
MAPPER.getTypeFactory().constructCollectionType(List.class, Item.class));
pageSzie = items.size();
this.httpSolrServer.addBeans(items);
this.httpSolrServer.commit();
page++;
} while (pageSzie == 100);
}
private String doGet(String url) throws Exception {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建http GET请求
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
return null;
}
}
四、Spring整合Solrj
首先建立一个外部属性文件用来定义Solr的相关参数
solr.url=http://solr.enjoyshop.com/enjoyshop
solr.maxRetries=1
solr.connectionTimeout=500
- 1
- 2
- 3
然后配置Spring的XML文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg index="0" value="${solr.url}"/>
<!-- 设置响应解析器 -->
<property name="parser">
<bean class="org.apache.solr.client.solrj.impl.XMLResponseParser"/>
</property>
<!-- 重试次数 -->
<property name="maxRetries" value="${solr.maxRetries}"/>
<property name="connectionTimeout" value="${solr.connectionTimeout}"/>
</bean>
</beans>
这里只是简单的示例,具体可参考相关源码进行配置。
五、源码Demo点我