lucene連結インデックス

3583 ワード

Clustorの問題でインデックスを同期できないため、rmi(デュアルマシン)、UDPブロードキャスト(マルチマシン)を通信ミドルウェアとしてclustorをインデックス同期することが頭に浮かぶが、テスト後の効率は相対的に低いため、別の道を切り開き、最終的にインデックス統合方式で高速なインデックス統合を行い、時間の短いインデックス同期が速い目的を達成する.コードは次のとおりです.
package com.jiepu.lucene_23;


import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
  
//lucene : 
public class Merge {  
    /** 
     * @author Shane Zhao about merge Index in PCCW BJDEV 
     *   
     *  
     * @param from 
     *             to  
     * @param to 
     *             from  
     * @param sa 
     */  
    private static void mergeIndex(File from, File to,StandardAnalyzer sa) {  
        IndexWriter indexWriter = null;  
        try {  
            System.out.println(" !\t ");  
            indexWriter = new IndexWriter(to, sa, false);  
            indexWriter.setMergeFactor(100000);  
            indexWriter.setMaxFieldLength(Integer.MAX_VALUE);  
            indexWriter.setMaxBufferedDocs(Integer.MAX_VALUE);  
            indexWriter.setMaxMergeDocs(Integer.MAX_VALUE);  
            FSDirectory[] fs = { FSDirectory.getDirectory(from) };  
            indexWriter.addIndexes(fs);   // 
            indexWriter.optimize();  
            indexWriter.close();  
            System.out.println(" !\t ");  
        } catch (Exception e) {  
            System.out.println(" !");  
            e.printStackTrace();  
        } finally {  
            try {  
                if (indexWriter != null)  
                    indexWriter.close();  
            } catch (Exception e) {  
  
            }  
  
        }  
  
    }  
    private static void mergeIndex2(File froms[], File to,StandardAnalyzer sa) {  
        IndexWriter indexWriter = null;  
        try {  
            System.out.println(" !\t ");  
            indexWriter = new IndexWriter(to, sa, false);  
            indexWriter.setMergeFactor(100000);  
            indexWriter.setMaxFieldLength(Integer.MAX_VALUE);  
            indexWriter.setMaxBufferedDocs(Integer.MAX_VALUE);  
            indexWriter.setMaxMergeDocs(Integer.MAX_VALUE); 
            FSDirectory[] fs = new FSDirectory[froms.length];
            for (int i=0;i<froms.length;i++) {
				fs[i]=FSDirectory.getDirectory(froms[i]);
			}      
           //indexWriter.addDocument(new Document());
            indexWriter.addIndexes(fs);   // 
            indexWriter.optimize();  
            indexWriter.close();  
            System.out.println(" !\t ");  
        } catch (Exception e) {  
            System.out.println(" !");  
            e.printStackTrace();  
        } finally {  
            try {  
                if (indexWriter != null)  
                    indexWriter.close();  
            } catch (Exception e) {  
  
            }  
  
        }  
  
    }  
    
    
    
      
    public static void main(String[] areg){  
        File from = new File("e:/index2");  
        File to = new File("e:/index");  
        //mergeIndex(from,to,new StandardAnalyzer());
        mergeIndex2(new File[]{new File("e:/index2"),new File("e:/index")},new File("e:/index4"),new StandardAnalyzer());
    }

  
}  
http://blog.csdn.net/witsmakemen/article/details/7481789