Skip to content

Commit d526ccb

Browse files
committed
add full text search
1 parent f573035 commit d526ccb

File tree

5 files changed

+131
-9
lines changed

5 files changed

+131
-9
lines changed

examples/official-site/extensions-to-sql.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ SET var = (SELECT * FROM t);
104104
-- now $var contains '1'
105105
```
106106

107-
Temporary table-valued results can be stored in two ways:
107+
Temporary table-valued results can be stored in two ways.
108108

109-
1. Using temporary tables
109+
## Storing large datasets in the database with temporary tables
110110

111111
This is the most efficient method to store large values.
112112
```sql
@@ -122,12 +122,13 @@ INSERT INTO my_temp_table(a, b)
122122
VALUES (1, 2), (3, 4);
123123
```
124124

125-
2. Using JSON
125+
## Storing rich structured data in memory using JSON
126126

127127
This can be more convenient, but should only be used for small values, because data
128128
is copied from the database into SQLPage memory, and to the database again at each use.
129129

130-
You can use the [JSON functions from your database](/blog.sql?post=JSON in SQL%3A A Comprehensive Guide).
130+
You can use the [JSON functions from your database](/blog.sql?post=JSON+in+SQL%3A+A+Comprehensive+Guide).
131+
131132
Here are some examples with SQLite:
132133
```sql
133134
-- CREATE TABLE my_table(a, b);
@@ -151,7 +152,7 @@ See the [functions page](/functions.sql) for more details.
151152

152153
They're either executed before or after the query is run in the database.
153154

154-
### Execution functions *before* sending a query to the database
155+
### Executing functions *before* sending a query to the database
155156

156157
When they don't process results coming from the database:
157158

@@ -161,12 +162,13 @@ SELECT * FROM blog WHERE slug = sqlpage.path()
161162

162163
`sqlpage.path()` will get replaced by the result of the function.
163164

164-
### Execution functions *after* receiving results from the database
165+
### Executing functions *after* receiving results from the database
165166

166167
When they process results coming from the database:
167168

168169
```sql
169-
SELECT sqlpage.read_file_as_text(blog_post_file) AS title FROM blog;
170+
SELECT sqlpage.read_file_as_text(blog_post_file) AS title
171+
FROM blog;
170172
```
171173

172174
The query executed will be:

examples/official-site/extensions-to-sql.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ select 'dynamic' as component, properties FROM example WHERE component = 'shell'
66
-- Article by Matthew Larkin
77
select 'text' as component,
88
sqlpage.read_file_as_text('extensions-to-sql.md') as contents_md,
9-
false as article;
9+
true as article;

