Drop indexes + view

This commit is contained in:
Hubert Van De Walle 2020-08-19 18:47:19 +02:00
parent 315a01ea18
commit 68109f8666
6 changed files with 52 additions and 24 deletions

View File

@ -54,6 +54,7 @@ fun main() {
migrations.migrate() migrations.migrate()
val noteService = koin.get<NoteService>() val noteService = koin.get<NoteService>()
noteService.dropAllIndexes()
noteService.indexAll() noteService.indexAll()
koin.get<Server>().start() koin.get<Server>().start()

View File

@ -1,6 +1,8 @@
package be.simplenotes.app.views.components package be.simplenotes.app.views.components
import kotlinx.html.* import kotlinx.html.*
import kotlinx.html.ButtonType.submit
import kotlinx.html.FormMethod.post
fun DIV.noteListHeader(numberOfDeletedNotes: Int) { fun DIV.noteListHeader(numberOfDeletedNotes: Int) {
div("flex justify-between mb-4") { div("flex justify-between mb-4") {
@ -16,4 +18,14 @@ fun DIV.noteListHeader(numberOfDeletedNotes: Int) {
) { +"New" } ) { +"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"
}
}
} }

View File

@ -86,6 +86,8 @@ class NoteService(
} }
fun search(userId: Int, searchTerms: SearchTerms) = searcher.search(userId, searchTerms) fun search(userId: Int, searchTerms: SearchTerms) = searcher.search(userId, searchTerms)
fun dropAllIndexes() = searcher.dropAll()
} }
data class PaginatedNotes(val pages: Int, val notes: List<PersistedNoteMetadata>) data class PaginatedNotes(val pages: Int, val notes: List<PersistedNoteMetadata>)

View File

@ -6,11 +6,11 @@
</pattern> </pattern>
</encoder> </encoder>
</appender> </appender>
<root level="INFO"> <root level="DEBUG">
<appender-ref ref="STDOUT"/> <appender-ref ref="STDOUT"/>
</root> </root>
<logger name="org.eclipse.jetty" level="INFO"/> <logger name="org.eclipse.jetty" level="INFO"/>
<logger name="me.liuwj.ktorm.database" level="DEBUG"/> <logger name="me.liuwj.ktorm.database" level="INFO"/>
<logger name="com.zaxxer.hikari" level="INFO"/> <logger name="com.zaxxer.hikari" level="INFO"/>
<logger name="org.flywaydb.core" level="INFO"/> <logger name="org.flywaydb.core" level="INFO"/>
</configuration> </configuration>

View File

@ -2,6 +2,7 @@ package be.simplenotes.search
import be.simplenotes.domain.model.PersistedNote import be.simplenotes.domain.model.PersistedNote
import be.simplenotes.domain.model.PersistedNoteMetadata import be.simplenotes.domain.model.PersistedNoteMetadata
import be.simplenotes.search.utils.rmdir
import org.apache.lucene.analysis.standard.StandardAnalyzer import org.apache.lucene.analysis.standard.StandardAnalyzer
import org.apache.lucene.index.* import org.apache.lucene.index.*
import org.apache.lucene.search.* import org.apache.lucene.search.*
@ -68,7 +69,7 @@ class NoteSearcher(basePath: Path = Path.of("/tmp", "lucene")) {
} }
fun deleteIndex(userId: Int, uuid: UUID) { 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 dir = getDirectory(userId)
val config = IndexWriterConfig(StandardAnalyzer()) val config = IndexWriterConfig(StandardAnalyzer())
@ -108,31 +109,14 @@ class NoteSearcher(basePath: Path = Path.of("/tmp", "lucene")) {
} }
val query = builder.build() val query = builder.build()
logger.debug("Searching: $query")
val topDocs = searcher.search(query, 10) val topDocs = searcher.search(query, 10)
logger.debug("Searching: `$query` results: ${topDocs.totalHits.value}")
return topDocs.toResults(searcher) return topDocs.toResults(searcher)
} }
fun dropIndex(userId: Int) { fun dropIndex(userId: Int) = rmdir(File(baseFile, userId.toString()).toPath())
val index = File(baseFile, userId.toString()).toPath()
try { fun dropAll() = rmdir(baseFile.toPath())
Files.walkFileTree(
index,
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
}
}
} }

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
}
}