Skip to content

Apache Lucene open-source search software

Close vector dictionary when exiting the demo

Michael Sokolovb8210dee7aa6
diff --git a/lucene/demo/src/java/org/apache/lucene/demo/IndexFiles.java b/lucene/demo/src/java/org/apache/lucene/demo/IndexFiles.java
index e9bc5815caa..1e23c5d9b84 100644
--- a/lucene/demo/src/java/org/apache/lucene/demo/IndexFiles.java
+++ b/lucene/demo/src/java/org/apache/lucene/demo/IndexFiles.java
@@ -54,15 +54,18 @@ import org.apache.lucene.store.FSDirectory;
* <p>This is a command-line application demonstrating simple Lucene indexing. Run it with no
* command-line arguments for usage information.
*/
-public class IndexFiles {
+public class IndexFiles implements AutoCloseable {
// Calculates embedding vectors for KnnVector search
private final DemoEmbeddings demoEmbeddings;
+ private final KnnVectorDict vectorDict;
private IndexFiles(Path vectorDictPath) throws IOException {
if (vectorDictPath != null) {
- demoEmbeddings = new DemoEmbeddings(new KnnVectorDict(vectorDictPath));
+ vectorDict = new KnnVectorDict(vectorDictPath);
+ demoEmbeddings = new DemoEmbeddings(vectorDict);
} else {
+ vectorDict = null;
demoEmbeddings = null;
}
}
@@ -139,19 +142,18 @@ public class IndexFiles {
//
// iwc.setRAMBufferSizeMB(256.0);
- IndexWriter writer = new IndexWriter(dir, iwc);
- IndexFiles indexFiles = new IndexFiles(vectorDictPath);
- indexFiles.indexDocs(writer, docDir);
+ try (IndexWriter writer = new IndexWriter(dir, iwc);
+ IndexFiles indexFiles = new IndexFiles(vectorDictPath)) {
+ indexFiles.indexDocs(writer, docDir);
- // NOTE: if you want to maximize search performance,
- // you can optionally call forceMerge here. This can be
- // a terribly costly operation, so generally it's only
- // worth it when your index is relatively static (ie
- // you're done adding documents to it):
- //
- // writer.forceMerge(1);
-
- writer.close();
+ // NOTE: if you want to maximize search performance,
+ // you can optionally call forceMerge here. This can be
+ // a terribly costly operation, so generally it's only
+ // worth it when your index is relatively static (ie
+ // you're done adding documents to it):
+ //
+ // writer.forceMerge(1);
+ }
Date end = new Date();
try (IndexReader reader = DirectoryReader.open(dir)) {
@@ -185,10 +187,9 @@ public class IndexFiles {
if (Files.isDirectory(path)) {
Files.walkFileTree(
path,
- new SimpleFileVisitor<Path>() {
+ new SimpleFileVisitor<>() {
@Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
- throws IOException {
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
try {
indexDoc(writer, file, attrs.lastModifiedTime().toMillis());
} catch (
@@ -259,4 +260,11 @@ public class IndexFiles {
}
}
}
+
+ @Override
+ public void close() throws IOException {
+ if (vectorDict != null) {
+ vectorDict.close();
+ }
+ }
}
diff --git a/lucene/demo/src/java/org/apache/lucene/demo/SearchFiles.java b/lucene/demo/src/java/org/apache/lucene/demo/SearchFiles.java
index 37da184be22..eeaaa95176e 100644
--- a/lucene/demo/src/java/org/apache/lucene/demo/SearchFiles.java
+++ b/lucene/demo/src/java/org/apache/lucene/demo/SearchFiles.java
@@ -154,6 +154,9 @@ public class SearchFiles {
break;
}
}
+ if (vectorDict != null) {
+ vectorDict.close();
+ }
reader.close();
}