solr与java整合使用
1.新建工程,此处示例只新建一个java工程
2.导入jar包,所有jar包都在下载好的solr压缩包中,可直接导入,注意示例图中的jar包路径
3.新建一个class类测试,代码如下
1 package com.kwm.solr.test; 2 3 import org.apache.solr.client.solrj.SolrQuery; 4 import org.apache.solr.client.solrj.impl.HttpSolrServer; 5 import org.apache.solr.client.solrj.response.QueryResponse; 6 import org.apache.solr.common.SolrDocument; 7 import org.apache.solr.common.SolrDocumentList; 8 9 /** 10 * solr测试 11 * @author Ankang 12 * 13 */ 14 public class SolrTest { 15 private final static String url = "http://localhost:8081/solr/"; 16 17 public static void main(String[] args) { 18 SolrTest st = new SolrTest(); 19 try { 20 st.querySolr(); 21 } catch (Exception e) { 22 e.printStackTrace(); 23 } 24 } 25 26 public HttpSolrServer createSolrServer(){ 27 HttpSolrServer hss = new HttpSolrServer(url); 28 return hss; 29 } 30 31 public void querySolr() throws Exception{ 32 HttpSolrServer hss = new HttpSolrServer(url + "collection1/"); 33 SolrQuery sq = new SolrQuery(); 34 sq.set("q", "FileContent:西瓜"); 35 sq.setHighlight(true);//设置高亮 36 QueryResponse response = hss.query(sq); 37 SolrDocumentList sdl = response.getResults(); 38 39 System.out.println("查询结果的总数量"+sdl.getNumFound()); 40 for(SolrDocument sd : sdl){ 41 System.out.println("FileContent:"+sd.get("FileContent")); 42 } 43 } 44 }
solr7.7.2版本后可使用此类进行 solr检索
1 /** 2 * 执行solr查询 3 * @return 4 * @throws Exception 5 */ 6 public JSONObject querySolr() throws Exception{ 7 JSONObject jsonObject = new JSONObject(); 8 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 9 10 HttpSolrClient hss = new HttpSolrClient.Builder(url+"solr0705/").build(); 11 StringBuilder stringBuilder = new StringBuilder(); 12 SolrQuery sq = new SolrQuery(); 13 14 String fc1 = "note:资生堂"; 15 String fc2 = "note:落合因人事变动"; 16 String fc3 = "note:となりました"; 17 18 if(!fc1.equals("")) stringBuilder.append(fc1); 19 if(!fc2.equals("")) stringBuilder.append(" "+fc2); 20 if(!fc3.equals("")) stringBuilder.append(" "+fc3); 21 22 sq.set("q", stringBuilder.toString());//查询条件 23 // sq.set("df", "note"); 24 // sq.set("fq", stringBuilder2.toString());//过滤条件 25 sq.set("defType","edismax"); 26 sq.set("qf","note^10.0"); 27 // sq.set("sort", "note asc"); 28 sq.setStart(0); 29 sq.setRows(30); 30 // sq.set("df", "note"); 31 sq.setHighlight(true);//设置高亮 32 long starttime = System.currentTimeMillis(); 33 QueryResponse response = hss.query(sq); 34 long endtime = System.currentTimeMillis(); 35 System.out.println("执行程序时间======>>>>> "+(endtime-starttime)); 36 System.out.println("查询用时==========>>>>>>> "+response.getQTime()+" ms"); 37 SolrDocumentList sdl = response.getResults(); 38 System.out.println("查询结果的总数量=======>>>>>>>>> "+sdl.getNumFound()+"条"); 39 if(sdl.size() > 0){ 40 for(SolrDocument sd : sdl){ 41 Map<String, Object> map = new HashMap<String, Object>(); 42 43 map.put("mainfilenumber", sd.get("mainfilenumber").toString()); 44 map.put("subfilenumber", sd.get("subfilenumber").toString()); 45 map.put("note", sd.get("note").toString()); 46 47 // map.put("weituorjh", sd.get("weituorjh").toString()); 48 49 list.add(map); 50 } 51 jsonObject.element("result", list); 52 } 53 return jsonObject; 54 }
如果在 q 中的查询条件太长的话,可用此种形式查询并且用配置权重的方式进行检索查询
1 sq.set("q", stringBuilder.toString()); 2 sq.setStart(0); 3 sq.setRows(10); 4 sq.set("df", "weituorjh"); 5 sq.set("defType","edismax"); 6 sq.set("qf","weituorjh^10.0"); 7 sq.setSort("weituorjh", ORDER.desc); 8 sq.setHighlight(true);
如需其他操作,可在代码中直接调用即可
Solr 利用 java 代码进行增删改查操作
1 HttpSolrClient hss = new HttpSolrClient.Builder(url+"solr0705/").build(); 2 StringBuilder stringBuilder = new StringBuilder(); 3 // StringBuilder stringBuilder2 = new StringBuilder(); 4 5 SolrQuery sq = new SolrQuery(); 6 7 //根据ID删除数据 8 hss.deleteById(new ArrayList<String>(){{add("AJ19115075");}}); 9 hss.commit(); 10 11 //添加索引数据 12 SolrInputDocument sid = new SolrInputDocument(); 13 sid.addField("id", "AJ19115099"); 14 sid.addField("weituorjh", "2019109736119"); 15 hss.add(sid); 16 hss.commit();
另附详细参考链接 https://blog.csdn.net/shao_zhiqiang/article/details/51879763