Solr III 通过SolrJ与Solr服务器进行数据交互
1.引入所需包
将如下jar包引入项目
2.获取与Solr服务器的连接
/**
* 获取与指定Solr地址的连接
* @param solrUrl
* @return
*/
public HttpSolrServer getSolrServer(String solrUrl){
final int ONE_HUNDRED_MS = 100;
if(solrUrl == null || "".equals(solrUrl)){
throw new RuntimeException("Solr url can not be empty!");
}
HttpSolrServer solrServer = new HttpSolrServer(solrUrl);
solrServer.setConnectionTimeout(ONE_HUNDRED_MS);
solrServer.setDefaultMaxConnectionsPerHost(100);
solrServer.setMaxTotalConnections(100);
return solrServer;
}
注:URL书写规则
在页面中访问某个core(例如core0)时,url是类似这种格式:http://127.0.0.1:8080/solr/#/core0。但是通过SolrJ访问Solr服务器时,url中不应该包含#,如http://127.0.0.1:8080/solr/core0
3.增删改Solr数据
具体代码如下。要注意的是,在Solr中,增和改是相同的代码。当添加的时候,遇到相同的id,则会将旧的数据覆盖掉,这就起到了修改数据的效果。
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
略……
/**
* 向指定的Solr地址添加一条数据
* @param solrUrl
* @param doc
* @throws Exception
*/
public void add(String solrUrl, SolrInputDocument doc) throws Exception {
if(doc == null){
throw new RuntimeException("SolrInputDocument object can not be null!");
}
HttpSolrServer solr = getSolrServer(solrUrl);
solr.add(doc);
solr.commit();
}
注:SolrInputDocument对象的内容如下:
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "aaa");
doc.addField("name", "qiuyj");
doc.addField("age", 26);
doc.addField("birthday", "1988-07-21");
doc.addField("identityID", "110xxxxx");
/**
* 向指定的Solr地址用JavaBean添加一条数据
* @param solrUrl
* @param obj
* @throws Exception
*/
public void add(String solrUrl, Object obj) throws Exception {
if(obj == null){
throw new RuntimeException("Object to be inserted can not be null!");
}
HttpSolrServer solr = getSolrServer(solrUrl);
solr.addBean(obj);
solr.commit();
}
注:如果想以Java Bean的形式添加到Solr中,该Java Bean应该像如下例子一样添加Solr相关注解
import org.apache.solr.client.solrj.beans.Field;
public class User {
private String id;
private String name;
private int age;
private String birthday;
private String identityID;
public String getId() {
return id;
}
@Field
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
@Field("name") //如果Java Bean的字段名称与Solr中的字段名称不一致,则应该像这样给注解构面添加字符串,代表在Solr中的字段名称是什么
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@Field
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
@Field
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getIdentityID() {
return identityID;
}
@Field
public void setIdentityID(String identityID) {
this.identityID = identityID;
}
}
/**
* 向指定Solr地址批量添加数据
* @param solrUrl
* @param docs
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void addAll(String solrUrl, Collection<? extends Object> objs) throws Exception {
if(objs == null || objs.size() == 0){
throw new RuntimeException("Object collection can not be empty!");
}
HttpSolrServer solr = getSolrServer(solrUrl);
if(objs.iterator().next() instanceof SolrInputDocument){
solr.add((Collection<SolrInputDocument>)objs);
} else {
solr.addBeans(objs);
}
solr.commit();
}
/**
* 根据给定的id,从solr中删除对应信息
* @param solrUrl
* @param ids
*/
public void deleteByIds(String solrUrl, String ... ids) throws Exception {
if(ids == null || ids.length == 0){
throw new RuntimeException("Ids can not be empty!");
}
HttpSolrServer solr = getSolrServer(solrUrl);
solr.deleteById(Arrays.asList(ids));
solr.commit();
}
/**
* 删除指定Solr路径下符合指定查询条件的数据
* @param solrUrl
* @param condition
* @throws Exception
*/
public void deleteByCondition(String solrUrl, String condition) throws Exception {
if(condition == null || "".equals(condition)){
throw new RuntimeException("Condition can not be empty!");
}
HttpSolrServer solr = getSolrServer(solrUrl);
solr.deleteByQuery(condition);
solr.commit();
}
/**
* 删除指定Solr路径下的所有数据
* @param solrUrl
* @throws Exception
*/
public void deleteAll(String solrUrl) throws Exception {
this.deleteByCondition(solrUrl, "*:*");
}
4.查询Solr数据
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
略……
/**
* 根据 指定查询条件从Solr中查询数据,并以SolrDocument的List形式返回
* @param solrUrl
* @param query
* @return
* @throws Exception
*/
public SolrDocumentList queryAndGetSolrDocumentList(String solrUrl, SolrQuery query) throws Exception {
if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}
HttpSolrServer solr = getSolrServer(solrUrl);
QueryResponse resp = solr.query(query);
return resp.getResults();
}
注:SolrQuery对象的创造方法如下:
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
SolrQuery对象还有众多其他查询条件,如排序、分组等。在本例中不一一赘述,以后会专门有一章介绍Solr查询的文章。
/**
* 根据 指定查询条件从Solr中查询数据,并以Java Bean的List形式返回
* @param solrUrl
* @param query
* @param returnClass
* @return
* @throws Exception
*/
public <T> List<T> queryAndGetBeanList(String solrUrl, SolrQuery query, Class<T> returnClass) throws Exception {
if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}
if(returnClass == null){
throw new RuntimeException("Return class can not be null!");
}
HttpSolrServer solr = getSolrServer(solrUrl);
QueryResponse resp = solr.query(query);
return resp.getBeans(returnClass);
}
如何获取符合条件的数据的总数:
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.setStart(0);
query.setRows(10);
HttpSolrServer solr = getSolrServer(solrUrl);
QueryResponse resp = solr.query(query);
long totalCount = resp.getResults().getNumFound();
//以上代码执行的结果,即便限定只查询10条出来,但是totalCount的数量也会是实际的符合查询条件的数据的总数