mirror of
https://github.com/gotson/komga.git
synced 2025-12-20 15:34:17 +01:00
feat: index updater facility on startup
This commit is contained in:
parent
6e0c51ed1d
commit
a7204e85b9
3 changed files with 29 additions and 0 deletions
|
|
@ -2,6 +2,9 @@ package org.gotson.komga.infrastructure.search
|
|||
|
||||
import mu.KotlinLogging
|
||||
import org.apache.lucene.analysis.Analyzer
|
||||
import org.apache.lucene.document.Document
|
||||
import org.apache.lucene.document.Field
|
||||
import org.apache.lucene.document.StringField
|
||||
import org.apache.lucene.index.DirectoryReader
|
||||
import org.apache.lucene.index.IndexWriter
|
||||
import org.apache.lucene.index.IndexWriterConfig
|
||||
|
|
@ -31,6 +34,23 @@ class LuceneHelper(
|
|||
|
||||
fun indexExists(): Boolean = DirectoryReader.indexExists(directory)
|
||||
|
||||
fun setIndexVersion(version: Int) {
|
||||
getIndexWriter().use { indexWriter ->
|
||||
val doc = Document().apply {
|
||||
add(StringField("index_version", version.toString(), Field.Store.YES))
|
||||
add(StringField("type", "index_version", Field.Store.NO))
|
||||
}
|
||||
indexWriter.updateDocument(Term("type", "index_version"), doc)
|
||||
}
|
||||
}
|
||||
|
||||
fun getIndexVersion(): Int =
|
||||
getIndexReader().use { index ->
|
||||
val searcher = IndexSearcher(index)
|
||||
val topDocs = searcher.search(TermQuery(Term("type", "index_version")), 1)
|
||||
topDocs.scoreDocs.map { searcher.doc(it.doc)["index_version"] }.firstOrNull()?.toIntOrNull() ?: 1
|
||||
}
|
||||
|
||||
fun searchEntitiesIds(searchTerm: String?, entity: LuceneEntity): List<String>? {
|
||||
return if (!searchTerm.isNullOrBlank()) {
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import kotlin.math.ceil
|
|||
import kotlin.time.measureTime
|
||||
|
||||
private val logger = KotlinLogging.logger {}
|
||||
private const val INDEX_VERSION = 2
|
||||
|
||||
@Component
|
||||
class SearchIndexLifecycle(
|
||||
|
|
@ -71,6 +72,9 @@ class SearchIndexLifecycle(
|
|||
logger.info { "Wrote ${entity.name} index in $duration" }
|
||||
}
|
||||
}
|
||||
|
||||
luceneHelper.setIndexVersion(INDEX_VERSION)
|
||||
logger.info { "Lucene index version: ${luceneHelper.getIndexVersion()}" }
|
||||
}
|
||||
|
||||
@JmsListener(destination = QUEUE_SSE, selector = QUEUE_SSE_SELECTOR, containerFactory = TOPIC_FACTORY)
|
||||
|
|
|
|||
|
|
@ -23,6 +23,11 @@ class SearchIndexController(
|
|||
if (!luceneHelper.indexExists()) {
|
||||
logger.info { "Lucene index not found, trigger rebuild" }
|
||||
taskReceiver.rebuildIndex(HIGHEST_PRIORITY)
|
||||
} else {
|
||||
logger.info { "Lucene index version: ${luceneHelper.getIndexVersion()}" }
|
||||
when (luceneHelper.getIndexVersion()) {
|
||||
1 -> taskReceiver.rebuildIndex(HIGHEST_PRIORITY)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue