在MyEclipse下的SSH項目,要調用Solr接口進行操作。
1、先運行solr
2、在已搭建好的SSH項目中用Solrj調用Solr的接口
3、導入如下solr的jar包到搭建好的SSH項目的WebRoot——WEB-INF——lib文件夾下(復制到這個文件夾下,刷新項目),
(1)F:\solr-4.10.0\dist\solrj-lib下的所有jar包,
(2) F:\solr-4.10.0\dist下的如下兩個jar包
添加索引:
package cn.lx.web; 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; public class Test { public static void main(String[] args) throws SolrServerException, IOException { SolrServer solr=null; SolrInputDocument document=new SolrInputDocument(); String url="http://localhost:8983/solr/"; solr=new HttpSolrServer(url); document.addField("id", "123"); document.addField("subject", "shusheng"); solr.add(document); solr.commit(); } }
2、刪除索引
public void delete() throws SolrServerException, IOException { String urlString = "http://localhost:8983/solr"; SolrServer solr = new HttpSolrServer(urlString); solr.deleteById("1"); solr.commit(); }