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?

package com.wilddog.lucene;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.Callable;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy;
import org.apache.lucene.index.SnapshotDeletionPolicy;
import org.apache.lucene.index.Term;
import org.apache.lucene.replicator.IndexReplicationHandler;
import org.apache.lucene.replicator.IndexRevision;
import org.apache.lucene.replicator.LocalReplicator;
import org.apache.lucene.replicator.PerSessionDirectoryFactory;
import org.apache.lucene.replicator.ReplicationClient;
import org.apache.lucene.replicator.ReplicationClient.ReplicationHandler;
import org.apache.lucene.replicator.ReplicationClient.SourceDirectoryFactory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**
 * Index all text files under a directory.
 * <p>
* This is a command-line application demonstrating simple Lucene indexing. Run
 * it with no command-line arguments for usage information.
 */
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("/tmp/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岁的他能在足球之路上走的更远。";
        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(new KeepOnlyLastCommitDeletionPolicy());
            iwc.setIndexDeletionPolicy(snapshotter);
IndexWriter writer = new IndexWriter(IndexFiles.getDirInstance(), iwc);// the 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));
            ReplicatorThread p = new ReplicatorThread();
            new Thread(p, "ReplicatorThread").start();
            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();
        }
    }
}

class ReplicatorThread implements Runnable {

    public void run() {
        Callable<Boolean> callback = null;
        ReplicationHandler handler = null;
        try {
handler = new IndexReplicationHandler(IndexFiles.getDirInstance(), callback); SourceDirectoryFactory factory = new PerSessionDirectoryFactory(IndexFiles.getPathInstance()); ReplicationClient client = new ReplicationClient(IndexFiles.getLocalReplicatorInstance(), handler, factory);
            client.updateNow();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("death");
    }
}

Reply via email to