1.在項(xiàng)目中使用SolrJ管理集群
使用步驟:
第一步:把solrJ相關(guān)的jar包添加到工程中。
第二步:創(chuàng)建一個(gè)SolrServer對(duì)象,需要使用CloudSolrServer子類。構(gòu)造方法的參數(shù)是zookeeper的地址列表。
第三步:需要設(shè)置DefaultCollection屬性。
第四步:創(chuàng)建一SolrInputDocument對(duì)象。
第五步:向文檔對(duì)象中添加域
第六步:把文檔對(duì)象寫入索引庫。
第七步:提交。
@Test
public void testsolrCloud() {
try{
// 第一步:把solrJ相關(guān)的jar包添加到工程中。
// 第二步:創(chuàng)建一個(gè)SolrServer對(duì)象,需要使用CloudSolrServer子類。構(gòu)造方法的參數(shù)是zookeeper的地址列表。
CloudSolrServer solrServer = new CloudSolrServer("192.168.208.40:2182,192.168.208.40:2183,192.168.208.40:2184");
// 第三步:需要設(shè)置DefaultCollection屬性。
solrServer.setDefaultCollection("collection2");
// 第四步:創(chuàng)建一SolrInputDocument對(duì)象。
SolrInputDocument document = new SolrInputDocument();
// 第五步:向文檔對(duì)象中添加域
document.addField("item_title", "測試商品");
document.addField("item_price", "100");
document.addField("id", "test001");
// 第六步:把文檔對(duì)象寫入索引庫。
solrServer.add(document);
// 第七步:提交。
solrServer.commit();
}catch(Exception e) {
e.printStackTrace();
}
}
單機(jī)版其實(shí)和集群版差不多~只是SolrServer變成了CloudSolrServer對(duì)象
2.把搜素功能切換到集群版

image.png
記住把以前單機(jī)版的注釋掉
3.測試結(jié)果

solr集群測試.png

集群導(dǎo)入索引庫結(jié)果.png