首页 > 代码库 > 3.solr学习速成之索引添加 查询 删除
3.solr学习速成之索引添加 查询 删除
solrserver.java
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; } }
solrTest.java
public class solrTest { 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(); } }
3.solr学习速成之索引添加 查询 删除
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。