solr的增删改查和高亮以及分组

来源:互联网 发布:孩子讲故事录音软件 编辑:程序博客网 时间:2024/05/16 05:19

solr详细学习资料可参考博客:http://www.cnblogs.com/HD/category/613196.html

代码如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.hj.solr;  
  2.   
  3. import org.apache.solr.client.solrj.beans.Field;  
  4.   
  5. /** 
  6.  * 在变量的set方法上注解上lucene内部的字段名称 
  7.  */  
  8. public class Message {  
  9.     private String id;  
  10.     private String title;  
  11.     private String content[];  
  12.       
  13.       
  14.     public Message() {  
  15.         super();  
  16.     }  
  17.   
  18.     public Message(String id, String title, String[] content) {  
  19.         super();  
  20.         this.id = id;  
  21.         this.title = title;  
  22.         this.content = content;  
  23.     }  
  24.   
  25.     public String getId() {  
  26.         return id;  
  27.     }  
  28.       
  29.     @Field  
  30.     public void setId(String id) {  
  31.         this.id = id;  
  32.     }  
  33.     public String getTitle() {  
  34.         return title;  
  35.     }  
  36.       
  37.     @Field("msg_title")  
  38.     public void setTitle(String title) {  
  39.         this.title = title;  
  40.     }  
  41.     public String[] getContent() {  
  42.         return content;  
  43.     }  
  44.       
  45.     @Field("msg_content")  
  46.     public void setContent(String[] content) {  
  47.         this.content = content;  
  48.     }  
  49. }  

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.hj.solr;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.MalformedURLException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import org.apache.solr.client.solrj.SolrQuery;  
  9. import org.apache.solr.client.solrj.SolrServerException;  
  10. import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;  
  11. import org.apache.solr.client.solrj.response.FacetField.Count;  
  12. import org.apache.solr.client.solrj.response.QueryResponse;  
  13. import org.apache.solr.common.SolrDocument;  
  14. import org.apache.solr.common.SolrDocumentList;  
  15. import org.apache.solr.common.SolrInputDocument;  
  16. import org.junit.Test;  
  17.   
  18. /** 
  19.  * @date 2013年12月4日 
  20.  * @author huangjie 
  21.  */  
  22. @SuppressWarnings("deprecation")  
  23. public class SolrTest {  
  24.     //指定solr服务器的地址  
  25.     private final static String URL = "http://localhost:8080/solr";  
  26.       
  27.     @Test  
  28.     public void test1(){  
  29.         //1、创建SolrServer对象,该对象有两个可以使用,都是线程安全的  
  30. //      CommonsHttpSolrServer:启动web服务器使用的,通过http请求的  
  31. //      EmbeddedSolrServer:内嵌式的,导入solr的jar包就可以使用了  
  32.         try {  
  33.             CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  34.             //把查询出来的数据全部删除  
  35. //          server.deleteByQuery("*:*");  
  36. //          server.commit();  
  37.               
  38.             SolrInputDocument doc = new SolrInputDocument();  
  39.             //id是必填的,并且是String类型的  
  40.             //<field name="id" type="string" indexed="true" stored="true" required="true" />  
  41.             //id是唯一的主键,当多次添加的时候,最后添加的相同id会覆盖前面的域  
  42.             doc.addField("id""1");  
  43.             doc.addField("msg_title""这是我的第一个solrj程序");  
  44.             doc.addField("msg_content""solr程序的运行");  
  45.             server.add(doc);  
  46.             server.commit();  
  47.         } catch (MalformedURLException e) {  
  48.             e.printStackTrace();  
  49.         } catch (SolrServerException e) {  
  50.             e.printStackTrace();  
  51.         } catch (IOException e) {  
  52.             e.printStackTrace();  
  53.         }  
  54.     }  
  55.       
  56.     /** 
  57.      * 基于列表的添加 
  58.      * @throws SolrServerException 
  59.      * @throws IOException 
  60.      */  
  61.     @Test  
  62.     public void test2() throws SolrServerException, IOException{  
  63.         List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();  
  64.         SolrInputDocument doc = new SolrInputDocument();  
  65.         doc.addField("id""2");  
  66.         doc.addField("msg_title""很好,solr可以工作了");  
  67.         doc.addField("msg_content""solr总算可以正式工作了");  
  68.           
  69.         docs.add(doc);  
  70.           
  71.         doc = new SolrInputDocument();  
  72.         doc.addField("id""3");  
  73.         doc.addField("msg_title""测试以下solr的添加");  
  74.         doc.addField("msg_content""看看能不能添加一个列表信息");  
  75.           
  76.         docs.add(doc);  
  77.           
  78.         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  79.         server.add(docs);  
  80.         server.commit();  
  81.     }  
  82.       
  83.     /** 
  84.      * 基于javabean的添加 
  85.      * @throws SolrServerException 
  86.      * @throws IOException 
  87.      */  
  88.     @Test  
  89.     public void test3() throws SolrServerException, IOException{  
  90.         List<Message> msgs = new ArrayList<Message>();  
  91.         //多值域的添加使用数组  
  92.         msgs.add(new Message("4","基于javabean的添加"new String[]{"javabean的添加附件","javabean的添加附件1"}));  
  93.         msgs.add(new Message("5","基于javabean的列表数据的添加"new String[]{"通过对象完成添加的附件","通过对象完成添加的附件1"}));  
  94.           
  95.         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  96.         server.addBeans(msgs);  
  97.         server.commit();  
  98.     }  
  99.       
  100.     @Test  
  101.     public void test4() throws SolrServerException, MalformedURLException{  
  102.         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  103.         //定义查询字符串  
  104.         SolrQuery query = new SolrQuery("*:*");  
  105.         //实现分页的查询  
  106.         query.setStart(0);  
  107.         query.setRows(3);  
  108.         QueryResponse res = server.query(query);  
  109.         //查询出来的结果都保存在SolrDocumentList中  
  110.         SolrDocumentList sdl = res.getResults();  
  111.         System.out.println("总数:"+sdl.getNumFound());  
  112.         for(SolrDocument sd : sdl){  
  113.             System.out.println(sd.get("id")+"#"+sd.get("msg_title")+"#"+sd.get("msg_content"));  
  114.         }  
  115.     }  
  116.       
  117.     @Test  
  118.     public void test5() throws MalformedURLException, SolrServerException{  
  119.         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  120.         //相当于QueryParser  
  121.         SolrQuery query = new SolrQuery("*:*");  
  122.         query.setStart(1);  
  123.         query.setRows(3);  
  124.         QueryResponse res = server.query(query);  
  125.         //可以直接查询相应的bean对象,但是不是很常用  
  126.         //使用这种方式无法获取总数量  
  127.         List<Message> list = res.getBeans(Message.class);  
  128.         System.out.println("当前总数:"+list.size());  
  129.         for(Message msg : list){  
  130.             System.out.println(msg.getId()+"#"+msg.getTitle()+"#"+msg.getContent());  
  131.         }  
  132.     }  
  133.       
  134.     @Test  
  135.     public void test6() throws SolrServerException, MalformedURLException{  
  136.         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  137.         SolrQuery query = new SolrQuery("msg_content:solr");  
  138.         query.setHighlight(true).setHighlightSimplePre("<span class='red'>").setHighlightSimplePost("</span>")  
  139.         .setStart(0).setRows(10);  
  140.         //hl.fl表示高亮的field,也就是高亮的区域  
  141.         query.setParam("hl.fl""msg_title,msg_content");  
  142.         QueryResponse res = server.query(query);  
  143.           
  144.         SolrDocumentList sdl = res.getResults();  
  145.         System.out.println("总数:"+sdl.getNumFound());  
  146.         for(SolrDocument sd : sdl){  
  147. //          System.out.println(sd.get("id")+"#"+sd.get("msg_title")+"#"+sd.get("msg_content"));  
  148.             String id = (String) sd.get("id");  
  149.             //在solr这里对需要加高亮的字段必须要在索引中的store=true才行  
  150.             System.out.println(id+"#"+res.getHighlighting().get(id).get("msg_content"));;  
  151.               
  152.         }  
  153.     }  
  154.       
  155.     /** 
  156.      * 测试分组 
  157.      * @throws MalformedURLException  
  158.      * @throws SolrServerException  
  159.      */  
  160.     @Test  
  161.     public void test7() throws MalformedURLException, SolrServerException{  
  162.         CommonsHttpSolrServer server = new CommonsHttpSolrServer(URL);  
  163.         SolrQuery query = new SolrQuery("city:*");  
  164.         query.setIncludeScore(false);  
  165.         query.setFacet(true);  
  166.         query.addFacetField("city");  
  167.         query.setFacetSort(true);  
  168.         QueryResponse res = server.query(query);  
  169.         List<Count> countList = res.getFacetField("city").getValues();  
  170.           
  171.         for(Count count : countList){  
  172.             System.out.println(count.getName()+"#"+count.getCount());  
  173.         }  
  174.         /** 
  175.          * 输出结果: 
  176.          *  上海#5290 
  177.             深圳#2763 
  178.             广州#2504 
  179.             北京#1962 
  180.             东莞#1764 
  181.             杭州#1713 
  182.             苏州#1661 
  183.             南京#1529 
  184.          */  
  185.     }  
  186.   
  187.       
  188.       
  189. }  

1、

2、

3、

4、

5、

6、

7、

8、

9、

10、

0 0
原创粉丝点击