@Test public void test1() throws SolrServerException, IOException{ //創建一個連接 HttpSolrServer server = new HttpSolrServer("http://localhost:8088/solr/core1"); //創建一個文檔對象 SolrInputDocument document = new SolrInputDocument(); document.addField("id", "1"); document.addField("title", "哈哈"); //把文檔對象寫入索引庫 server.add(document); //提交 server.commit(); }
特別注意:
url要加上對應的庫,如果
HttpSolrServer server = new HttpSolrServer("http://localhost:8088/solr");這樣不指定在哪個core下會報錯,
一定要這樣
HttpSolrServer server = new HttpSolrServer("http://localhost:8088/solr/core1");