一.solrj使用步驟
第一步:把solrJ相關的jar包添加到工程中。
第二步:創建一個SolrServer對象,需要使用CloudSolrServer子類。構造方法的參數是zookeeper的地址列表。
第三步:需要設置DefaultCollection屬性。
第四步:創建一SolrInputDocument對象。
第五步:向文檔對象中添加域
第六步:把文檔對象寫入索引庫。
第七步:提交。
@Test
public void testSolrCloudAddDocument() throws Exception {
// 第一步:把solrJ相關的jar包添加到工程中。
// 第二步:創建一個SolrServer對象,需要使用CloudSolrServer子類。構造方法的參數是zookeeper的地址列表。
//參數是zookeeper的地址列表,使用逗號分隔
CloudSolrServer solrServer = new CloudSolrServer("192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183");
// 第三步:需要設置DefaultCollection屬性。
solrServer.setDefaultCollection("collection2");
// 第四步:創建一SolrInputDocument對象。
SolrInputDocument document = new SolrInputDocument();
// 第五步:向文檔對象中添加域
document.addField("item_title", "測試商品");
document.addField("item_price", "100");
document.addField("id", "test001");
// 第六步:把文檔對象寫入索引庫。
solrServer.add(document);
// 第七步:提交。
solrServer.commit();
}
創建一個CloudSolrServer對象,其他處理和單機版一致。
把搜索功能切換到集群版
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util4.2.xsd">