1. 程式人生 > >solr 實現資料的刪除和修改

solr 實現資料的刪除和修改

修改主方法

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<EnterpriseContentBean> contentList = qr.getBeans(EnterpriseContentBean.class);
// 設定需要儲存的文章資訊
for (EnterpriseContentBean bean : contentList) {
bean.setEnterpriseId(enterpriseId);
bean.setEnterpriseName(enterpriseName);
List<String> contents = new ArrayList<String>();
contents.add(content);
bean.setContent(contents);
bean.setPubDate(pubDate);
System.out.println("pubDate======>" + pubDate);
List<String> lableTypes = Arrays.asList(lableType.split(","));


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<SolrInputDocument>)objs);
} 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<String> stringIdList = new ArrayList<>(ids.length);
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 <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!");
}

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 <T> SolrQueryResult<T> queryAndGetSolrQueryResult(String solrUrl, SolrQuery query, Class<T> returnClass) throws Exception {
SolrQueryResult<T> result = new SolrQueryResult<T>();

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<T> resultList = resp.getBeans(returnClass);
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 <T> SolrQueryResult<T> queryAndGetSolrQueryResult(LBHttpSolrServer solr, SolrQuery query, Class<T> returnClass) throws Exception {
SolrQueryResult<T> result = new SolrQueryResult<T>();

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<T> resultList = resp.getBeans(returnClass);
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<EnterpriseContentBean> contentList = qr.getBeans(EnterpriseContentBean.class);
//設定需要儲存的文章資訊
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();
}

}
}