添加删除文档
添加jar包
<solrj.version>4.10.3</solrj.version>
<!-- solr客户端 -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>${solrj.version}</version>
</dependency>
添加文档
第一步:创建一个SolrServer,使用HttpSolrServer创建对象
第二步:创建一个文档对象SolrInputDocument对象
第三步:向文档中添加域
必须有id域,域的名称必须在schema.xml中定义
第四步:把文档添加到索引库中
第五步:提交
@Test
public void testAddDocument() throws Exception {
//创建一个SolrServer对象。创建一个HttpSolrServer对象
//需要指定solr服务的url
SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr/collection1");
//创建一个文档对象SolrInputDocument
SolrInputDocument document = new SolrInputDocument();
//向文档中添加域,必须有id域,域的名称必须在schema.xml中定义
document.addField("id", "123");
document.addField("item_title", "测试商品3");
document.addField("item_price", 1000);
//把文档对象写入索引库
solrServer.add(document);
//提交
solrServer.commit();
}
删除文档
根据id删除
第一步:创建一个SolrServer对象
第二步:调用SolrServer对象的根据id删除的方法
第三步:提交
@Test
public void deleteDocumentById() throws Exception {
SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr/collection1");
solrServer.deleteById("test001");
//提交
solrServer.commit();
}
根据查询删除
@Test
public void deleteDocumentByQuery() throws Exception {
SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr/collection1");
solrServer.deleteByQuery("item_title:测试商品3");
solrServer.commit();
}