CommonsHttpSolrServer
CommonsHttpSolrServer 使用HTTPClient 和solr服务器进行通信。
String url = "http://localhost:8983/solr";
SolrServer server = new CommonsHttpSolrServer( url );
CommonsHttpSolrServer 是线程安全的,建议重复使用CommonsHttpSolrServer 实例。
Setting XMLResponseParser
sorlr J 目前使用二进制的格式作为默认的格式。对于solr1.2的用户通过显示的设置才能使用XML格式。
server.setParser(new XMLResponseParser());
Changing other Connection Settings
CommonsHttpSorlrServer 允许设置链接属性。
String url = "http://localhost:8983/solr"
CommonsHttpSolrServer server = new CommonsHttpSolrServer( url );
server.setSoTimeout(1000); // socket read timeout
server.setConnectionTimeout(100);
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false); // defaults to false
// allowCompression defaults to false.
// Server side must support gzip or deflate for this to have any effect.
server.setAllowCompression(true);
server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
EmbeddedSolrServer
EmbeddedSorrServer提供和CommonsHttpSorlrServer相同的接口,它不需要http连接。
//注意,下面的属性也是可以在jvm参数里面设置的
System.setProperty("solr.solr.home", "/home/shalinsmangar/work/oss/branch-1.3/example/solr");
CoreContainer.Initializer initializer = new CoreContainer.Initializer();
CoreContainer coreContainer = initializer.initialize();
EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, "");
如果你想要使用 Multicore 特性,那么你可以这样使用:
File home = new File( getSolrHome() );
File f = new File( home, "solr.xml" );
multicore.load( getSolrHome(), f );
EmbeddedSolrServer server = new EmbeddedSolrServer( multicore, "core name as defined in solr.xml" );
如果你在你的项目中内嵌solr服务,这将是一个不错的选择。无论你能否使用http,它都提供相同的接口。
用法
solrj 被设计成一个可扩展的框架,用以向solr服务器提交请求,并接收回应。
我们已经将最通用的一些命令封装在了solrServer类中了。
Adding Data to Solr
- 首先需要获得一个server的实例,
SolrServer server = getSolrServer();
- 如果,你使用的是一个远程的solrServer的话呢,你或许会这样来实现getSolrServer()这个方法:
public SolrServer getSolrServer(){
//the instance can be reused
return new CommonsHttpSolrServer();
}
- 如果,你使用的是一个本地的solrServer的话,你或许会这样来实现getSolrServer()方法:
public SolrServer getSolrServer(){
//the instance can be reused
return new EmbeddedSolrServer();
}
- 如果,你在添加数据之前,想清空现有的索引,那么你可以这么做:
server.deleteByQuery( "*:*" );// delete everything!
- 构造一个document
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField( "id", "id1", 1.0f );
doc1.addField( "name", "doc1", 1.0f );
doc1.addField( "price", 10 );
- 构造另外一个文档,每个文档都能够被独自地提交给solr,但是,批量提交是更高效的。每一个对SolrServer的请求都是http请求,当然对于EmbeddedSolrServer来说,是不一样的。
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField( "id", "id2", 1.0f );
doc2.addField( "name", "doc2", 1.0f );
doc2.addField( "price", 20 );
- 构造一个文档的集合
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
docs.add( doc2 );
- 将documents提交给solr
server.add( docs );
- 提交一个commit
server.commit();
- 在添加完documents后,立即做一个commit,你可以这样来写你的程序:
UpdateRequest req = new UpdateRequest();
req.setAction( UpdateRequest.ACTION.COMMIT, false, false );
req.add( docs );
UpdateResponse rsp = req.process( server );
Streaming documents for an update
在很多的情况下,StreamingUpdateSolrServer也挺有用的。如果你使用的是solr1.4以上的版本的话,下面的代码,或许会用得着。下面的这种方法挺好用的,尤其是当你向服务器提交数据的时候。
.
CommonsHttpSolrServer server = new CommonsHttpSolrServer(); Iterator<SolrInputDocument> iter = new Iterator<SolrInputDocument>(){ public boolean hasNext() { boolean result ; // set the result to true false to say if you have more documensts return result; } public SolrInputDocument next() { SolrInputDocument result = null; // construct a new document here and set it to result return result; } }; server.add(iter);
you may also use the addBeans(Iterator<?> beansIter) method to write pojos
Directly adding POJOs to Solr
- 使用 java 注释创建java bean。@Field ,可以被用在域上,或者是setter方法上。如果一个域的名称跟bean的名称是不一样的,那么在java注释中填写别名,具体的,可以参照下面的域categories
import org.apache.solr.client.solrj.beans.Field;
public class Item {
@Field
String id;
@Field("cat")
String[] categories;
@Field
List<String> features;
}
- java注释也可以使用在setter方法上,如下面的例子:
@Field("cat")
public void setCategory(String[] c){
this.categories = c;
}
这里应该要有一个相对的,get方法(没有加java注释的)来读取属性
- Get an instance of server
SolrServer server = getSolrServer();
- 创建bean实例
Item item = new Item();
item.id = "one";
item.categories = new String[] { "aaa", "bbb", "ccc" };
- 添加给solr
server.addBean(item);
- 将多个bean提交给solr
List<Item> beans ;
//add Item objects to the list
server.addBeans(beans);
注意: 你可以重复使用SolrServer,这样可以提高性能。
Reading Data from Solr
- 获取solrserver的实例
SolrServer server = getSolrServer();
- 构造 SolrQuery
SolrQuery query = new SolrQuery();
query.setQuery( "*:*" );
query.addSortField( "price", SolrQuery.ORDER.asc );
- 向服务器发出查询请求
QueryResponse rsp = server.query( query );
- 获取结果。
SolrDocumentList docs = rsp.getResults();
- 想要以javabean的方式获取结果,那么这个javabean必须像之前的例子一样有java注释。
List<Item> beans = rsp.getBeans(Item.class);
高级用法
solrJ 提供了一组API,来帮助我们创建查询,下面是一个faceted query的例子。
SolrServer server = getSolrServer();
SolrQuery solrQuery = new SolrQuery().
setQuery("ipod").
setFacet(true).
setFacetMinCount(1).
setFacetLimit(8).
addFacetField("category").
addFacetField("inStock");
QueryResponse rsp = server.query(solrQuery);
所有的 setter/add 方法都是返回它自己本身的实例,所以就像你所看到的一样,上面的用法是链式的。