1.solrJ概念
solrJ是Java连接solr进行查询检索和索引更新维护的jar包。
2.项目引入solrJ相关jar包
对于maven工程,直接将下面内容加入到pom文件中即可。
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.3.1</version>
</dependency>
注意solrj编译依赖下面jar包
![](https://images2015.cnblogs.com/blog/150046/201510/150046-20151022194039645-2092738306.png)
![](https://images2015.cnblogs.com/blog/150046/201510/150046-20151022194040208-1891039082.png)
3.主要用到的类接口简介
- 初始化SolrClient对象
//直接指定solr的URL和core1,只能查询或更新core1内容
SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr/core1");
QueryResponse resp = client.query(new SolrQuery("*:*"));
//指定solr的URL,查询或更新时要指定core
SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr");
QueryResponse resp = client.query("core1", new SolrQuery("*:*"));
更新维护索引的主要接口是
addBean(Object obj)
addBean(Object obj, int commitWithinMs)
addBean(String collection, Object obj, int commitWithinMs)
add(String collection, Collection<SolrInputDocument> docs, int commitWithinMs)
add(String collection, SolrInputDocument doc, int commitWithinMs)
该函数有多个重载形式,obj是要加入索引的实体对象,collection指定要操作的core,commitWithinMs要提交的毫秒数,默认为-1,add后不会更新,要调用
commit(String collection)
提交后才能更新查询到。SolrInputDocument和Object之间转换
doc = getBinder().toSolrInputDocument(obj);
objList =solr.getBinder().getBeans(CaseEntity.class, resp.getResults());