修改主方法
public int saveContent(String enterpriseId, String enterpriseName, String lableType, String resouce, String pubDate,
String content) {
int state = 0;
LBHttpSolrServer server = SolrUtil.getSolrServer(ap.getEnterprisenewSolrUrl());
SolrQuery query = new SolrQuery();
query.set("q", "enterpriseId:" + enterpriseId);
try {
QueryResponse qr = server.query(query);
List
// 设置需要保存的文章信息
for (EnterpriseContentBean bean : contentList) {
bean.setEnterpriseId(enterpriseId);
bean.setEnterpriseName(enterpriseName);
List
contents.add(content);
bean.setContent(contents);
bean.setPubDate(pubDate);
System.out.println("pubDate======>" + pubDate);
List
bean.setLableType(lableTypes);
bean.setResource(resouce);
bean.setIsVisited_s("1");
}
server.addBeans(contentList);
server.commit();
} catch (SolrServerException e) {
state = 1;
System.out.println("修改solr数据报错");
e.printStackTrace();
} catch (IOException e) {
state = 1;
System.out.println("修改solr数据报错");
e.printStackTrace();
}
return state;
}
删除主方法
public int deletContent(String enterpriseId) {
LBHttpSolrServer server = SolrUtil.getSolrServer(ap.getEnterprisenewSolrUrl());
int state = 0;
try {
server.deleteById(enterpriseId);
server.commit();
} catch (SolrServerException e) {
state = 1;
System.out.println("删除solr数据报错");
e.printStackTrace();
} catch (IOException e) {
state = 1;
System.out.println("删除solr数据报错");
e.printStackTrace();
}
return state;
}
solr工具类
package com.dinfo.boc.utils;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
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.SolrServerException;
import org.apache.solr.client.solrj.impl.LBHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import com.dinfo.boc.enterprise.bean.EnterpriseContentBean;
import com.dinfo.boc.enterprisenew.bean.SolrQueryResult;
/**
* 与Solr服务器交互的工具类
* @author qiuyj
*
*/
public class SolrUtil {
/**
* 获取与指定Solr地址的连接
* @param solrUrl
* @return
*/
public static LBHttpSolrServer getSolrServer(String solrUrl){
final int ONE_HUNDRED_MS = 10000000;
if(solrUrl == null || "".equals(solrUrl)){
throw new RuntimeException("Solr url can not be empty!");
}
LBHttpSolrServer solrServer = null;
try {
solrServer = new LBHttpSolrServer(solrUrl);
solrServer.setConnectionTimeout(ONE_HUNDRED_MS);
} catch (MalformedURLException e) {
e.printStackTrace();
} //SolrUtil.getSolrServer(solrUrl);
//solrServer.setDefaultMaxConnectionsPerHost(100);
//solrServer.setMaxTotalConnections(100);
return solrServer;
}
/**
* 向指定的Solr地址添加一条数据
* @param solrUrl
* @param doc
* @throws Exception
*/
public static void add(String solrUrl, SolrInputDocument doc) throws Exception {
if(doc == null){
throw new RuntimeException("SolrInputDocument object can not be null!");
}
LBHttpSolrServer solr = getSolrServer(solrUrl);
solr.add(doc);
solr.commit();
}
/**
* 向指定的Solr地址用JavaBean添加一条数据
* @param solrUrl
* @param obj
* @throws Exception
*/
public static void add(String solrUrl, Object obj) throws Exception {
if(obj == null){
throw new RuntimeException("Object to be inserted can not be null!");
}
LBHttpSolrServer solr = getSolrServer(solrUrl);
solr.addBean(obj);
solr.commit();
}
/**
* 向指定Solr地址批量添加数据
* @param solrUrl
* @param docs
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static void addAll(String solrUrl, Collection extends Object> objs) throws Exception {
if(objs == null){
throw new RuntimeException("Object collection can not be null!");
}
if(objs.size() == 0){
return;
}
LBHttpSolrServer solr = getSolrServer(solrUrl);
if(objs.iterator().next() instanceof SolrInputDocument){
solr.add((Collection
} else {
solr.addBeans(objs);
}
solr.commit();
}
/**
* 根据给定的id,从solr中删除对应信息
* @param solrUrl
* @param ids
*/
public static void deleteByIds(String solrUrl, String ... ids) throws Exception {
if(ids == null || ids.length == 0){
throw new RuntimeException("Ids can not be empty!");
}
LBHttpSolrServer solr = getSolrServer(solrUrl);
solr.deleteById(Arrays.asList(ids));
solr.commit();
}
public static void deleteByIds(String solrUrl, Integer ... ids) throws Exception {
if(ids == null || ids.length == 0){
throw new RuntimeException("Ids can not be empty!");
}
List
for(Integer id : ids){
stringIdList.add("" + id);
}
LBHttpSolrServer solr = getSolrServer(solrUrl);
solr.deleteById(stringIdList);
solr.commit();
}
/**
* 删除指定Solr路径下符合指定查询条件的数据
* @param solrUrl
* @param condition
* @throws Exception
*/
public static void deleteByCondition(String solrUrl, String condition) throws Exception {
if(condition == null || "".equals(condition)){
throw new RuntimeException("Condition can not be empty!");
}
LBHttpSolrServer solr = getSolrServer(solrUrl);
solr.deleteByQuery(condition);
solr.commit();
}
/**
* 删除指定Solr路径下的所有数据
* @param solrUrl
* @throws Exception
*/
public static void deleteAll(String solrUrl) throws Exception {
deleteByCondition(solrUrl, "*:*");
}
/**
* 根据 指定查询条件从Solr中查询数据,并以SolrDocument的List形式返回
* @param solrUrl
* @param query
* @return
* @throws Exception
*/
public static SolrDocumentList queryAndGetSolrDocumentList(String solrUrl, SolrQuery query) throws Exception {
if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}
LBHttpSolrServer solr = getSolrServer(solrUrl);
QueryResponse resp = solr.query(query);
return resp.getResults();
}
/**
* 根据 指定查询条件从Solr中查询数据,并以QueryResponse形式返回
* @param solrUrl
* @param query
* @return
* @throws Exception
*/
public static QueryResponse queryAndGetSolrQueryResponse(String solrUrl, SolrQuery query) throws Exception {
if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}
LBHttpSolrServer solr = getSolrServer(solrUrl);
QueryResponse resp = solr.query(query);
return resp;
}
/**
* 根据 指定查询条件从Solr中查询数据,并以Java Bean的List形式返回
* @param solrUrl
* @param query
* @param returnClass 返回的List集合的泛型
* @return
* @throws Exception
*/
public static
if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}
if(returnClass == null){
throw new RuntimeException("Return class can not be null!");
}
LBHttpSolrServer solr = getSolrServer(solrUrl);
QueryResponse resp = solr.query(query);
return resp.getBeans(returnClass);
}
/**
* 根据 指定查询条件从Solr中查询数据,并以SolrQueryResult对象的形式返回,其中包含List对象和totalCount
* @param solrUrl
* @param query
* @param returnClass 返回的List集合的泛型
* @return
* @throws Exception
*/
public static
SolrQueryResult
if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}
if(returnClass == null){
throw new RuntimeException("Return class can not be null!");
}
LBHttpSolrServer solr = getSolrServer(solrUrl);
solr.setConnectionTimeout(10000);
QueryResponse resp = solr.query(query);
List
long totalCount = resp.getResults().getNumFound();
result.setResultList(resultList);
result.setTotalCount(totalCount);
return result;
}
/**
* 根据 指定查询条件从Solr中查询数据,并以SolrQueryResult对象的形式返回,其中包含List对象和totalCount
* @param solrUrl
* @param query
* @param returnClass 返回的List集合的泛型
* @return
* @throws Exception
*/
public static
SolrQueryResult
if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}
if(returnClass == null){
throw new RuntimeException("Return class can not be null!");
}
QueryResponse resp = solr.query(query);
List
long totalCount = resp.getResults().getNumFound();
result.setResultList(resultList);
result.setTotalCount(totalCount);
return result;
}
/**
* 用以过滤一些影响Solr查询的特殊字符,如左右括号、星号等
* @param str
* @return
*/
public static String filterSpecialCharacters(String str){
if(str == null){
return str;
}
str = str.replace("(", "\\(");
str = str.replace(")", "\\)");
str = str.replace("*", "\\*");
return str;
}
public static void updateSolrById(LBHttpSolrServer server){
SolrQuery query = new SolrQuery();
String id="5d495a00a5c8118c03ef0bec0111dd8d";
int state=0;
String name="新疆金风科技股份有限公司";
query.set("q", "enterpriseId:"+id);
try {
QueryResponse qr = server.query(query);
List
//设置需要保存的文章信息
for(EnterpriseContentBean bean:contentList){
// bean.setEnterpriseId(enterpriseId);
bean.setEnterpriseName(name);
bean.setResource("东方财富网港股频道");
}
server.addBeans(contentList);
server.commit();
} catch (SolrServerException e) {
state = 1;
e.printStackTrace();
} catch (IOException e) {
state = 1;
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
LBHttpSolrServerenterpriseServer=new LBHttpSolrServer("http://115.182.226.165:8008/solr/enterprisenew");
enterpriseServer.setConnectionTimeout(10000000);
updateSolrById(enterpriseServer);
System.out.println("over");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}