分享

Solr学习(三) 牛刀小试之SolrJ

xioaxu790 2014-12-7 19:02:07 发表于 连载型 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 1 38442
本帖最后由 xioaxu790 于 2014-12-7 20:16 编辑
问题导读

1、什么是Solr Client,有哪些支持语言?
2、Solr的工作过程分为哪两类?



1 初识Solr Client
我们首先用一个最简单的Solr Client来小试一下我们前面的部署成果,即选用一个资料较多的Java平台的Client: SolrJ。
Solr各种平台下的Client有很多,但是貌似SolrJ最容易上手。。。 看看下面都是Solr CLient:
Solr Client Libraries / Language Bindings



在跑SolrJ的最简单的程序之前先整体对Solr全文检索有个框架性的了解:

Solr的工作过程分为大概有两类:
(1)绿色的线为第一类,通过Solr Client编程自己从数据库查出数据,然后通过Solr的接口将这些数据发送给Solr,让Solr为这些数据建立索引并
存储起来(1-3)。然后Solr Client编程去请求solr来进行查询并返回结果(4-6);
(2)另一条线唯一不同的就是导入数据的方式,不是通过Solr Client,而是Solr自己导入,通过DIH(DataImportHandler)自己从数据库导入数据并建立索引,接下来4-6步就一样了。

作为初学者的我,更倾向于先学习第二种了,不过还是先用第一种跑个SolrJ的小例子吧,因为我现在这台机器上没有装数据库。。。下面就是第一种方式。

2 简单的SolrJ测试程序:
经过我前面介绍的部署Solr到Tomcat的准备工作(http://blog.csdn.net/jiyiqinlovexx/article/details/14648501)之后,现在就把Tomcat启动,打开Eclipse,新建一个工程,右击工程选择Properties,选择Add External Jars,吧E:\solr-4.5.1\dist\solrj-lib 目录和 E:\solr-4.5.1\dist目录下面的jar包都引入进去吧(其实我也不知道哪些是不必要的)。
然后新建两个类:

(1)导入数据病创建索引的类:
  1. package jiq.solrJ;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.solr.client.solrj.SolrServerException;  
  6. import org.apache.solr.client.solrj.impl.HttpSolrServer;  
  7. import org.apache.solr.common.SolrInputDocument;  
  8.   
  9. public class SolrJDataImporter {  
  10.       
  11.     public static void main(String[] args) throws IOException, SolrServerException {  
  12.         HttpSolrServer server = new HttpSolrServer("http://localhost:80/solr");  
  13.         for (int i = 0; i < 1000; ++i) {  
  14.             SolrInputDocument doc = new SolrInputDocument();  
  15.             doc.addField("cat", "book");  
  16.             doc.addField("id", "book-" + i);  
  17.             doc.addField("name", "The Legend of Po part " + i);  
  18.             server.add(doc);  
  19.             if (i % 100 == 0)  
  20.                 server.commit(); // periodically flush  
  21.         }  
  22.         server.commit();  
  23.     }  
  24. }
复制代码


(2)查询的类:
  1. package jiq.solrJ;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import org.apache.solr.client.solrj.SolrServerException;  
  5. import org.apache.solr.client.solrj.impl.HttpSolrServer;  
  6. import org.apache.solr.client.solrj.response.QueryResponse;  
  7. import org.apache.solr.common.SolrDocumentList;  
  8. import org.apache.solr.common.params.ModifiableSolrParams;  
  9.   
  10. public class SolrJSearcher {  
  11.     public static void main(String[] args) throws MalformedURLException, SolrServerException {  
  12.         HttpSolrServer solr = new HttpSolrServer("http://localhost:80/solr");  
  13.   
  14.         ModifiableSolrParams params = new ModifiableSolrParams();  
  15.         params.set("q", "Legend");  
  16.         params.set("defType", "edismax");  
  17.         params.set("start", "0");  
  18.   
  19.         QueryResponse response = solr.query(params);  
  20.         SolrDocumentList results = response.getResults();  
  21.          
  22.         if(results.size() < 1) System.out.println("没有查到任何数据");  
  23.         else{         
  24.             for (int i = 0; i < results.size(); ++i) {  
  25.                 System.out.println(results.get(i));  
  26.             }  
  27.         }  
  28.     }  
  29. }
复制代码


先运行第一个,在运行第二个,输出结果如下:
  1. log4j:WARN No appenders could be found for logger (org.apache.solr.client.solrj.impl.HttpClientUtil).
  2. log4j:WARN Please initialize the log4j system properly.
  3. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
  4. SolrDocument{cat=[book], id=book-701, name=The Legend of Po part 701, _version_=1451323401851371520}
  5. SolrDocument{cat=[book], id=book-702, name=The Legend of Po part 702, _version_=1451323401857662976}
  6. SolrDocument{cat=[book], id=book-703, name=The Legend of Po part 703, _version_=1451323401862905856}
  7. SolrDocument{cat=[book], id=book-704, name=The Legend of Po part 704, _version_=1451323401867100160}
  8. SolrDocument{cat=[book], id=book-705, name=The Legend of Po part 705, _version_=1451323401871294464}
  9. SolrDocument{cat=[book], id=book-706, name=The Legend of Po part 706, _version_=1451323401873391616}
  10. SolrDocument{cat=[book], id=book-707, name=The Legend of Po part 707, _version_=1451323401877585920}
  11. SolrDocument{cat=[book], id=book-708, name=The Legend of Po part 708, _version_=1451323401879683072}
  12. SolrDocument{cat=[book], id=book-709, name=The Legend of Po part 709, _version_=1451323401882828800}
  13. SolrDocument{cat=[book], id=book-710, name=The Legend of Po part 710, _version_=1451323401885974528}
复制代码


OK,测试可以用了吧,接下来我要真正开始在我的.NET项目中利用Solr来完成全文检索功能了,嗯。。。 估计首选SolrNet吧,资料多一些。
不过我觉得首先还是要学习DIH。

已有(1)人评论

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条