solr工具类,
package com.ecai.utils;import java.io.IOException;
import java.util.ArrayList;
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.response.QueryResponse;
import com.ecai.bean.Product;
/**
*
* 缂佺媴鎷烽柛妤佹礈閸屻劌顕i弴鐔告儥濞达綇鎷�
* @author tf
*
*/
public class SimpleQuery {
private HttpSolrServer solrServer = new SolrServer().getSolrServer();
/**
* 闁圭瑳鍡╂斀闁哄被鍎撮妤冩嫚椤撶偛缍�
* @param query
* @throws IOException
*/
public List<Product> query(String query) throws IOException{
SolrQuery solrQuery = new SolrQuery();
List<Product> itemList = new ArrayList<Product>();
if(query != null && !"".equals(query)){
solrQuery.setQuery(query);
}
QueryResponse rsp = null;
try {
rsp = solrServer.query(solrQuery);
itemList= rsp.getBeans(Product.class); // 闁哄秷顫夊畵渚�鎳涢鍕毎濞戞柨顦辩悮顐﹀垂鐎n厾绠查柛銉у仧缁劑寮搁敓锟�
/*SolrDocumentList results = rsp.getResults();
for (SolrDocument solrDocument : results) {
System.out.println(solrDocument.getFieldValue("name")+"....."+solrDocument.getFieldValue("age"));
}*/
} catch (SolrServerException e) {
e.printStackTrace();
}
return itemList;
}
/**
* 婵烇綀顕ф慨鐐垫閵忕姷绌块柡浣哄瀹擄拷
* @param doc
*/
/*public void addIndexDoc(SchoolSolr bean){
try {
solrServer.addBean(bean);
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}*/
/**
* 闁归潧缍婇崳鍝勄庣拠鎻掝潱缂佷究鍨圭槐鈺呭极閻楀牆绁�
* @param docs
*/
/*public void addIndexDoc(Collection<SchoolSolr> beans){
try {
solrServer.addBeans(beans);
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}*/
/**
* 闁哄秷顫夊畵渚�寮堕垾鍙夘偨闁告帞濞�濞呭海妲愰姀鐘电┛闁轰胶澧楀畵锟�
* @param query
*/
public void deleteByQuery(String query){
try {
solrServer.deleteByQuery(query);
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 婵炴挸鎳愰埞鏍閵忕姷绌块幖瀛樻尰閺嗙喖骞戦敓锟�
*/
public void deleteAll(){
try {
solrServer.deleteByQuery("*:*");
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 缂佷究鍨圭槐鈺呭极閻楀牆绁﹀ù鍏济�碉拷
*/
public void optimize(){
try {
solrServer.optimize();
solrServer.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SimpleQuery sq = new SimpleQuery();
sq.deleteAll();
}
}
package com.ecai.utils;
import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
/**
*
* 缁便垹绱╅張宥呭閸忋儱褰�
* @author tf
*
*/
public class SolrServer {
private static final String SOLR_URL = "http://localhost:8080/solr/core";//LoadProperties.getValue("solrIndex"); //"http://localhost:8080/solr";
private HttpSolrServer solrServer = null;
private ConcurrentUpdateSolrServer updateSolrServer = null;
// 閻€劋绨崥鍕潚鐎靛湱鍌ㄥ鏇犳畱閹垮秳缍�,鐎佃鍧婇崝鐘虫纯娴硷拷
public HttpSolrServer getSolrServer(){
if(solrServer == null)
solrServer = new SolrServer().solrServer;
return solrServer;
}
// 閻€劋绨弴瀛樻煀缁便垹绱�
public ConcurrentUpdateSolrServer getUpdateSolrServer(){
if(updateSolrServer == null)
updateSolrServer = new SolrServer(true).updateSolrServer;
return updateSolrServer;
}
public SolrServer(){
solrServer = new HttpSolrServer(SOLR_URL); // 閼惧嘲褰囩槐銏犵穿閺堝秴濮�
solrServer.setDefaultMaxConnectionsPerHost(100); // 鐠佸墽鐤嗛崡鏇氱閺堝搫娅掗崷銊х舶鐎规碍妞傞梻鏉戝敶 閸欘垵鎻崚鎵畱閺堬拷婢堆呮畱鏉╃偞甯撮弫浼村櫤 閸欏倹鏆�:閺冨爼妫�
solrServer.setMaxTotalConnections(100); // 閺堬拷婢堆嗙箾閹恒儲鏆�
solrServer.setSoTimeout(10000); // socket鐠囪褰囩搾鍛閺冨爼妫�
solrServer.setConnectionTimeout(5000); // 鐠佸墽鐤嗘潻鐐村复鐡掑懏妞傞弮鍫曟?
}
public SolrServer(boolean bUpdateOper){
updateSolrServer = new ConcurrentUpdateSolrServer(SOLR_URL, 100, 3); // 閼惧嘲褰囩槐銏犵穿閺堝秴濮�
updateSolrServer.setSoTimeout(10000); // socket鐠囪褰囩搾鍛閺冨爼妫�
updateSolrServer.setConnectionTimeout(5000); // 鐠佸墽鐤嗘潻鐐村复鐡掑懏妞傞弮鍫曟?
}
public static void main(){
}
}
//SearchServiceImpl
@Override
public List<Product> searchBykeywords(String keywords) throws Exception {
SimpleQuery sq = new SimpleQuery();
String query = "product_name:"+keywords;
List<Product> list = sq.query(query);
return list;
}