每日學習筆記(20)

1, Solr合併索引數據有兩種方法,第一種是1.4版本中引入的,通過CoreAdminHandler來實現,示例如下: http://localhost:8983/solr/admin/cores?action=mergeindexes&core=core0&indexDir=/opt/solr/core1/data/index&indexDir=/opt/solr/core2/data/index 上述命令會將core1和core2的索引合併到core0中去,這裏最值得注意的一點是:一旦合併完成,必須在core0上調用commit操作,否則索引數據的變化對於searchers來說是暫時不可見的,只有等到下次core0重新裝載起來時纔可見。 實現代碼如下:

  1. /**  
  2.  * 合併索引數據 
  3.  * @param otherIndexPathList 其他待合併的索引數據路徑 
  4.  * @param coreName 合併的目標solr核名稱 
  5.  *  
  6.  */    private void mergeIndexData(List<String> otherIndexPathList, String coreName) { 
  7.     if (null != otherIndexPathList && otherIndexPathList.size() > 0) { 
  8.         HttpClient client = new HttpClient(); 
  9.         client.setConnectionTimeout(20000); 
  10.         client.setTimeout(20000); 
  11.         client.setHttpConnectionFactoryTimeout(20000); 
  12.          
  13.         StringBuffer sb = new StringBuffer(); 
  14.         for (int i = 0; i < otherIndexPathList.size(); ++i) { 
  15.             sb.append("&indexDir=" + otherIndexPathList.get(i) + "/data/index"); 
  16.         } 
  17.         String mergeIndexCMD = "http://" + Constants.LOCAL_ADDRESS + ":" + this.port  + "/admin/cores?action=mergeindexes&core="+ coreName; 
  18.         if (sb.length() > 0) { 
  19.             mergeIndexCMD += sb.toString(); 
  20.         } 
  21.         HttpMethod method = new GetMethod(mergeIndexCMD); 
  22.         method.getParams().setContentCharset("GBK"); 
  23.         method.getParams().setHttpElementCharset("GBK"); 
  24.         method.getParams().setCredentialCharset("GBK"); 
  25.  
  26.         // execute the method. 
  27.         try { 
  28.             if (client.executeMethod(method) == 200) { 
  29.                 String response = method.getResponseBodyAsString(); 
  30.                 if (logger.isInfoEnabled()) { 
  31.                     logger.info("merge result" + response); 
  32.                 } 
  33.             } 
  34.         } catch (Exception e) { 
  35.             logger.error("合併其他索引數據失敗 " + coreName + ",索引目錄: " + otherIndexPathList, e); 
  36.         } 
  37.          
  38.         //commit操作讓合併後的索引對搜索生效 
  39.         StreamingUpdateSolrServer httpSolrServer = null
  40.         httpSolrServer = getSolrServer(Constants.LOCAL_ADDRESS, this.port, coreName);  
  41.         try { 
  42.             httpSolrServer.commit(); 
  43.         } catch (Exception e) { 
  44.         } 
  45.     } 
  46.          

 第二種方法是Solr3.3中引入的,也是通過CoreAdminHandler來實現,示例如下:

http://localhost:8983/solr/admin/cores?action=mergeindexes&core=core0&srcCore=core1&srcCore=core2

      同第一種方法一樣,一旦合併完成,必須在core0上調用commit操作,否則索引數據的變化對於searchers來說是暫時不可見的,只有等到下次core0重新裝載起來時纔可見。

      使用”srcCore”和”indexDir”這兩種方法的區別:

1)    使用”indexDir”參數,你可以合並不是與Solr核相關聯的索引數據,比如通過Lucene直接創建的索引

2)    使用”indexDir”參數,你必須注意索引數據不是直接寫入的,這就意味着如果它是一個solr核的索引,必須要關閉IndexWriter,這樣才能觸發一個commit命令。

3)    “indexDir”必須指向solr核所在的主機上的磁盤路徑,這就限制比較多了,而相反,你可以只給srcCore一個solr核的名稱,而不關心它的實際索引路徑在哪。

4)    使用”srcCore”,你必須確保即使源索引數據同時存在寫操作的時候,合併後的索引頁不會損壞。

 

2,   solr索引合併的時候,底層其實調用的還是Lucene,因此你schema.xml中配置的uniqueKeys它並不知道,因此當你對兩個包含相同文檔(由uniqueKey確定)的索引進行合併時,你會得到雙倍的文檔數,solr這個地方應該改下,畢竟你不是簡單的Lucene包裝嘛。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章