Skip to content

Commit 77dd9c9

Browse files
committed
Add UNION in main search query for better index utilisation.
1 parent 8f55f93 commit 77dd9c9

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

static/sql/queries.sql

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
-- name: search
22
-- FTS5 search with length-based ranking (shorter content ranks higher).
33
-- Exact content matches get extra boost via negative rank adjustment.
4+
-- Supports both FTS matches and direct content_head matches (for multi-word phrases
5+
-- where tokens may be incomplete). Uses UNION for better index utilization.
46
-- $1: lang, $2: raw query, $3: FTS query, $4: status, $5: offset, $6: limit
57
SELECT e.*,
68
JSON_ARRAY_LENGTH(e.content) AS content_length,
@@ -12,8 +14,16 @@ SELECT e.*,
1214
AS rank,
1315
COUNT(*) OVER() AS total
1416
FROM entries e
15-
INNER JOIN entries_fts fts ON fts.rowid = e.id
16-
WHERE entries_fts MATCH $3
17+
WHERE e.id IN (
18+
-- FTS matches
19+
SELECT rowid FROM entries_fts WHERE entries_fts MATCH $3
20+
UNION
21+
-- Direct content_head matches (for multi-word phrases with incomplete tokens)
22+
SELECT id FROM entries
23+
WHERE content_head = LOWER(SUBSTR($2, 1, 20))
24+
AND ($1 = '' OR lang = $1)
25+
AND status != 'disabled'
26+
)
1727
AND EXISTS (SELECT 1 FROM relations r WHERE r.from_id = e.id)
1828
AND ($1 = '' OR e.lang = $1)
1929
AND ($4 = '' OR e.status = $4)

0 commit comments

Comments
 (0)