diff --git a/app/src/main/kotlin/SimpleNotes.kt b/app/src/main/kotlin/SimpleNotes.kt index 8bd5060..b73389b 100644 --- a/app/src/main/kotlin/SimpleNotes.kt +++ b/app/src/main/kotlin/SimpleNotes.kt @@ -54,6 +54,7 @@ fun main() { migrations.migrate() val noteService = koin.get() + noteService.dropAllIndexes() noteService.indexAll() koin.get().start() diff --git a/app/src/main/kotlin/views/components/NoteListHeader.kt b/app/src/main/kotlin/views/components/NoteListHeader.kt index a315060..9f7aefc 100644 --- a/app/src/main/kotlin/views/components/NoteListHeader.kt +++ b/app/src/main/kotlin/views/components/NoteListHeader.kt @@ -1,6 +1,8 @@ package be.simplenotes.app.views.components import kotlinx.html.* +import kotlinx.html.ButtonType.submit +import kotlinx.html.FormMethod.post fun DIV.noteListHeader(numberOfDeletedNotes: Int) { div("flex justify-between mb-4") { @@ -16,4 +18,14 @@ fun DIV.noteListHeader(numberOfDeletedNotes: Int) { ) { +"New" } } } + form(method = post, classes = "mb-4") { + val colors = "bg-gray-800 border-gray-700 focus:border-teal-500 text-white" + input( + name = "search", + classes = "$colors rounded w-3/4 border appearance-none focus:outline-none text-base p-2" + ) + button(type = submit, classes = "btn btn-green w-1/4") { + +"search" + } + } } diff --git a/domain/src/main/kotlin/usecases/NoteService.kt b/domain/src/main/kotlin/usecases/NoteService.kt index d7fdabd..f03fa37 100644 --- a/domain/src/main/kotlin/usecases/NoteService.kt +++ b/domain/src/main/kotlin/usecases/NoteService.kt @@ -86,6 +86,8 @@ class NoteService( } fun search(userId: Int, searchTerms: SearchTerms) = searcher.search(userId, searchTerms) + + fun dropAllIndexes() = searcher.dropAll() } data class PaginatedNotes(val pages: Int, val notes: List) diff --git a/persistance/src/main/resources/logback.xml b/persistance/src/main/resources/logback.xml index 5024926..d94bc53 100644 --- a/persistance/src/main/resources/logback.xml +++ b/persistance/src/main/resources/logback.xml @@ -6,11 +6,11 @@ - + - + diff --git a/search/src/main/kotlin/NoteSearcher.kt b/search/src/main/kotlin/NoteSearcher.kt index 8a0c79d..27d59f4 100644 --- a/search/src/main/kotlin/NoteSearcher.kt +++ b/search/src/main/kotlin/NoteSearcher.kt @@ -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() { - 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 - } - } } diff --git a/search/src/main/kotlin/utils/PathUtils.kt b/search/src/main/kotlin/utils/PathUtils.kt new file mode 100644 index 0000000..fd5966e --- /dev/null +++ b/search/src/main/kotlin/utils/PathUtils.kt @@ -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() { + 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 + } +}