Skip to content

Instantly share code, notes, and snippets.

Last active December 22, 2015 10:48
Show Gist options
  • Save jpountz/6461246 to your computer and use it in GitHub Desktop.
Save jpountz/6461246 to your computer and use it in GitHub Desktop.
File to restore a corrupted segment if the stored fields are not corrupted.
// Set codec, dir and segmentName accordingly to the segment you are trying to restore
Codec codec = new Lucene42Codec();
Directory dir = File("/tmp/test"));
String segmentName = "_0";
IOContext ioContext = new IOContext();
SegmentInfo segmentInfos = codec.segmentInfoFormat().getSegmentInfoReader().read(dir, segmentName, ioContext);
Directory segmentDir;
if (segmentInfos.getUseCompoundFile()) {
segmentDir = new CompoundFileDirectory(dir, IndexFileNames.segmentFileName(segmentName, "", IndexFileNames.COMPOUND_FILE_EXTENSION), ioContext, false);
} else {
segmentDir = dir;
FieldInfos fieldInfos = codec.fieldInfosFormat().getFieldInfosReader().read(segmentDir, segmentName, ioContext);
StoredFieldsReader storedFieldsReader = codec.storedFieldsFormat().fieldsReader(segmentDir, segmentInfos, fieldInfos, ioContext);
for (int i = 0; i < segmentInfos.getDocCount(); ++i) {
try {
DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
storedFieldsReader.visitDocument(i, visitor);
Document doc = visitor.getDocument();
// do whatever you need with 'doc'
} catch (IOException e) {
System.err.println("Couldn't get document " + i + ", stored fields corruption?");
Copy link

Nice idea!

Copy link

taton commented Feb 10, 2014

-use progames?
-(create .fdt .fdx .fnm .frq .nrm .prx .tii .tis and sements_1 .gen)

and web?

help me please..

-use progames?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment