Java連線單機版solr測試程式碼之solrJ的運用
阿新 • • 發佈:2018-12-10
1、首先確保solr伺服器已開啟,本地通過http://ip:埠號/solr可以訪問到
2、pom.xml中引用solrJ.jar包
<properties>
<solrj.version>4.10.3</solrj.version>
</properties>
<!-- solr客戶端 <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>${solrj.version}</version> </dependency>
3、編寫測試程式碼
新增,具體說明請參考註釋:
@Test public void add() throws Exception{ //單機版solr連線時,建立HttpSolrServer物件;叢集版建立CloudSolrServer物件 SolrServer solrServer = new HttpSolrServer("http://192.168.63.117:8080/solr/collection1"); //建立文件物件SolrInputDocument SolrInputDocument document = new SolrInputDocument(); //向文件中新增域,必須有id域,域的名稱必須在schema.xml中定義 document.addField("id",123); document.addField("item_title","solr測試1"); document.addField("item_price",1100000); document.addField("item_category_name","林哈哈"); //把文件物件寫入索引庫 solrServer.add(document); //提交 solrServer.commit(); }
刪除:
@Test public void deleteByDocumentId() throws Exception{ SolrServer solrServer = new HttpSolrServer("http://192.168.63.117:8080/solr/collection1"); solrServer.deleteById("change.me"); //提交 solrServer.commit(); } @Test public void deleteByQuery() throws Exception{ SolrServer solrServer = new HttpSolrServer("http://192.168.63.117:8080/solr/collection1"); solrServer.deleteByQuery("title:李"); //提交 solrServer.commit(); }