导入jar包
package com.tzy.solrJ; import java.io.IOException; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.common.SolrInputDocument; import org.junit.Test; public class SolrJManager { @Test public void testAdd() throws SolrServerException, IOException{ String baseURL = "http://localhost:8080/solr/"; SolrServer solrServer = new HttpSolrServer(baseURL); SolrInputDocument doc = new SolrInputDocument(); doc.setField("id", "lala"); doc.setField("name", "张三"); //添加 solrServer.add(doc); solrServer.commit(); } }
@Test public void deleteDocumentByid() throws Exception { //创建连接 SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr"); //根据id删除文档 solrServer.deleteById("lala"); //删除全部 //solrServer.deleteByQuery("*:*", 1000); //提交修改 solrServer.commit(); }