单机solrJ不需要占用太多的服务器和资源,本机使用solr-6.3.0,也不需要配置tomcat.
一、新建一个java工程需要依赖的jar包如下:
solr-solrj-6.3.0.jar; commons-lang-2.5.jar ; log4j-1.2.17.jar; slf4j-api-1.7.7.jar; slf4j-log4j12-1.7.6.jar;httpclient-4.4.1.jar; httpcore-4.4.1.jar; httpmime-4.4.1.jar
(解析json常用的包:commons-logging-1.1.1.jar;commons-beanutils-1.7.jar;commons-collections.jar; noggit-0.5.jar; ezmorph.jar; json-lib-2.2.2-jdk15.jar;)
二、文档操作(增加,删除,查询)的代码如下:
1.首先创建一个对象,对象只有一些属性值,没有成员方法。
1 package test; 2 3 public class LawTest { 4 // String fields[]={"guid","lawName","itemIndex","itemTitle","itemContent","categoryTitle","categoryIndex"}; 5 private String guid; 6 private String lawName; 7 private int itemIndex; 8 private String itemTitle; 9 private String itemContent; 10 private String categoryTitle; 11 private int categoryIndex; 12 public LawTest(){ 13 this.guid=""; 14 this.lawName=""; 15 this.itemIndex=0; 16 this.itemTitle=""; 17 this.itemContent=""; 18 this.categoryTitle=""; 19 this.categoryIndex=0; 20 } 21 public LawTest(String guid,String lawName,int itemIndex,String itemTitle,String itemContent,String categoryTitle,int categoryIndex){ 22 this.guid=guid; 23 this.lawName=lawName; 24 this.itemIndex=itemIndex; 25 this.itemTitle=itemTitle; 26 this.itemContent=itemContent; 27 this.categoryTitle=categoryTitle; 28 this.categoryIndex=categoryIndex; 29 } 30 public String getGuid() { 31 return guid; 32 } 33 public void setGuid(String guid) { 34 this.guid = guid; 35 } 36 public String getLawName() { 37 return lawName; 38 } 39 public void setLawName(String lawName) { 40 this.lawName = lawName; 41 } 42 public int getItemIndex() { 43 return itemIndex; 44 } 45 public void setItemIndex(int itemIndex) { 46 this.itemIndex = itemIndex; 47 } 48 public String getItemTitle() { 49 return itemTitle; 50 } 51 public void setItemTitle(String itemTitle) { 52 this.itemTitle = itemTitle; 53 } 54 public String getItemContent() { 55 return itemContent; 56 } 57 public void setItemContent(String itemContent) { 58 this.itemContent = itemContent; 59 } 60 public String getCategoryTitle() { 61 return categoryTitle; 62 } 63 public void setCategoryTitle(String categoryTitle) { 64 this.categoryTitle = categoryTitle; 65 } 66 public int getCategoryIndex() { 67 return categoryIndex; 68 } 69 public void setCategoryIndex(int categoryIndex) { 70 this.categoryIndex = categoryIndex; 71 } 72 73 74 }
2.有了对象后,开始构建我们的客户端代码:首先初始化连接
1 private static String serverUrl="http://localhost:8983/solr/collectionTest"; 2 private static String fields[]={"guid","lawName","itemIndex","itemTitle","itemContent","categoryTitle","categoryIndex"}; 3 private static SolrClient solrclient = null; 4 private static SolrQuery query=new SolrQuery("*:*"); 5 /** 6 * 初始化连接 7 */ 8 @SuppressWarnings("deprecation") 9 public static void initiate(){ 10 solrclient = new HttpSolrClient(serverUrl); 11 }
3. 然后需要将已有的入库json文本转化为对象,再根据对象添加到solr中
1 /** 2 * 输入文档存放地址,读取为字符串 3 * 将字符串解析为json并转换为对象 4 * @param DirectoryPath 5 * @return 6 */ 7 public static List<LawTest> readTxtFile(String DirectoryPath){ 8 List<LawTest> list = new ArrayList<LawTest>(); 9 try { 10 String encoding="GB2312";//UTF-8 11 String guid=null; 12 String lawName=null; 13 int itemIndex=0; 14 String itemTitle=null; 15 String itemContent=null; 16 String categoryTitle=null; 17 int categoryIndex=0; 18 File file=new File(DirectoryPath); 19 File[] tempList = file.listFiles(); 20 System.out.println("该目录下对象个数:"+tempList.length); 21 for (int i = 0; i < tempList.length; i++) { 22 if (tempList[i].isFile() && tempList[i].exists()) { 23 //保存每一个文本为字符串 24 StringBuilder stringAll=new StringBuilder(); 25 System.out.println("文 件:"+i+":"+tempList[i]); 26 InputStreamReader read = new InputStreamReader( 27 new FileInputStream(tempList[i]),encoding);//考虑到编码格式 28 BufferedReader bufferedReader = new BufferedReader(read); 29 String lineTxt = null; 30 while((lineTxt = bufferedReader.readLine()) != null){ 31 System.out.println(lineTxt); 32 stringAll.append(lineTxt); 33 } 34 String text=stringAll.toString(); 35 JSONArray arry = JSONArray.fromObject(text); 36 System.out.println("json字符串内容如下"); 37 System.out.println(arry); 38 //List<Map<String, String>> rsList = new ArrayList<Map<String, String>>(); 39 for (int j = 0; j < arry.size(); j++) 40 { 41 JSONObject jsonObject = arry.getJSONObject(j); 42 //Map<String, String> map = new HashMap<String, String>(); 43 for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();) 44 { 45 String key = (String) iter.next(); 46 String value = jsonObject.get(key).toString(); 47 // map.put(key, value); 48 if("guid".equals(key)){ guid=value; } 49 else if("lawName".equals(key)){ lawName=value;} 50 else if("itemIndex".equals(key)){ itemIndex=Integer.valueOf(value);} 51 else if("itemTitle".equals(key)){ itemTitle=value;} 52 else if("itemContent".equals(key)){ itemContent=value;} 53 else if("categoryIndex".equals(key)){ categoryIndex=Integer.valueOf(value);} 54 else if("categoryTitle".equals(key)){ categoryTitle=value;} 55 56 } 57 LawTest lawlistbean=new LawTest( guid, lawName, itemIndex, itemTitle, itemContent, categoryTitle, categoryIndex); 58 list.add(lawlistbean); 59 //rsList.add(map); 60 } 61 } 62 } 63 } 64 catch (Exception e) { 65 System.out.println("读取文件内容出错"); 66 e.printStackTrace(); 67 } 68 return list; 69 70 } 71
4.添加文档:
1 /** 2 * 添加文档 3 * @param DocList 4 */ 5 public static void addDocs(List<LawTest> DocList) { 6 try { 7 for (int i = 0; i<DocList.size();i++){ 8 SolrInputDocument doc1=new SolrInputDocument(); 9 doc1.setField("guid", DocList.get(i).getGuid()); 10 doc1.setField("lawName", DocList.get(i).getLawName()); 11 doc1.setField("itemIndex", DocList.get(i).getItemIndex()); 12 doc1.setField("itemTitle", DocList.get(i).getItemTitle()); 13 doc1.setField("itemContent", DocList.get(i).getItemContent()); 14 doc1.setField("categoryIndex", DocList.get(i).getCategoryIndex()); 15 doc1.setField("categoryTitle", DocList.get(i).getItemTitle()); 16 solrclient.add(doc1); 17 } 18 }catch (Exception e) { 19 e.printStackTrace(); 20 } finally { 21 try { 22 solrclient.commit(); 23 } catch (Exception e) { 24 e.printStackTrace(); 25 } 26 } 27 }
5.删除文档;
1 /** 2 * 根据索引删除文档 3 * @param query 4 */ 5 public void delDoc(String query){ 6 try { 7 solrclient.deleteByQuery(query); 8 solrclient.commit(); 9 } catch (Exception e) { 10 e.printStackTrace(); 11 } 12 13 }
6.查询文档;
/** * 查询文档 * @param query */ public static List<Map<String,Object>> queryDoc(SolrQuery query, String[] fields){ if(query==null || fields==null){ return null; } else{ QueryResponse resp3 = null; query.setStart(0); query.setRows(10); query.setFields(fields); try { resp3=solrclient.query(query); } catch (SolrServerException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } SolrDocumentList solrList = resp3.getResults(); List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); for (SolrDocument doc : solrList) { Map<String, Object> resultMap=new HashMap<String, Object>(); for (String field : fields){ resultMap.put(field,doc.get(field)); } result.add(resultMap); } return result; } } //查询文档入口 public static List<Map<String,Object>> retrieveContent(Map<String, Object> args){ //关键词 String keywords = (String) args.remove("keyword"); if (!(keywords == null || "".equals(keywords))) { query.setQuery(keywords); } //其他查询过滤条件 for (Entry<String, Object> entry : args.entrySet()) { query.addFilterQuery(entry.getKey() + ":" + entry.getValue()); } return queryDoc(query,fields); }
7.获取文档数量:
1 public static long getCount(){ 2 long numFound = 0; 3 SolrQuery q = query.getCopy(); 4 q.setStart(0); 5 q.setRows(0); 6 try { 7 //没必要排序 8 q.remove("sort"); 9 if ((query.getBool("group", false))) { 10 q.setParam("group", false); 11 q.setParam("group.ngroups", false); 12 String field = query.get("group.field"); 13 q.set("json.facet", "{distinctCount:'unique(" + field + ")'}"); 14 } 15 QueryResponse results = solrclient.query(q,METHOD.POST); 16 if ((query.getBool("group", false))) { 17 Object facets = results.getResponse().get("facets"); 18 if(facets != null && facets instanceof SimpleOrderedMap){ 19 Object distinctCount = ((SimpleOrderedMap)facets).get("distinctCount"); 20 if(distinctCount != null ){ 21 numFound = (Long)distinctCount; 22 } 23 } 24 } else { 25 numFound = results.getResults().getNumFound(); 26 } 27 } catch (SolrServerException e) { 28 e.printStackTrace(); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 return numFound; 33 }
8.测试主函数:
1 public static void main(String [] args) throws SolrServerException, IOException{ 2 String path="D:\文件\solr入库测试文件"; 3 Map<String, Object> map=new HashMap<String, Object>(); 4 //初始化 5 initiate(); 6 //添加文档测试 7 //addDocs(readTxtFile(path)); 8 //查询 9 map.put("keyword", "标题"); 10 List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); 11 result=retrieveContent(map); 12 if(result==null || result.size()==0){ 13 System.out.println("data is null"); 14 } 15 else{ 16 System.out.println("-----输出查询结果如下:----- "); 17 result.forEach( mp->{ 18 for(String key: mp.keySet()){ 19 System.out.println(key+":"+mp.get(key).toString()); 20 } 21 22 }); 23 } 24 System.out.println("query count:"+getCount()); 25 System.out.println("query over!"); 26 27 }