一、SolrJ简介
SolrJ是操作Solr的Java客户端,它提供了增加、修改、删除、查询Solr索引的Java接口。通过solrJ提供的API接口来操作solr服务,SolrJ底层是通过使用httpClient中的方法来完成Solr的操作。
二、依赖配置
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.7.1</version>
</dependency>
三、简单实例
接这篇文章的结果Solr-导入MySQL数据
当前core中已存在的信息如图:
1、添加文档
@Test
public void testAdd() throws Exception{
//1.创建连接
HttpSolrClient solrServer = new HttpSolrClient.Builder("http://localhost:8983/solr/zym").build();
//2.创建一个文档对象
SolrInputDocument inputDocument = new SolrInputDocument();
//向文档中添加域以及对应的值(注意:所有的域必须在schema.xml中定义过,前两篇导入时已定义)
inputDocument.addField("id", "5");
inputDocument.addField("product_title", "裙子");
inputDocument.addField("product_sell_point", "爆款");
//3.将文档写入索引库中
solrServer.add(inputDocument);
//4.提交
solrServer.commit();
}
添加成功后,页面可直接查看如图:
2、更新文档(其实更新的内容不存在则是新增)
@Test
public void testUpdate() throws Exception