You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Dancer <46...@qq.com> on 2016/01/25 09:53:39 UTC
about Lucene replicator
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