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

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)导入数据病创建索引的类:

package jiq.solrJ;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;

public class SolrJDataImporter {
	
	public static void main(String[] args) throws IOException, SolrServerException {
		HttpSolrServer server = new HttpSolrServer("http://localhost:80/solr");
		for (int i = 0; i < 1000; ++i) {
			SolrInputDocument doc = new SolrInputDocument();
			doc.addField("cat", "book");
			doc.addField("id", "book-" + i);
			doc.addField("name", "The Legend of Po part " + i);
			server.add(doc);
			if (i % 100 == 0)
				server.commit(); // periodically flush
		}
		server.commit();
	}
}

(2)查询的类:

package jiq.solrJ;

import java.net.MalformedURLException;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.ModifiableSolrParams;

public class SolrJSearcher {
	public static void main(String[] args) throws MalformedURLException, SolrServerException {
		HttpSolrServer solr = new HttpSolrServer("http://localhost:80/solr");

		ModifiableSolrParams params = new ModifiableSolrParams();
		params.set("q", "Legend");
		params.set("defType", "edismax");
		params.set("start", "0");

		QueryResponse response = solr.query(params);
		SolrDocumentList results = response.getResults();
		
		if(results.size() < 1) System.out.println("没有查到任何数据");
		else{		
			for (int i = 0; i < results.size(); ++i) {
				System.out.println(results.get(i));
			}
		}
	}
}

先运行第一个,在运行第二个,输出结果如下:

log4j:WARN No appenders could be found for logger (org.apache.solr.client.solrj.impl.HttpClientUtil).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SolrDocument{cat=[book], id=book-701, name=The Legend of Po part 701, _version_=1451323401851371520}
SolrDocument{cat=[book], id=book-702, name=The Legend of Po part 702, _version_=1451323401857662976}
SolrDocument{cat=[book], id=book-703, name=The Legend of Po part 703, _version_=1451323401862905856}
SolrDocument{cat=[book], id=book-704, name=The Legend of Po part 704, _version_=1451323401867100160}
SolrDocument{cat=[book], id=book-705, name=The Legend of Po part 705, _version_=1451323401871294464}
SolrDocument{cat=[book], id=book-706, name=The Legend of Po part 706, _version_=1451323401873391616}
SolrDocument{cat=[book], id=book-707, name=The Legend of Po part 707, _version_=1451323401877585920}
SolrDocument{cat=[book], id=book-708, name=The Legend of Po part 708, _version_=1451323401879683072}
SolrDocument{cat=[book], id=book-709, name=The Legend of Po part 709, _version_=1451323401882828800}
SolrDocument{cat=[book], id=book-710, name=The Legend of Po part 710, _version_=1451323401885974528}


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

不过我觉得首先还是要学习DIH。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值