https://github.com/cihm/GradleAndSolr
SolrSearch.java
package com.job; import java.net.MalformedURLException; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; 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 org.apache.solr.common.SolrDocumentList; import com.vo.SolrArgVo; public class SolrSearch { public static Logger log = Logger.getLogger(SolrSearch.class.getName()); public boolean solrSearch(SolrArgVo solrArgVo) throws MalformedURLException, SolrServerException{ //HttpSolrServer solr = new HttpSolrServer("http://10.24.100.237:8080/solr/collection1"); HttpSolrServer solr = new HttpSolrServer("http://192.168.22.148:8080/solr/collection1"); SolrQuery query = new SolrQuery(); //英文是精確比對,中文是模糊比對 //查詢條件 query.setQuery( "*:*" ); //query.setFilterQueries("name:" + "登基", "description:" + "登基", "channel:" + "ylib"); String keyword = solrArgVo.getKetWord(); String channel = solrArgVo.getChanneCode(); query.addFilterQuery("channel:" + channel); query.setQuery("name:"+keyword+" OR description:"+keyword); //query.setQuery("name:"+"王道 AND currency:"+"NTD"); //can use to be get book by cat query.setRows(100); //get row of query result//default is 10 //query.setQuery("title:國王"); //query.setQuery("title: art"); QueryResponse response = solr.query(query); SolrDocumentList results = response.getResults(); System.out.println("NumFound="+results.getNumFound()); System.out.println("SIZE="+results.size()); //System.out.println(results); //System.out.println(results.get(0).get("title")); for (int i = 0; i < results.size(); ++i) { System.out.println("result "+i+"= "+results.get(i).get("id")); System.out.println("result "+i+"="+ results.get(i)); } return true; } }
SolrBuildIndex.java
package com.job; import java.io.IOException; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.BinaryRequestWriter; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.common.SolrInputDocument; import com.util.DataUtil; import com.util.SolrConstants; public class SolrBuildIndex { public static Logger log = Logger.getLogger(SolrBuildIndex.class.getName()); public boolean solrBuildIndex() throws MalformedURLException, SolrServerException,IOException{ HttpSolrServer server = new HttpSolrServer("http://192.168.22.148:8080/solr/collection1"); // 清空之前建立的索引數據 // delete all doc server.deleteByQuery( "*:*" ); //提升性能 server.setRequestWriter(new BinaryRequestWriter()); String datFilePath = "C:/Users/1409035/Desktop/FTP_server_backup/candelete/"; //String datFilePath = PropertyLoader.getInstance().getValue(DAT_FILE_PATH_KEY); Map zinioMap= (HashMap) DataUtil.DeSerialization(datFilePath + "Zinio" + "-" + "getItemListByCategoryCode" + "-using" + ".dat"); Map ylibMap =(HashMap) DataUtil.DeSerialization(datFilePath + "Ylib" + "-" + "getItemListByCategoryCode" + "-using" + ".dat"); Map ibobarMap =(HashMap) DataUtil.DeSerialization(datFilePath + "Ibobar" + "-" + "getItemListByCategoryCode" + "-using" + ".dat"); Map linkingMap =(HashMap) DataUtil.DeSerialization(datFilePath + "Linking" + "-" + "getItemListByCategoryCode" + "-using" + ".dat"); Collectiondocs2 = new ArrayList (); int k=0; Iterator linkingIter = linkingMap.entrySet().iterator(); while (linkingIter.hasNext()) { Map.Entry entry = (Map.Entry) linkingIter.next(); ArrayList > bookAL = (ArrayList >) entry .getValue(); for (HashMap hm : bookAL) { k++; SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", k); doc.addField("channel", "Linking"); doc.addField("name", hm.get("name")); doc.addField("description", hm.get("description")); docs2.add(doc); } } Iterator ylibIter = ylibMap.entrySet().iterator(); while (ylibIter.hasNext()) { Map.Entry entry = (Map.Entry) ylibIter.next(); ArrayList > bookAL = (ArrayList >) entry .getValue(); for (HashMap hm : bookAL) { k++; SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", k); doc.addField("channel", "Ylib"); doc.addField("name", hm.get("name")); doc.addField("description", hm.get("description")); System.out.println(doc.toString()); docs2.add(doc); } } Iterator zinioIter = zinioMap.entrySet().iterator(); while(zinioIter.hasNext()) { Map.Entry entry = (Map.Entry) zinioIter.next(); //下述不能用ALHM 去接,否則會拋出ArrayList can't be cast to ALHM的錯誤 ArrayList > bookAL=(ArrayList >)entry.getValue(); for(HashMap hm:bookAL){ k++; System.out.println(entry.getKey()+" "+hm.get("Title")); SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", k); doc.addField("channel", "Zinio"); doc.addField("name", hm.get("name")); doc.addField("description", hm.get("description")); docs2.add(doc); } } Iterator ibobarIter = ibobarMap.entrySet().iterator(); while(ibobarIter.hasNext()) { Map.Entry entry = (Map.Entry) ibobarIter.next(); ArrayList > bookAL=(ArrayList >)entry.getValue(); for(HashMap hm:bookAL){ k++; SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", k); doc.addField("channel", "Ibobar"); doc.addField("name", hm.get("name")); doc.addField("description", hm.get("description")); docs2.add(doc); } } //將ArrayList轉為XML格式 //String resultList=GeneralXmlPullParser.reverse(contentAL); System.out.println("======="); //System.out.println(docs2.toString()); server.add(docs2); server.commit(); server.optimize(true, true); System.out.println("finish"); return true; } }
沒有留言:
張貼留言