Index setup
We’ll create a similar index to DirectoryFileContents, but we’ll stick with the default codec, since we’re
going to explore how the codec is used to decode the index files (since codec is short for “coder and decoder”).
private static Path createLuceneIndex() throws IOException {
Path indexDir = Files.createTempDirectory(DirectoryFileContents.class.getSimpleName());
try (FSDirectory directory = FSDirectory.open(indexDir);
IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig())) {
List<String> texts = List.of(
"The quick fox jumped over the lazy, brown dog",
"Lorem ipsum, dolor sit amet",
"She left the web, she left the loom, she made three paces through the room",
"The sly fox sneaks past the oblivious dog"
);
int i = 0;
for (String text : texts) {
List<IndexableField> doc = new ArrayList<>();
doc.add(new TextField("text", text, Field.Store.YES));
doc.add(new IntField("val", i++, Field.Store.YES));
indexWriter.addDocument(doc);
}
}
return indexDir;
}