###solrj介绍: solrj是solr的一款java工具包,通过solrj我们可以使用java语言来对solr服务进行操作。它提供了对solr索引的改查等一系列操作的接口。 ###maven中对solrj的引入 我们要使用solrj首先要在maven中对其引入:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.2.1</version>
</dependency>
引入之后我们就可以实现用java对solr索引的操作。 ###solrj对solr索引的操作 首先我们先定义一个实体类,用于对获取的数据进行处理
public class Blog {
@Field
private String id;
@Field
private String title;
@Field//如果和managed-scheme里面配置的field name不一致需要在注解内指定
private String author;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
###新增索引 新增索引代码如下:
public class SolrCurl {
public static void main(String[] args) throws IOException, SolrServerException {
HttpSolrClient.Builder builder = new HttpSolrClient.Builder();
builder.withBaseSolrUrl(“http://120.79.213.17:8983/solr”);
//获得一个solr的客户端
SolrClient client = builder.build();
Blog blog = new Blog();
blog.setId(“2”);
blog.setAuthor(“cgss”);
blog.setTitle(“五月天”);
client.addBean(“test-core”,blog);
client.commit(“test-core”);
}
}
执行如上代码之后我们可以去test-core的索引库中查找到该数据:
###查询索引 查询索引代码如下:
public class SolrCurl {
public static void main(String[] args) throws IOException, SolrServerException {
HttpSolrClient.Builder builder = new HttpSolrClient.Builder();
builder.withBaseSolrUrl(“http://120.79.213.17:8983/solr”);
//获得一个solr的客户端
SolrClient client = builder.build();
//构建查询参数
SolrQuery query = new SolrQuery(“id:1”);
//添加回显内容
// query.addField(“age”);
// query.addField(“id”);
//执行查询,获得响应结果
QueryResponse response = client.query(“test-core”, query);
//输出结果
List<Blog> blog = response.getBeans(Blog.class);
System.out.println(blog);
}
}
执行如上代码,我们可以得到如下结果: 从图中我们可以看到solrj非常方便的将返回的json数据自动转化成了我们想要的blog对象。SolrQuery的方法还有很多,比如我们可以使用addField()方法指定solr给我们返回的参数,从而是结果更加清晰。还有比如我们可以使用addHighlightField()方法指定返回参数的高亮显示,可以用于在前端的展示。SolrQuery的查询条件方法有很多,更多可以查看api文档。
###修改索引 修改索引的方式和新增类似,代码如下:
public class SolrCurl {
public static void main(String[] args) throws IOException, SolrServerException {
HttpSolrClient.Builder builder = new HttpSolrClient.Builder();
builder.withBaseSolrUrl(“http://120.79.213.17:8983/solr”);
//获得一个solr的客户端
SolrClient client = builder.build();
Blog blog = new Blog();
blog.setId(“2”);
blog.setAuthor(“cgss”);
blog.setTitle(“五月天11”);
client.addBean(“test-core”,blog);
client.commit(“test-core”);
}
}
从代码可以看出,我们修改指定id的索引时,同样执行新增的方法,只是将id保持不变。执行如上代码之后,我们就可得到如下图的结果:
###索引的删除 索引的删除代码如下:
public class SolrCurl {
public static void main(String[] args) throws IOException, SolrServerException {
HttpSolrClient.Builder builder = new HttpSolrClient.Builder();
builder.withBaseSolrUrl(“http://120.79.213.17:8983/solr”);
//获得一个solr的客户端
SolrClient client = builder.build();
client.deleteById(“test-core”,“2”);
client.commit(“test-core”);
}
}
执行如上代码之后,我们可以看到索引库中已经没有刚刚我们修改的数据了
**注意:**在执行数据的新增时无需执行commit()方法。而在执行删除和修改操作时我们需要执行commit方法来提交操作,否则该操作并不会保存。
solrj7.2.1 api地址:api地址