import org.apache.solr.client.solrj.impl.CloudSolrServer; import org.apache.solr.common.SolrInputDocument; import org.junit.Test; public class SolrCloudTest { @Test public void testAddDocument() throws Exception { //创建一个和solr集群的连接 String zkHost = "192.168.242.135:2181,192.168.242.135:2182,192.168.242.135:2183"; CloudSolrServer solrServer = new CloudSolrServer(zkHost); //指定默认的collection solrServer.setDefaultCollection("collection2"); //创建一个文档对象 SolrInputDocument document = new SolrInputDocument(); //向文档中添加域 document.addField("id", "test001"); document.addField("item_title", "测试商品"); //把文档添加到索引库 solrServer.add(document); //提交 solrServer.commit(); } @Test public void deleteDoucment() throws Exception { //创建一个和solr集群的连接 String zkHost = "192.168.242.135:2181,192.168.242.135:2182,192.168.242.135:2183"; CloudSolrServer solrServer = new CloudSolrServer(zkHost); //指定默认的collection solrServer.setDefaultCollection("collection2"); solrServer.deleteByQuery("*:*"); solrServer.commit(); } }