标签:text 标记 结果 XML ges 努力 ssi 中文分词 final
1 public class SolrServer { 2 private static SolrServer solrServer = null; 3 private static HttpSolrServer server=null; 4 private static String url="http://solrIP:8080/solr"; 5 6 public static synchronized SolrServer getInstance() { 7 if (solrServer==null){ 8 solrServer=new SolrServer(); 9 } 10 return solrServer; 11 } 12 public static HttpSolrServer getServer(){ 13 try { 14 if(server==null){ 15 server = new HttpSolrServer(url); 16 server.setSoTimeout(1000); // socket read timeout 17 server.setConnectionTimeout(1000); 18 server.setDefaultMaxConnectionsPerHost(100); 19 server.setMaxTotalConnections(100); 20 server.setFollowRedirects(false); // defaults to false 21 //allowCompression defaults to false. 22 //Server side must support gzip or deflate for this to have any effect. 23 server.setAllowCompression(true); 24 server.setMaxRetries(1); // defaults to 0. > 1 not recommended. 25 } 26 } catch (Exception e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 return server; 31 } 32 }
1 @Entity 2 @Table(name="blogs") 3 public class BlogsDO implements Serializable{ 4 private static final long serialVersionUID = -4721368786493126226L; 5 @Field("Id") 6 private String id; 7 @Field("content") 8 private String content; 9 @Field("title") 10 private String title; 11 @Field("url") 12 private String url; 13 @Field("_version_") 14 private String _version_; 15 public String getId() { 16 return id; 17 } 18 public void setId(String id) { 19 this.id = id; 20 } 21 public String getContent() { 22 return content; 23 } 24 public void setContent(String content) { 25 this.content = content; 26 } 27 public String getTitle() { 28 return title; 29 } 30 public void setTitle(String title) { 31 this.title = title; 32 } 33 public String getUrl() { 34 return url; 35 } 36 public void setUrl(String url) { 37 this.url = url; 38 } 39 public String get_version_() { 40 return _version_; 41 } 42 public void set_version_(String version) { 43 _version_ = version; 44 } 45 public static long getSerialversionuid() { 46 return serialVersionUID; 47 } 48 }
1 String searchWord=request.getParameter("searchText"); 2 List<BlogsDO> blogList=new ArrayList<BlogsDO>(); 3 BlogsDO blogsDO=null; 4 HttpSolrServer solrServer= SolrServer.getInstance().getServer(); 5 SolrQuery sQuery = new SolrQuery(); 6 String para=""; 7 Page page=null; 8 para="content:"+searchWord+""; 9 10 logger.info("para:"+para); 11 sQuery.setQuery(para); 12 sQuery.setStart(0); 13 sQuery.setRows(10); 14 //设置高亮 15 sQuery.setHighlight(true); // 开启高亮组件 16 sQuery.addHighlightField("content");// 高亮字段 17 sQuery.addHighlightField("title");// 高亮字段 18 sQuery.setHighlightSimplePre("<font color=‘red‘>");//标记,高亮关键字前缀 19 sQuery.setHighlightSimplePost("</font>");//后缀 20 sQuery.setHighlightSnippets(2);//结果分片数,默认为1 21 sQuery.setHighlightFragsize(1000);//每个分片的最大长度,默认为100 22 23 //分片信息 24 sQuery.setFacet(true) 25 .setFacetMinCount(1) 26 .setFacetLimit(5)//段 27 .addFacetField("content");//分片字段 28 29 long startSearch=System.currentTimeMillis(); 30 Integer counts=0; 31 try { 32 QueryResponse response = solrServer.query(sQuery); 33 SolrDocumentList list = response.getResults(); 34 counts=(int) list.getNumFound();//搜索数量 35 logger.info("counts:"+counts); 36 //获取所有高亮的字段 37 Map<String,Map<String,List<String>>> highlightMap=response.getHighlighting(); 38 String blogId=""; 39 for (SolrDocument solrDocument : list) { 40 blogsDO=new BlogsDO(); 41 blogId=(String) solrDocument.getFieldValue("id").toString(); 42 blogsDO.setId(blogId); 43 blogsDO.set_version_(solrDocument.getFieldValue("_version_").toString()); 44 blogsDO.setUrl(solrDocument.getFieldValue("url").toString()); 45 46 List<String> titleList=highlightMap.get(blogId).get("title"); 47 List<String> contentList=highlightMap.get(blogId).get("content"); 48 if(titleList!=null && titleList.size()>0){ 49 blogsDO.setTitle(titleList.get(0)); 50 }else{ 51 //获取并设置高亮的字段title 52 blogsDO.setTitle(solrDocument.getFieldValue("title").toString()); 53 } 54 if(contentList!=null && contentList.size()>0){ 55 blogsDO.setContent(contentList.get(0)); 56 }else{ 57 //获取并设置高亮的字段content 58 blogsDO.setContent(solrDocument.getFieldValue("content").toString()); 59 } 60 blogList.add(blogsDO); 61 } 62 } catch (SolrServerException e) { 63 e.printStackTrace(); 64 } 65 long endSearch=System.currentTimeMillis(); 66 model.addObject("time", (double)(endSearch-startSearch)/1000); 67 model.addObject("counts", counts); 68 model.addObject("blogList", blogList);
友情赞助
如果你觉得博主的文章对你那么一点小帮助,恰巧你又有想打赏博主的小冲动,那么事不宜迟,赶紧扫一扫,小额地赞助下,攒个奶粉钱,也是让博主有动力继续努力,写出更好的文章^^。
1. 支付宝 2. 微信
标签:text 标记 结果 XML ges 努力 ssi 中文分词 final
原文地址:http://www.cnblogs.com/yujihaia/p/7468297.html