Skip to content

Commit 03c2add

Browse files
committed
Fix FTS query behavior when there are multiple terms
Only use FTS when `matchAll = true`, otherwise fallback to regular queries
1 parent dcecee3 commit 03c2add

File tree

6 files changed

+22
-41
lines changed

6 files changed

+22
-41
lines changed

app/src/main/kotlin/com/fibelatti/pinboard/features/linkding/data/BookmarksDao.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ interface BookmarksDao {
5454
tag1: String = "",
5555
tag2: String = "",
5656
tag3: String = "",
57-
matchAll: Boolean = true,
5857
exactMatch: Boolean = false,
5958
untaggedOnly: Boolean = false,
6059
postVisibility: PostVisibility = PostVisibility.None,
@@ -67,7 +66,6 @@ interface BookmarksDao {
6766
tag1 = tag1,
6867
tag2 = tag2,
6968
tag3 = tag3,
70-
matchAll = matchAll,
7169
exactMatch = exactMatch,
7270
untaggedOnly = untaggedOnly,
7371
postVisibility = postVisibility,
@@ -82,7 +80,6 @@ interface BookmarksDao {
8280
tag1: String = "",
8381
tag2: String = "",
8482
tag3: String = "",
85-
matchAll: Boolean = true,
8683
exactMatch: Boolean = false,
8784
untaggedOnly: Boolean = false,
8885
postVisibility: PostVisibility = PostVisibility.None,
@@ -97,7 +94,6 @@ interface BookmarksDao {
9794
tag1 = tag1,
9895
tag2 = tag2,
9996
tag3 = tag3,
100-
matchAll = matchAll,
10197
exactMatch = exactMatch,
10298
untaggedOnly = untaggedOnly,
10399
postVisibility = postVisibility,
@@ -114,7 +110,6 @@ interface BookmarksDao {
114110
tag1: String,
115111
tag2: String,
116112
tag3: String,
117-
matchAll: Boolean,
118113
exactMatch: Boolean,
119114
untaggedOnly: Boolean,
120115
postVisibility: PostVisibility,
@@ -127,7 +122,7 @@ interface BookmarksDao {
127122
val words: List<String> = term.trim()
128123
.split(regex = "\\s+".toRegex())
129124
.filterNot { it.isEmpty() }
130-
val logicalOperator: String = if (matchAll) "and" else "or"
125+
val logicalOperator = "and"
131126

132127
val filterSubquery: String = buildString {
133128
if (words.isNotEmpty()) {
@@ -193,13 +188,13 @@ interface BookmarksDao {
193188
val args: List<Any> = buildList {
194189
if (words.isNotEmpty()) {
195190
add(
196-
words.joinToString(separator = " $logicalOperator ") { word: String ->
197-
if (exactMatch) word else "*$word*"
191+
words.joinToString(separator = " ") { word: String ->
192+
if (exactMatch) word else "$word*"
198193
},
199194
)
200195
addAll(
201196
words.map { word: String ->
202-
if (exactMatch) word else "%$word%"
197+
if (exactMatch) word else "$word%"
203198
},
204199
)
205200
}
@@ -226,7 +221,7 @@ interface BookmarksDao {
226221

227222
private fun formatTagArgument(tag: String, exactMatch: Boolean = true): String {
228223
val sanitizedTag: String = tag.replace(oldValue = "\"", newValue = "")
229-
return if (exactMatch) sanitizedTag else "*$sanitizedTag*"
224+
return if (exactMatch) sanitizedTag else "$sanitizedTag*"
230225
}
231226
// endregion FTS queries
232227

app/src/main/kotlin/com/fibelatti/pinboard/features/linkding/data/PostsDataSourceLinkdingApi.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ internal class PostsDataSourceLinkdingApi @Inject constructor(
283283
readLaterOnly: Boolean,
284284
countLimit: Int,
285285
): Int {
286-
val isFtsCompatible: Boolean = isFtsCompatible(searchTerm) &&
286+
val isFtsCompatible: Boolean = matchAll &&
287+
isFtsCompatible(searchTerm) &&
287288
(tags.isNullOrEmpty() || tags.all { isFtsCompatible(it.name) })
288289

289290
val query: SimpleSQLiteQuery = if (isFtsCompatible) {
@@ -292,7 +293,6 @@ internal class PostsDataSourceLinkdingApi @Inject constructor(
292293
tag1 = tags.getTagName(index = 0),
293294
tag2 = tags.getTagName(index = 1),
294295
tag3 = tags.getTagName(index = 2),
295-
matchAll = matchAll,
296296
exactMatch = exactMatch,
297297
untaggedOnly = untaggedOnly,
298298
postVisibility = postVisibility,
@@ -342,15 +342,15 @@ internal class PostsDataSourceLinkdingApi @Inject constructor(
342342
readLaterOnly = readLaterOnly,
343343
countLimit = countLimit,
344344
)
345-
val isFtsCompatible: Boolean = isFtsCompatible(searchTerm) &&
345+
val isFtsCompatible: Boolean = matchAll &&
346+
isFtsCompatible(searchTerm) &&
346347
(tags.isNullOrEmpty() || tags.all { isFtsCompatible(it.name) })
347348
val query: SimpleSQLiteQuery = if (isFtsCompatible) {
348349
BookmarksDao.allBookmarksFtsQuery(
349350
term = searchTerm,
350351
tag1 = tags.getTagName(index = 0),
351352
tag2 = tags.getTagName(index = 1),
352353
tag3 = tags.getTagName(index = 2),
353-
matchAll = matchAll,
354354
exactMatch = exactMatch,
355355
untaggedOnly = untaggedOnly,
356356
postVisibility = postVisibility,

app/src/main/kotlin/com/fibelatti/pinboard/features/posts/data/PostsDao.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ interface PostsDao {
5656
tag1: String = "",
5757
tag2: String = "",
5858
tag3: String = "",
59-
matchAll: Boolean = true,
6059
exactMatch: Boolean = false,
6160
untaggedOnly: Boolean = false,
6261
postVisibility: PostVisibility = PostVisibility.None,
@@ -69,7 +68,6 @@ interface PostsDao {
6968
tag1 = tag1,
7069
tag2 = tag2,
7170
tag3 = tag3,
72-
matchAll = matchAll,
7371
exactMatch = exactMatch,
7472
untaggedOnly = untaggedOnly,
7573
postVisibility = postVisibility,
@@ -84,7 +82,6 @@ interface PostsDao {
8482
tag1: String = "",
8583
tag2: String = "",
8684
tag3: String = "",
87-
matchAll: Boolean = true,
8885
exactMatch: Boolean = false,
8986
untaggedOnly: Boolean = false,
9087
postVisibility: PostVisibility = PostVisibility.None,
@@ -99,7 +96,6 @@ interface PostsDao {
9996
tag1 = tag1,
10097
tag2 = tag2,
10198
tag3 = tag3,
102-
matchAll = matchAll,
10399
exactMatch = exactMatch,
104100
untaggedOnly = untaggedOnly,
105101
postVisibility = postVisibility,
@@ -116,7 +112,6 @@ interface PostsDao {
116112
tag1: String,
117113
tag2: String,
118114
tag3: String,
119-
matchAll: Boolean,
120115
exactMatch: Boolean,
121116
untaggedOnly: Boolean,
122117
postVisibility: PostVisibility,
@@ -129,7 +124,7 @@ interface PostsDao {
129124
val words: List<String> = term.trim()
130125
.split(regex = "\\s+".toRegex())
131126
.filterNot { it.isEmpty() }
132-
val logicalOperator: String = if (matchAll) "and" else "or"
127+
val logicalOperator = "and"
133128

134129
val filterSubquery: String = buildString {
135130
if (words.isNotEmpty()) {
@@ -193,13 +188,13 @@ interface PostsDao {
193188
val args: List<Any> = buildList {
194189
if (words.isNotEmpty()) {
195190
add(
196-
words.joinToString(separator = " $logicalOperator ") { word: String ->
197-
if (exactMatch) word else "*$word*"
191+
words.joinToString(separator = " ") { word: String ->
192+
if (exactMatch) word else "$word*"
198193
},
199194
)
200195
addAll(
201196
words.map { word: String ->
202-
if (exactMatch) word else "%$word%"
197+
if (exactMatch) word else "$word%"
203198
},
204199
)
205200
}
@@ -226,7 +221,7 @@ interface PostsDao {
226221

227222
private fun formatTagArgument(tag: String, exactMatch: Boolean = true): String {
228223
val sanitizedTag: String = tag.replace(oldValue = "\"", newValue = "")
229-
return if (exactMatch) sanitizedTag else "*$sanitizedTag*"
224+
return if (exactMatch) sanitizedTag else "$sanitizedTag*"
230225
}
231226
// endregion FTS queries
232227

app/src/main/kotlin/com/fibelatti/pinboard/features/posts/data/PostsDataSourceNoApi.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ class PostsDataSourceNoApi @Inject constructor(
128128
readLaterOnly: Boolean,
129129
countLimit: Int,
130130
): Int {
131-
val isFtsCompatible: Boolean = isFtsCompatible(searchTerm) &&
131+
val isFtsCompatible: Boolean = matchAll &&
132+
isFtsCompatible(searchTerm) &&
132133
(tags.isNullOrEmpty() || tags.all { isFtsCompatible(it.name) })
133134

134135
val query: SimpleSQLiteQuery = if (isFtsCompatible) {
@@ -137,7 +138,6 @@ class PostsDataSourceNoApi @Inject constructor(
137138
tag1 = tags.getTagName(index = 0),
138139
tag2 = tags.getTagName(index = 1),
139140
tag3 = tags.getTagName(index = 2),
140-
matchAll = matchAll,
141141
exactMatch = exactMatch,
142142
untaggedOnly = untaggedOnly,
143143
postVisibility = postVisibility,
@@ -186,15 +186,15 @@ class PostsDataSourceNoApi @Inject constructor(
186186
readLaterOnly = readLaterOnly,
187187
countLimit = countLimit,
188188
)
189-
val isFtsCompatible: Boolean = isFtsCompatible(searchTerm) &&
189+
val isFtsCompatible: Boolean = matchAll &&
190+
isFtsCompatible(searchTerm) &&
190191
(tags.isNullOrEmpty() || tags.all { isFtsCompatible(it.name) })
191192
val query: SimpleSQLiteQuery = if (isFtsCompatible) {
192193
PostsDao.allPostsFtsQuery(
193194
term = searchTerm,
194195
tag1 = tags.getTagName(index = 0),
195196
tag2 = tags.getTagName(index = 1),
196197
tag3 = tags.getTagName(index = 2),
197-
matchAll = matchAll,
198198
exactMatch = exactMatch,
199199
untaggedOnly = untaggedOnly,
200200
postVisibility = postVisibility,

app/src/main/kotlin/com/fibelatti/pinboard/features/posts/data/PostsDataSourcePinboardApi.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ internal class PostsDataSourcePinboardApi @Inject constructor(
361361
readLaterOnly: Boolean,
362362
countLimit: Int,
363363
): Int {
364-
val isFtsCompatible: Boolean = isFtsCompatible(searchTerm) &&
364+
val isFtsCompatible: Boolean = matchAll &&
365+
isFtsCompatible(searchTerm) &&
365366
(tags.isNullOrEmpty() || tags.all { isFtsCompatible(it.name) })
366367

367368
val query: SimpleSQLiteQuery = if (isFtsCompatible) {
@@ -370,7 +371,6 @@ internal class PostsDataSourcePinboardApi @Inject constructor(
370371
tag1 = tags.getTagName(index = 0),
371372
tag2 = tags.getTagName(index = 1),
372373
tag3 = tags.getTagName(index = 2),
373-
matchAll = matchAll,
374374
exactMatch = exactMatch,
375375
untaggedOnly = untaggedOnly,
376376
postVisibility = postVisibility,
@@ -420,15 +420,15 @@ internal class PostsDataSourcePinboardApi @Inject constructor(
420420
readLaterOnly = readLaterOnly,
421421
countLimit = countLimit,
422422
)
423-
val isFtsCompatible: Boolean = isFtsCompatible(searchTerm) &&
423+
val isFtsCompatible: Boolean = matchAll &&
424+
isFtsCompatible(searchTerm) &&
424425
(tags.isNullOrEmpty() || tags.all { isFtsCompatible(it.name) })
425426
val query: SimpleSQLiteQuery = if (isFtsCompatible) {
426427
PostsDao.allPostsFtsQuery(
427428
term = searchTerm,
428429
tag1 = tags.getTagName(index = 0),
429430
tag2 = tags.getTagName(index = 1),
430431
tag3 = tags.getTagName(index = 2),
431-
matchAll = matchAll,
432432
exactMatch = exactMatch,
433433
untaggedOnly = untaggedOnly,
434434
postVisibility = postVisibility,

app/src/test/kotlin/com/fibelatti/pinboard/features/posts/data/PostsDataSourcePinboardApiTest.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,6 @@ class PostsDataSourcePinboardApiTest {
12311231
tag1 = SAMPLE_TAG_1.name,
12321232
tag2 = "",
12331233
tag3 = "",
1234-
matchAll = true,
12351234
exactMatch = false,
12361235
untaggedOnly = false,
12371236
postVisibility = PostVisibility.None,
@@ -1290,7 +1289,6 @@ class PostsDataSourcePinboardApiTest {
12901289
tag1 = "",
12911290
tag2 = "",
12921291
tag3 = "",
1293-
matchAll = true,
12941292
exactMatch = false,
12951293
untaggedOnly = false,
12961294
postVisibility = PostVisibility.None,
@@ -1331,7 +1329,6 @@ class PostsDataSourcePinboardApiTest {
13311329
tag1 = SAMPLE_TAG_1.name,
13321330
tag2 = "",
13331331
tag3 = "",
1334-
matchAll = true,
13351332
exactMatch = false,
13361333
untaggedOnly = false,
13371334
postVisibility = PostVisibility.None,
@@ -1372,7 +1369,6 @@ class PostsDataSourcePinboardApiTest {
13721369
tag1 = SAMPLE_TAG_1.name,
13731370
tag2 = SAMPLE_TAG_2.name,
13741371
tag3 = "",
1375-
matchAll = true,
13761372
exactMatch = false,
13771373
untaggedOnly = false,
13781374
postVisibility = PostVisibility.None,
@@ -1413,7 +1409,6 @@ class PostsDataSourcePinboardApiTest {
14131409
tag1 = SAMPLE_TAG_1.name,
14141410
tag2 = SAMPLE_TAG_2.name,
14151411
tag3 = SAMPLE_TAG_3.name,
1416-
matchAll = true,
14171412
exactMatch = false,
14181413
untaggedOnly = false,
14191414
postVisibility = PostVisibility.None,
@@ -1525,7 +1520,6 @@ class PostsDataSourcePinboardApiTest {
15251520
tag1 = "",
15261521
tag2 = "",
15271522
tag3 = "",
1528-
matchAll = true,
15291523
exactMatch = false,
15301524
untaggedOnly = false,
15311525
postVisibility = PostVisibility.None,
@@ -1574,7 +1568,6 @@ class PostsDataSourcePinboardApiTest {
15741568
tag1 = SAMPLE_TAG_1.name,
15751569
tag2 = "",
15761570
tag3 = "",
1577-
matchAll = true,
15781571
exactMatch = false,
15791572
untaggedOnly = false,
15801573
postVisibility = PostVisibility.None,
@@ -1623,7 +1616,6 @@ class PostsDataSourcePinboardApiTest {
16231616
tag1 = SAMPLE_TAG_1.name,
16241617
tag2 = SAMPLE_TAG_2.name,
16251618
tag3 = "",
1626-
matchAll = true,
16271619
exactMatch = false,
16281620
untaggedOnly = false,
16291621
postVisibility = PostVisibility.None,
@@ -1672,7 +1664,6 @@ class PostsDataSourcePinboardApiTest {
16721664
tag1 = SAMPLE_TAG_1.name,
16731665
tag2 = SAMPLE_TAG_2.name,
16741666
tag3 = SAMPLE_TAG_3.name,
1675-
matchAll = true,
16761667
exactMatch = false,
16771668
untaggedOnly = false,
16781669
postVisibility = PostVisibility.None,

0 commit comments

Comments
 (0)