examples/official-site/search.sql

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
-- Check for exact matches and redirect if found
2+
set redirect = CASE
3+
WHEN EXISTS (SELECT 1 FROM component WHERE name = $search) THEN sqlpage.link('/component.sql', json_object('component', $search))
4+
WHEN EXISTS (SELECT 1 FROM sqlpage_functions WHERE name = $search) THEN sqlpage.link('/functions.sql', json_object('function', $search))
5+
END
6+
SELECT 'redirect' as component, $redirect as link WHERE $redirect IS NOT NULL;
7+
8+
9+
select 'dynamic' as component, properties FROM example WHERE component = 'shell' LIMIT 1;
10+
11+
SELECT 'form' as component,
12+
'GET' as method,
13+
true as auto_submit,
14+
'Search documentation' as title;
15+
16+
SELECT 'text' as type,
17+
'search' as name,
18+
'' as label,
19+
true as autofocus,
20+
'Search for components, parameters, functions...' as placeholder,
21+
$search as value;
22+
23+
SELECT 'text' as component,
24+
CASE
25+
WHEN $search IS NULL THEN 'Enter a search term above to find documentation about components, parameters, functions, and blog posts.'
26+
WHEN NOT EXISTS (
27+
SELECT 1 FROM documentation_fts
28+
WHERE documentation_fts = $search
29+
) THEN 'No results found for "' || $search || '".'
30+
ELSE NULL
31+
END as contents;
32+
33+
SELECT 'list' as component,
34+
'Search Results' as title,
35+
'No results found for "' || $search || '".' as empty_description
36+
WHERE $search IS NOT NULL;
37+
38+
WITH search_results AS (
39+
SELECT
40+
CASE
41+
WHEN parameter_name IS NOT NULL THEN component_name || ' component: parameter ' || parameter_name
42+
WHEN component_name IS NOT NULL THEN component_name || ' component'
43+
WHEN blog_title IS NOT NULL THEN 'blog: ' || blog_title
44+
WHEN function_parameter_name IS NOT NULL THEN function_name || '(...' || function_parameter_name || '...)'
45+
WHEN function_name IS NOT NULL THEN function_name || '(...)'
46+
END as title,
47+
CASE
48+
WHEN component_description IS NOT NULL THEN component_description
49+
WHEN parameter_description IS NOT NULL THEN parameter_description
50+
WHEN blog_description IS NOT NULL THEN blog_description
51+
WHEN function_description IS NOT NULL THEN function_description
52+
WHEN function_parameter_description IS NOT NULL THEN function_parameter_description
53+
END as description,
54+
CASE
55+
WHEN component_name IS NOT NULL THEN json_object('page', '/component.sql', 'parameters', json_object('component', component_name))
56+
WHEN parameter_name IS NOT NULL THEN json_object('page', '/component.sql', 'parameters', json_object('component', (
57+
SELECT component FROM parameter
58+
WHERE name = parameter_name
59+
LIMIT 1
60+
)))
61+
WHEN blog_title IS NOT NULL THEN json_object('page', '/blog.sql', 'parameters', json_object('post', blog_title))
62+
WHEN function_name IS NOT NULL THEN json_object('page', '/functions.sql', 'parameters', json_object('function', function_name))
63+
WHEN function_parameter_name IS NOT NULL THEN json_object('page', '/functions.sql', 'parameters', json_object('function', (
64+
SELECT function FROM sqlpage_function_parameters
65+
WHERE name = function_parameter_name
66+
LIMIT 1
67+
)))
68+
END as link_data,
69+
rank
70+
FROM documentation_fts
71+
WHERE $search IS NOT NULL
72+
AND documentation_fts = $search
73+
)
74+
SELECT
75+
title,
76+
description,
77+
sqlpage.link(link_data->>'page', link_data->'parameters') as link
78+
FROM search_results
79+
ORDER BY
80+
rank,
81+
CASE
82+
WHEN title LIKE 'component:%' THEN 1
83+
WHEN title LIKE 'parameter:%' THEN 2
84+
WHEN title LIKE 'blog:%' THEN 3
85+
WHEN title LIKE 'function:%' THEN 4
86+
END,
87+
description;

examples/official-site/sqlpage/migrations/01_documentation.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,8 @@ You see the [page layouts demo](./examples/layouts.sql) for a live example of th
12441244
{"link": "/extensions-to-sql", "title": "Extensions to SQL", "icon": "cube-plus"},
12451245
{"link": "/custom_components.sql", "title": "Custom Components", "icon": "puzzle"},
12461246
{"link": "//github.com/sqlpage/SQLPage/blob/main/configuration.md#configuring-sqlpage", "title": "Configuration", "icon": "settings"}
1247-
]}
1247+
]},
1248+
{"title": "Search", "link": "/search"}
12481249
],
12491250
"layout": "boxed",
12501251
"language": "en-US",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
CREATE VIRTUAL TABLE documentation_fts USING fts5(
2+
component_name,
3+
component_description,
4+
parameter_name,
5+
parameter_description,
6+
blog_title,
7+
blog_description,
8+
function_name,
9+
function_description,
10+
function_parameter_name,
11+
function_parameter_description,
12+
component_example_description,
13+
component_example_json
14+
);
15+
16+
INSERT INTO documentation_fts(component_name, component_description)
17+
SELECT name, description FROM component;
18+
19+
INSERT INTO documentation_fts(component_name, parameter_name, parameter_description)
20+
SELECT component, name, description FROM parameter;
21+
22+
INSERT INTO documentation_fts(blog_title, blog_description)
23+
SELECT title, description FROM blog_posts;
24+
25+
INSERT INTO documentation_fts(function_name, function_description)
26+
SELECT name, description_md FROM sqlpage_functions;
27+
28+
INSERT INTO documentation_fts(function_name, function_parameter_name, function_parameter_description)
29+
SELECT function, name, description_md FROM sqlpage_function_parameters;
30+
31+
INSERT INTO documentation_fts(component_name, component_example_description, component_example_json)
32+
SELECT component, description, properties FROM example;

0 commit comments

Comments
 (0)