Drop indexes + view

This commit is contained in:
2020-08-19 18:47:19 +02:00
parent 315a01ea18
commit 68109f8666
6 changed files with 52 additions and 24 deletions
+6 -22
View File
@@ -2,6 +2,7 @@ package be.simplenotes.search
import be.simplenotes.domain.model.PersistedNote
import be.simplenotes.domain.model.PersistedNoteMetadata
import be.simplenotes.search.utils.rmdir
import org.apache.lucene.analysis.standard.StandardAnalyzer
import org.apache.lucene.index.*
import org.apache.lucene.search.*
@@ -68,7 +69,7 @@ class NoteSearcher(basePath: Path = Path.of("/tmp", "lucene")) {
}
fun deleteIndex(userId: Int, uuid: UUID) {
logger.debug("Deleting indexing $uuid for user $userId")
logger.debug("Deleting index $uuid for user $userId")
val dir = getDirectory(userId)
val config = IndexWriterConfig(StandardAnalyzer())
@@ -108,31 +109,14 @@ class NoteSearcher(basePath: Path = Path.of("/tmp", "lucene")) {
}
val query = builder.build()
logger.debug("Searching: $query")
val topDocs = searcher.search(query, 10)
logger.debug("Searching: `$query` results: ${topDocs.totalHits.value}")
return topDocs.toResults(searcher)
}
fun dropIndex(userId: Int) {
val index = File(baseFile, userId.toString()).toPath()
try {
Files.walkFileTree(
index,
object : SimpleFileVisitor<Path>() {
override fun visitFile(file: Path, attrs: BasicFileAttributes?): FileVisitResult {
Files.delete(file)
return FileVisitResult.CONTINUE
}
fun dropIndex(userId: Int) = rmdir(File(baseFile, userId.toString()).toPath())
fun dropAll() = rmdir(baseFile.toPath())
override fun postVisitDirectory(dir: Path, exc: IOException?): FileVisitResult {
Files.delete(dir)
return FileVisitResult.CONTINUE
}
}
)
} catch (e: IOException) {
// This is fine
}
}
}
+29
View File
@@ -0,0 +1,29 @@
package be.simplenotes.search.utils
import java.io.IOException
import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes
internal fun rmdir(path: Path) {
try {
Files.walkFileTree(
path,
object : SimpleFileVisitor<Path>() {
override fun visitFile(file: Path, attrs: BasicFileAttributes?): FileVisitResult {
Files.delete(file)
return FileVisitResult.CONTINUE
}
override fun postVisitDirectory(dir: Path, exc: IOException?): FileVisitResult {
Files.delete(dir)
return FileVisitResult.CONTINUE
}
}
)
} catch (e: IOException) {
// This is fine
}
}