顯示具有 SolrJ 標籤的文章。 顯示所有文章
顯示具有 SolrJ 標籤的文章。 顯示所有文章

2015年2月3日 星期二

【Apache Solr】 建立 Collection (Create Collection using SolrJ or RESTful)

【Apache Solr】建立 Collection (Create Collection) 介紹了如何在 SolrCloud 中建立一個 Collection

當然也說明了一些建立 Collection 的一些條件,包括 Shard 的數量以及 Config 須先上傳到 Zookeeper

接下來介紹一下如何用 SolrJ 以及 RESTful 的方式建立一個 Collection

SolrJ 是一個套件能夠讓開發者透過 Java 的方式來操作 Solr,以下是建立 Collection 的片段程式

String zkHosts = "locahost:9983";
String collectionName = "testCollection1";
String confName       = "testCollection1";  //Configuration Name (You must upload to zookeeper first)
int shards            = 3;
CloudSolrServer server = new CloudSolrServer(zkHosts);
CollectionAdminResponse response = CollectionAdminRequest.createCollection(
                                collectionName, shards, confName , server);

if(null != response.getErrorMessages()){
        /* print error here */
 for(Entry error: response.getErrorMessages()){
  result.addErrorMsg(error.getValue());
 }
}
server.shutdown();

CollectionAdminRequest 有很多的重載方法,可以去查它的 API