使用SolrJ组件实现简单的创建索引,建立查询功能。
package com.clzhang.sample.solr; import java.io.*; import java.util.*; 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.client.solrj.response.UpdateResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.params.ModifiableSolrParams; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * 此类是测试SolrJ组件使用的类,包括:单条插入索引,多条插入索引,简单查询,复杂查询数据等 * @author Administrator * */ public class SlorJTest { private static final String DEFAULT_URL = "http://localhost:8080/solr/news"; private HttpSolrServer server; @Before public void init() { server = new HttpSolrServer(DEFAULT_URL); server.setMaxRetries(1); // defaults to 0. > 1 not recommended. server.setConnectionTimeout(5000); // 5 seconds to establish TCP // Setting the XML response parser is only required for cross // version compatibility and only when one side is 1.4.1 or // earlier and the other side is 3.1 or later. // server.setParser(new XMLResponseParser()); // binary parser is used // by default // The following settings are provided here for completeness. // They will not normally be required, and should only be used // after consulting javadocs to know whether they are truly required. server.setSoTimeout(1000); // socket read timeout server.setDefaultMaxConnectionsPerHost(100); server.setMaxTotalConnections(100); server.setFollowRedirects(false); // defaults to false // allowCompression defaults to false. // Server side must support gzip or deflate for this to have any effect. server.setAllowCompression(true); // SolrJ lets you upload content in XML and Binary format. // The default is set to be XML. // Use the following to upload using Binary format. // This is the same format which SolrJ uses to fetch results, and can // greatly improve performance as it reduces XML marshalling overhead. // Note -- be sure you have also enabled the // "BinaryUpdateRequestHandler" in your solrconfig.xml for example like: // <requestHandler name="/update/javabin" // class="solr.BinaryUpdateRequestHandler" /> // server.setRequestWriter(new BinaryRequestWriter()); } @After public void destory() { server = null; System.runFinalization(); System.gc(); } /** * 获取查询结果,直接打印输出 * @param query */ public void query(String query) { SolrQuery params = new SolrQuery(query); params.set("rows", 5); try { QueryResponse response = server.query(params); SolrDocumentList list = response.getResults(); System.out.println("总计:" + list.getNumFound() + "条,本批次:" + list.size() + "条"); for (int i = 0; i < list.size(); i++) { SolrDocument doc = list.get(i); System.out.println(doc.get("webTitle")); } } catch (SolrServerException e) { e.printStackTrace(); } } @Test public void addDoc() { // 创建doc文档 SolrInputDocument doc = new SolrInputDocument(); doc.addField("objectId", 0); doc.addField("webTitle", "测试标题"); doc.addField("webTime", new java.util.Date()); doc.addField("webContent", "这是一条测试内容"); try { // 添加一个doc文档 UpdateResponse response = server.add(doc); // commit后才保存到索引库 server.commit(); // 输出统计信息 System.out.println("Query Time:" + response.getQTime()); System.out.println("Elapsed Time:" + response.getElapsedTime()); System.out.println("Status:" + response.getStatus()); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("--------------------------"); query("测试"); System.out.println("--------------------------"); } @Test public void addDocs() { String[] title = new String[] { "IK Analyzer介绍", "观前街介绍", "服务业情况", "人大动态", "高技能" }; String[] content = new String[] { "IK Analyzer是一个结合词典分词和文法分词的中文分词开源工具包。它使用了全新的正向迭代最细粒度切分算法。", "观前街实际上就是玄妙观前面的那条街,卫道观前当然也有一个观,那就是卫道", "服务业集聚区加快建设。全市完成全社会固定资产投资5265亿元,比上年增长17%", "为了提高加快立法质量和实效,市人大常委会还首次开展了立法后评估工作,对《苏州市公路条例》", "继续位居动态全国地级市首位。2012年新增高技能人才7.6万人,其中新培养技师、高级技师4600人" }; Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for (int i = 0; i < title.length; i++) { SolrInputDocument doc = new SolrInputDocument(); doc.addField("objectId", (i + 1)); doc.addField("webTitle", title[i]); doc.addField("webContent", content[i]); doc.addField("webTime", new java.util.Date()); docs.add(doc); } try { UpdateResponse response = server.add(docs); server.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } query("webTitle:全国"); System.out.println("--------------------------"); } @Test public void querySimple() { // 这是一个简单查询 ModifiableSolrParams params = new ModifiableSolrParams(); params.set("q", "加快 开展"); params.set("q.op", "AND"); params.set("start", 0); params.set("rows", 3); params.set("fl", "*,score"); try { QueryResponse response = server.query(params); SolrDocumentList list = response.getResults(); System.out.println("总计:" + list.getNumFound() + "条,本批次:" + list.size() + "条"); for (int i = 0; i < list.size(); i++) { SolrDocument doc = list.get(i); System.out.println(doc.get("webTitle")); } } catch (SolrServerException e) { e.printStackTrace(); } System.out.println("--------------------------"); } @Test public void queryCase() { // 这是一个稍复杂点的查询 SolrQuery params = new SolrQuery("苏州"); // params.set("q.op", "OR"); params.set("start", 0); params.set("rows", 4); // params.set("fl", "*,score"); // params.setIncludeScore(true); // params.set("sort", "webTime desc"); params.setHighlight(true); // 开启高亮组件 params.addHighlightField("webTitle");// 高亮字段 params.addHighlightField("webContent");// 高亮字段 // params.set("hl.useFastVectorHighlighter", "true"); params.set("hl.fragsize", "200"); // params.setHighlightSimplePre("<SPAN class=\"red\">");// 高亮关键字前缀; // params.setHighlightSimplePost("</SPAN>");// 高亮关键字后缀 params.setHighlightSnippets(2); //结果分片数,默认为1 try { QueryResponse response = server.query(params); // 输出查询结果集 SolrDocumentList list = response.getResults(); System.out.println("总计:" + list.getNumFound() + "条,本批次:" + list.size() + "条"); for (int i = 0; i < list.size(); i++) { SolrDocument doc = list.get(i); // System.out.println(doc.get("webTitle")); } // 第一种:常用遍历Map方法; Map<String, Map<String, List<String>>> map = response.getHighlighting(); Iterator<String> iterator = map.keySet().iterator(); while(iterator.hasNext()) { String keyname = (String) iterator.next(); Map<String, List<String>> keyvalue = map.get(keyname); System.out.println("objectId:" + keyname); // 第二种:JDK1.5之后的新遍历Map方法。 for (Map.Entry<String, List<String>> entry : keyvalue.entrySet()) { String subkeyname = entry.getKey().toString(); List<String> subkeyvalue = entry.getValue(); System.out.print(subkeyname + "\n"); for(String str: subkeyvalue) { System.out.print(str); } System.out.println(); } } } catch (SolrServerException e) { e.printStackTrace(); } System.out.println("--------------------------"); } }
Query Time:0
Elapsed Time:94
Status:0
--------------------------
总计:1258条,本批次:5条
车主掀起退车潮 汉兰达爬坡门事件后续(转载)
致福建省人民检察院的一封控诉信
纪检监察干部腐败谁来监督?撕掉广西联通纪检监察主管徐生军虚伪的面具!
官官相护何时了?太原公安局长李亚力仅被建议撤职和留党察看(视频+图)
QQ水浒运营团队伙同其长期托团成员诈骗玩家30万元
--------------------------
总计:668条,本批次:5条
震惊全国的江西省景德镇市国企焦化集团的拆迁贪污腐败大案
震惊全国的江西省景德镇市国企焦化集团的拆迁贪污腐败大案
雷政富揭露“资本家黑社会敲诈”事件,相当典型,全国应当拉网排查,看看有多少资本家通过多少种手段敲诈。
全国聚焦山东省嘉祥县公安局~贪赃枉法案~篡改杀人凶手为证人,性质恶劣
河北三河法院院长丁凤俭全国最牛竟指使法警暴打受害人
--------------------------
总计:484条,本批次:3条
2012年广东6652人因腐败问题受到处分
安徽歙县政府非法强行征地861亩 补偿款太低遭村民反对
记者调查:重庆忠县一桩疑窦重生的受贿案(转载)
--------------------------
总计:1018条,本批次:4条
objectId:5115e5a55b31d04c2db01ff6
webContent
依法追究江苏省吴江市法院审判长刘成文、陪审员朱阿美、吴爱明捏造伪证程序违法裁判案控诉状
objectId:512298ba5b310f5a05a86c35
webContent
重大项目办副主任夏某委托帮助舒润公司协调贷款,具体叫陆育生找一下<em>苏州</em>银行晁行长。
objectId:510fb89d4b3f16b75d2ba042
webContent
刘超英带325名学员在<em>苏州</em>实习期间,应对每位学员发放1500元,实际给每人发放仅有150元,
objectId:5110285e4b3f16b75d2bc42d
webContent
近来看到<em>苏州</em>市司法局就该局原基层处处长王月青涉嫌帮助他人骗保问题相关举报的回复。
webTitle
<em>苏州</em>市司法局为何敢于如此明目张胆指鹿为马?
--------------------------