1、创建SolrServer类
SolrServer类:提供与Solr实例的连接与通信。
往Solr里添加索引数据,据说有好几种办法,这边利用SolrJ操作solr API完成index操作,具体SolrJ是个什么东西,有多么强大的功能,日后在消灭你,今儿放你一马。
简单来说就是配置SolrServer的一些信息,比如solr的服务器地址,超时时间等
以下是我的配置,把这些个信息封装在一个SolrServer类中
import org.apache.solr.client.solrj.impl.HttpSolrServer; ......
...... public class SolrServer { private static SolrServer solrServer = null; private static HttpSolrServer server = null; public static synchronized SolrServer getInstance() { if (solrServer == null) { solrServer = new SolrServer(); } return solrServer; } public static HttpSolrServer getServer() { server = new HttpSolrServer(SysConstants.SOLR_SERVER); server.setSoTimeout(20000); // socket read timeout server.setConnectionTimeout(20000); server.setDefaultMaxConnectionsPerHost(100); server.setMaxTotalConnections(100); server.setFollowRedirects(false); // defaults to false server.setAllowCompression(true); server.setMaxRetries(1); // defaults to 0. > 1 not recommended. return server; } }
SysConstants.SOLR_SERVER=http://localhost:8080/solr/collection1
2、添加索引数据
以下是我的配置,传入的参数是一个List,这样就可以添加SolrInputDocument集合了
public static int newsIndexToSolr(List<News> newsList){ HttpSolrServer solrServer = SolrServer.getInstance().getServer(); List<SolrInputDocument> docsList = new ArrayList<SolrInputDocument>(); int iReturn = -1; for(News news:newsList) { SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", news.getId()); String title = SolrUtil.rmHtmlTag(news.getTitle()); String content = SolrUtil.rmHtmlTag(news.getContent()); doc.addField("title", title); doc.addField("content", content); doc.addField("mediaId", news.getMediaId()); doc.addField("pointCode", news.getPointCode()); Date publishTime = news.getPublishTime(); doc.addField("publishTime", publishTime.getTime()); ...... ...... docsList.add(doc); } try { solrServer.add(docsList); UpdateResponse response = solrServer.commit(); iReturn = response.getStatus(); } catch (SolrServerException e) { e.printStackTrace(); iReturn = -1; } catch (IOException e) { e.printStackTrace(); iReturn = -1; }finally{ solrServer.shutdown(); solrServer = null; } return iReturn; }
OK!collection1中已经有数据了,接下来就是各种玩了,上张图呗,不然全篇都是文字,效果不好
3、搜索结果说明
<?xml version="1.0" encoding="UTF-8"?> <response> <lst name="responseHeader"> <int name="status">0</int> <int name="QTime">1</int>//<!--查询耗时(毫秒)Solr有一些缓存(保存过去的搜索结果),提高了搜索效率--> <lst name="params">//<!--查询的参数--> <str name="indent">true</str>//<!--是否缩进XML文件--> <str name="q">*:*</str> <str name="_">1389874266090</str>//<!--版本信息--> <str name="wt">xml</str> <str name="rows">100</str>//<!--返回的结果条数--> </lst> </lst> //<!--numFound是找到几条,start从第几条开始显示--> //<!--这里并没有显示得分情况(非full interface),但结果其实已经按照得分排序了(Solr默认)--> //<!--如果是full interface查询,result会包括maxScore属性,标识最高得分--> <result name="response" numFound="20004" start="0"> <doc> <str name="id">change.me</str> <str name="title">change.me</str> <long name="_version_">1457195667361366016</long></doc> //<!--默认情况Solr会列出所有存储的fields //(不是所有field都需要存储,虽然可能根据它来查索引,但不用包含在就结果中) // --> // <!-- //注意,某些field是多值的,由arr标签标记的 // --> <doc> <str name="id">123456789</str> <str name="title">标题测试啦</str> <str name="content">内容测试啦我勒个去</str> <long name="_version_">1457297718869229568</long></doc> <doc> <str name="id">123456789111</str> <str name="title">标题测试啦111</str> <str name="content">内容测试啦我勒个去111</str> <long name="_version_">1457297750709239808</long></doc> <doc> <str name="id">22222222</str> <str name="title">正常数据结构一个是需要考虑中文分词</str> <str name="content">不存在不需要索引、也不需要分词,也不需要存储的字段,因为这样的字段在Lucene中无意义。</str> <long name="_version_">1457297868749537280</long></doc> <doc> <str name="id">20130416092253920700002</str> <long name="_version_">1457378992085532672</long></doc> <doc> <str name="id">20130416092255050701069</str> <long name="_version_">1457378992088678400</long></doc> <doc> <str name="id">20130416092837610701043</str> <long name="_version_">1457378992089726976</long></doc> <doc> <str name="id">20130412103434210700066</str> <long name="_version_">1457378992091824128</long></doc> <doc> <str name="id">20130412103435370700004</str> <long name="_version_">1457378992093921280</long></doc> <doc> <str name="id">20130412103436450701083</str> <long name="_version_">1457378992096018432</long></doc> </result> </response>
在路上……