Last active
August 25, 2017 03:23
-
-
Save madneal/753a297bbc046ac04e47ddc0c423a3f3 to your computer and use it in GitHub Desktop.
lucene-test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void indexDocs(final IndexWriter writer, Path path) throws IOException | |
{ | |
//Directory? | |
if (Files.isDirectory(path)) | |
{ | |
//Iterate directory | |
Files.walkFileTree(path, new SimpleFileVisitor<Path>() | |
{ | |
@Override | |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException | |
{ | |
try | |
{ | |
//Index this file | |
indexDoc(writer, file, attrs.lastModifiedTime().toMillis()); | |
} | |
catch (IOException ioe) | |
{ | |
ioe.printStackTrace(); | |
} | |
return FileVisitResult.CONTINUE; | |
} | |
}); | |
} | |
else | |
{ | |
//Index this file | |
indexDoc(writer, path, Files.getLastModifiedTime(path).toMillis()); | |
} | |
} | |
private static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException | |
{ | |
try (InputStream stream = Files.newInputStream(file)) | |
{ | |
//Create lucene Document | |
Document doc = new Document(); | |
doc.add(new StringField("path", file.toString(), Field.Store.YES)); | |
doc.add(new LongPoint("modified", lastModified)); | |
doc.add(new TextField("contents", new String(Files.readAllBytes(file)), Field.Store.YES)); | |
//Updates a document by first deleting the document(s) | |
//containing <code>term</code> and then adding the new | |
//document. The delete and then add are atomic as seen | |
//by a reader on the same index | |
writer.updateDocument(new Term("path", file.toString()), doc); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment