第十三篇:使用SolrJ管理集群

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容