From 5c53cbae5586432b81272eb8979558b3a778d802 Mon Sep 17 00:00:00 2001 From: Joseph Ivie Date: Thu, 31 Oct 2024 09:35:30 -0600 Subject: [PATCH] fuzzyPresent directly in TextQuery --- .../lightningdb/SearchTextConditionTest.kt | 28 ++++++------------- .../lightningkite/lightningdb/TextQuery.kt | 11 ++++++-- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/server-mongo/src/test/kotlin/com/lightningkite/lightningdb/SearchTextConditionTest.kt b/server-mongo/src/test/kotlin/com/lightningkite/lightningdb/SearchTextConditionTest.kt index 843242c4..b48151c9 100644 --- a/server-mongo/src/test/kotlin/com/lightningkite/lightningdb/SearchTextConditionTest.kt +++ b/server-mongo/src/test/kotlin/com/lightningkite/lightningdb/SearchTextConditionTest.kt @@ -153,25 +153,15 @@ class SearchTextConditionTest { .toList() .let { assertEquals(2, it.size, "Got $it") } -// var query = "One" -// var condition = path().fullTextSearch(query, requireAllTermsPresent = true) -// var results = collection.find(condition).toList() -// assertContains(results, value1) -// -// query = "One two three" -// condition = path().fullTextSearch(query, requireAllTermsPresent = true) -// results = collection.find(condition).toList() -// assertContains(results, value1) -// -// query = "One two four" -// condition = path().fullTextSearch(query, requireAllTermsPresent = true) -// results = collection.find(condition).toList() -// assertTrue(results.isEmpty()) -// -// query = "one" -// condition = path().fullTextSearch(query, requireAllTermsPresent = true) -// results = collection.find(condition).toList() -// assertContains(results, value1) + collection + .find(condition { it.fullTextSearch("911 gtx", requireAllTermsPresent = true) }) + .toList() + .let { assertEquals(1, it.size, "Got $it") } + + collection + .find(condition { it.fullTextSearch("911 gtz", requireAllTermsPresent = true) }) + .toList() + .let { assertEquals(0, it.size, "Got $it") } } } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/com/lightningkite/lightningdb/TextQuery.kt b/shared/src/commonMain/kotlin/com/lightningkite/lightningdb/TextQuery.kt index a49d2028..11ab6093 100644 --- a/shared/src/commonMain/kotlin/com/lightningkite/lightningdb/TextQuery.kt +++ b/shared/src/commonMain/kotlin/com/lightningkite/lightningdb/TextQuery.kt @@ -55,11 +55,16 @@ data class TextQuery( fun fuzzyPresent(input: String, off: Int = 2): Boolean { val words = input.split(' ', '\n', '\t') return exact.all { - input.contains(it) + input.contains(it, true) } && loose.all { l -> - words.any { w -> levenshtein(l.lowercase(), w.lowercase()) <= off } + words.any { w -> + if(l.termShouldUseFuzzySearch()) + levenshtein(l.lowercase(), w.lowercase()) <= off + else + input.contains(l, true) + } } && reject.none { - input.contains(it) + input.contains(it, true) } }