aboutsummaryrefslogtreecommitdiff
path: root/internal/dbstore/sql/cpe-search-title.sql
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-05 02:34:41 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-05 02:34:41 -0500
commit425d2a2aa3e3d4ebb1cbf60982ef7a699dd79d97 (patch)
tree68cf04eaf99ae83f678399971a44458ad295d6fe /internal/dbstore/sql/cpe-search-title.sql
parent4f21e2b639f7d66062f0340793626db962ebb9f6 (diff)
downloadcvez-425d2a2aa3e3d4ebb1cbf60982ef7a699dd79d97.tar.bz2
cvez-425d2a2aa3e3d4ebb1cbf60982ef7a699dd79d97.zip
internal/dbstore/sql: populate cpe search sql
Diffstat (limited to 'internal/dbstore/sql/cpe-search-title.sql')
-rw-r--r--internal/dbstore/sql/cpe-search-title.sql44
1 files changed, 44 insertions, 0 deletions
diff --git a/internal/dbstore/sql/cpe-search-title.sql b/internal/dbstore/sql/cpe-search-title.sql
new file mode 100644
index 0000000..7421ffc
--- /dev/null
+++ b/internal/dbstore/sql/cpe-search-title.sql
@@ -0,0 +1,44 @@
+SELECT a.cpe_id,
+ a.cpe23,
+ COALESCE(c.titles, '[]') AS titles,
+ COALESCE(d.refs, '[]') AS refs,
+ b.rank
+
+ -- find matching cpes
+ FROM cpes a
+ JOIN (
+ SELECT cpe_id,
+ MIN(rank) AS rank
+ FROM cpe_fts_titles
+ WHERE cpe_fts_titles MATCH :q
+ GROUP BY cpe_id
+ ) b ON (b.cpe_id = a.cpe_id)
+
+ -- get all titles, grouped by cpe
+ LEFT JOIN (
+ SELECT cpe_id,
+ json_group_array(json_object(
+ 'lang', lang,
+ 'text', val
+ )) AS titles
+
+ FROM cpe_titles
+
+ GROUP BY cpe_id
+ ) c ON (c.cpe_id = b.cpe_id)
+
+ -- get all refs, grouped by cpe
+ LEFT JOIN (
+ SELECT cpe_id,
+ json_group_array(json_object(
+ 'href', href,
+ 'text', val
+ )) AS refs
+
+ FROM cpe_refs
+
+ GROUP BY cpe_id
+ ) d ON (d.cpe_id = b.cpe_id)
+
+ -- order by rank
+ ORDER BY b.rank;