Hi,
Here is my code to backup index       files with Lucene Replicator, but It 
doesn't work well, No files       were backuped.
     Could you check my code and give me       your advice?

I bolded the key code.




public class IndexFiles {


    private static Directory dir;
    private static Path bakPath;
    private static LocalReplicator replicator;


    public static LocalReplicator getLocalReplicatorInstance() {
        if (replicator == null) {
            replicator = new LocalReplicator();
        }
        return replicator;
    }
    public static Directory getDirInstance() {
        if (dir == null) {
            try {
                dir = FSDirectory.open(Paths.get("/tmp/index"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return dir;
    }
    public static Path getPathInstance() {
        if (bakPath == null) {
            bakPath = Paths.get("/data/indexBak");
        }
        return bakPath;
    }




    /** Index all text files under a directory. */
    public static void main(String[] args) {
        String id = "-oderfilssdhsjs";
        String title = "????????";
        String body = "???????????????????????????????????????????????? 
??????????????????????????????????3??2??????"
                + "?????????????????????????????????????????????????? 
??????????????????????????????????????????????" + "???????????????????? 
????????22??????????????????????????????";
        Callable<Boolean> callback = null;
        ReplicationHandler handler = null;
        ReplicationClient client = null;
        try {
            handler = new IndexReplicationHandler(IndexFiles.getDirInstance(), 
callback);
            SourceDirectoryFactory factory = new 
PerSessionDirectoryFactory(IndexFiles.getPathInstance());
            client = new 
ReplicationClient(IndexFiles.getLocalReplicatorInstance(), handler, factory);
        } catch (IOException e) {
            e.printStackTrace();
        }


        try {
            // Directory dir = FSDirectory.open(Paths.get(indexPath));
            Analyzer analyzer = new IKAnalyzer(true);
            IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
            SnapshotDeletionPolicy snapshotter = new 
SnapshotDeletionPolicy(iwc.getIndexDeletionPolicy());
            iwc.setIndexDeletionPolicy(snapshotter);
            IndexWriter writer = new IndexWriter(IndexFiles.getDirInstance(), 
iwc);
            LocalReplicator replicator = 
IndexFiles.getLocalReplicatorInstance();


            Document doc = new Document();
            Field articleId = new StringField("id", id, Field.Store.YES);
            doc.add(articleId);
            Field articleTitle = new TextField("title", title, Field.Store.YES);
            doc.add(articleTitle);
            Field articleBody = new TextField("body", body, Field.Store.NO);
            doc.add(articleBody);
            Field tag1 = new TextField("tags", "????", Field.Store.NO);
            doc.add(tag1);
            // Field tag2 = new TextField("tags", "????", Field.Store.NO);
            // doc.add(tag2);
            // Field tag3 = new TextField("tags", "????", Field.Store.NO);
            // doc.add(tag3);
            // Field tag4 = new TextField("tags", "??????", Field.Store.NO);
            // doc.add(tag4);


            writer.updateDocument(new Term("id", id), doc);
            writer.commit();
            replicator.publish(new IndexRevision(writer));
            client.updateNow();
            Thread.sleep(50000);
            writer.close();
        } catch (IOException e) {
            System.out.println(" caught a " + e.getClass() + "\n with message: 
" + e.getMessage());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Yours Jean Ju

Reply via email to