Solr中初学Demo

  1 import java.util.Collection;
  2 import java.util.Date;
  3 
  4 import org.apache.solr.client.solrj.SolrQuery;
  5 import org.apache.solr.client.solrj.impl.HttpSolrServer;
  6 import org.apache.solr.client.solrj.response.QueryResponse;
  7 import org.apache.solr.common.SolrDocument;
  8 import org.apache.solr.common.SolrDocumentList;
  9 import org.apache.solr.common.SolrInputDocument;
 10 import org.junit.Test;
 11 
 12 public class TestSolr {
 13     
 14     String baseURL = "http://192.168.1.99:8983/solr";
 15     HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL);
 16     
 17     /**
 18      * 获取操作solr的客户端对象
 19      * @throws Exception
 20      */    
 21     @Test
 22     public void test1() {
 23         //指定solr的连接地址,注意:默认情况下连接的是collection1这个索引库
 24         //下面的两个baseurl效果一样
 25         //String baseURL = "http://192.168.1.171:8983/solr/collection1";        
 26         String baseURL = "http://192.168.1.99:8983/solr";
 27         HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL);        
 28         System.out.println(httpSolrServer.toString());
 29     }
 30 
 31     /**
 32      * 建立索引-1
 33      * 
 34      * add(HttpSolrServer server,SolrInputDocument doc){
 35      *         server.add(doc);
 36      *         server.commit();
 37      * }
 38      * @throws Exception
 39      */
 40     @Test
 41     public void test2() throws Exception {
 42         //把数据封装为一个document
 43         SolrInputDocument doc = new SolrInputDocument();
 44         doc.setField("id", "1");
 45         doc.setField("name", "crxy1");//这个字段必须在schema.xml文件中定义了,否则会设置失败.这里id和name都已经在schema.xml文件中定义了.
 46         doc.setField("last_modified", new Date());
 47         //把这个文档添加到solr中
 48         httpSolrServer.add(doc);//也会把数据添加到内存中,但是查询不到,因为没有在内存中生成segment,执行软提交的时候才会生成
 49         //把这个添加操作提交
 50         httpSolrServer.commit();//(硬提交)这个提交其实是表示把索引数据直接提交到硬盘中,并且可以保证数据能够查询到
 51         //httpSolrServer.commit(true, true, true);//软提交,数据保存在内存中,并且保证数据可以查询
 52         /**
 53          * 在工作中,不建议没add一条数据,就硬提交一次,这样太消耗性能
 54          * 建议,为了保证实时读取到新增的数据,可以,每add一条数据,就调用一次软提交
 55          * 如果对数据的实时查询要求不是很高,建议在批量添加数据的时候,可以每添加1000条左右调用一次硬提交。
 56          */
 57     }
 58     
 59     /**
 60      * 建立索引-2
 61      * 这种工作中用的比较多
 62      * test2中需要自己去封装set...使用比较少...
 63      * @throws Exception
 64      */
 65     @Test
 66     public void test3() throws Exception {
 67         Person person = new Person();
 68         person.setId("22");
 69         person.setName("heeh22");
 70         
 71         httpSolrServer.addBean(person);
 72         httpSolrServer.commit();
 73     }
 74     
 75     /**
 76      * 删除
 77      * @throws Exception
 78      */
 79     @Test
 80     public void test4() throws Exception {
 81         //httpSolrServer.deleteById("1");//根据id删除
 82         httpSolrServer.deleteByQuery("id:22");//根据查询条件删除
 83         httpSolrServer.commit();
 84         
 85     }
 86     
 87     /**
 88      * 
 89      * @throws Exception
 90      */
 91     @Test
 92     public void test5() throws Exception {
 93         //组装查询条件
 94         SolrQuery params = new SolrQuery();
 95         
 96         //具体查询条件就要拼字符串
 97         params.setQuery("id:1");
 98         //params.setQuery("name:samsung");//params.setQuery()和params.set()是一样的...建议使用setQuery
 99         //params.set("q", "*:*");
100         
101         //执行查询请求
102         QueryResponse response = httpSolrServer.query(params);
103         //从response中获取返回的结果
104         SolrDocumentList results = response.getResults();
105         //获取满足条件的数据总条数
106         long numFound = results.getNumFound();
107         System.out.println("总数:"+numFound);
108         
109         //当前查询返回document文档的总数,默认最多返回10条,通过rows控制的
110         //这个获取的总数是有问题的,如果要做分页,获取总页面数,要用results.getNumFound()来获取
111         System.out.println(results.size());//如果上面params对象使用params.set("q", "*:*"); 打印的是10
112         
113         for (SolrDocument solrDocument : results) {
114             //获取文档中的所有字段
115             Collection<String> fieldNames = solrDocument.getFieldNames();
116             for (String field : fieldNames) {
117                 //打印字段和对应字段的值
118                 System.out.println(field+":"+solrDocument.get(field));
119             }
120         }
121     }
122     
123 }
原文地址:https://www.cnblogs.com/DreamDrive/p/5770576.html