Fix stale Lucene reader after ratings index rebuild

The reader and searcher were lazily initialized once, so after
rebuild() created a new index, searches still used the old reader
pointing to outdated document IDs. When those IDs were used to
index into the new players array, wrong entries were returned.

Now reader/searcher are refreshed after each build().
This commit is contained in:
Claude Brisson
2025-11-29 10:21:31 +01:00
parent 662f438cee
commit 4113d76904

View File

@@ -39,8 +39,8 @@ class PlayerIndex {
val stopChars = Regex("[_-]")
}
private final val directory: Directory = ByteBuffersDirectory(NoLockFactory.INSTANCE)
private val reader by lazy { DirectoryReader.open(directory) }
private val searcher by lazy { IndexSearcher(reader) }
private lateinit var reader: DirectoryReader
private lateinit var searcher: IndexSearcher
// helper functions
fun Json.Object.field(key: String) = getString(key) ?: throw Error("missing $key")
@@ -68,6 +68,9 @@ class PlayerIndex {
++count
}
}
// Refresh reader and searcher to see the new index
reader = DirectoryReader.open(directory)
searcher = IndexSearcher(reader)
logger.info("indexed $count players")
}