常用工具類--solr索引新增 查詢 刪除
阿新 • • 發佈:2018-12-11
solr常用操作
首先是連線solr伺服器:
新增solr索引public class solrServer { private solrServer(){}; final static String SOLR_URL = "http://localhost:8080/solr/test"; private static HttpSolrClient server = null; public static HttpSolrClient getServer(){ if(server == null){ server = new HttpSolrClient(SOLR_URL); server.setDefaultMaxConnectionsPerHost(1000); server.setMaxTotalConnections(10000); server.setConnectionTimeout(60000); server.setSoTimeout(60000); server.setFollowRedirects(false); server.setAllowCompression(true); } return server; } }
刪除solr索引public static void addIndex(){ HttpSolrClient server = solrServer.getServer(); SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", "1003"); doc.addField("name_s", "wanglc3"); doc.addField("score_i", "883"); try { server.add(doc); server.commit(); } catch (SolrServerException | IOException e) { e.printStackTrace(); } }
public static void delete(){
HttpSolrClient server = solrServer.getServer();
try {
server.deleteById("1001");
server.commit();
} catch (SolrServerException | IOException e) {
e.printStackTrace();
}
}
查詢搜尋
public static void search(){ HttpSolrClient server = solrServer.getServer(); SolrQuery query = new SolrQuery(); query.setQuery("*:*"); query.setStart(0); query.setRows(5); QueryResponse queryResponse; try { queryResponse = server.query(query); SolrDocumentList list = queryResponse.getResults(); System.out.println("num = "+list.getNumFound()); for(int i=0;i<list.size();i++){ System.out.println(list.get(i).getFieldValue("name_s")); } } catch (Exception e) { e.printStackTrace(); } }
測試:
public static void main(String[] args) {
//addIndex();
//delete();
search();
}