|
| 1 | +-- testing sql found in https://supabase.com/docs/guides/database/full-text-search |
| 2 | +create table books ( |
| 3 | + id serial primary key, |
| 4 | + title text, |
| 5 | + author text, |
| 6 | + description text |
| 7 | +); |
| 8 | +insert into books |
| 9 | + (title, author, description) |
| 10 | +values |
| 11 | + ( |
| 12 | + 'The Poky Little Puppy', |
| 13 | + 'Janette Sebring Lowrey', |
| 14 | + 'Puppy is slower than other, bigger animals.' |
| 15 | + ), |
| 16 | + ('The Tale of Peter Rabbit', 'Beatrix Potter', 'Rabbit eats some vegetables.'), |
| 17 | + ('Tootle', 'Gertrude Crampton', 'Little toy train has big dreams.'), |
| 18 | + ( |
| 19 | + 'Green Eggs and Ham', |
| 20 | + 'Dr. Seuss', |
| 21 | + 'Sam has changing food preferences and eats unusually colored food.' |
| 22 | + ), |
| 23 | + ( |
| 24 | + 'Harry Potter and the Goblet of Fire', |
| 25 | + 'J.K. Rowling', |
| 26 | + 'Fourth year of school starts, big drama ensues.' |
| 27 | + ); |
| 28 | +select to_tsvector('green eggs and ham'); |
| 29 | + to_tsvector |
| 30 | +--------------------------- |
| 31 | + 'egg':2 'green':1 'ham':4 |
| 32 | +(1 row) |
| 33 | + |
| 34 | +select to_tsvector('english', 'green eggs and ham'); |
| 35 | + to_tsvector |
| 36 | +--------------------------- |
| 37 | + 'egg':2 'green':1 'ham':4 |
| 38 | +(1 row) |
| 39 | + |
| 40 | +select * |
| 41 | +from books |
| 42 | +where title = 'Harry'; |
| 43 | + id | title | author | description |
| 44 | +----+-------+--------+------------- |
| 45 | +(0 rows) |
| 46 | + |
| 47 | +select * |
| 48 | +from books |
| 49 | +where to_tsvector(title) @@ to_tsquery('Harry'); |
| 50 | + id | title | author | description |
| 51 | +----+-------------------------------------+--------------+------------------------------------------------- |
| 52 | + 5 | Harry Potter and the Goblet of Fire | J.K. Rowling | Fourth year of school starts, big drama ensues. |
| 53 | +(1 row) |
| 54 | + |
| 55 | +select |
| 56 | + * |
| 57 | +from |
| 58 | + books |
| 59 | +where |
| 60 | + to_tsvector(description) |
| 61 | + @@ to_tsquery('big'); |
| 62 | + id | title | author | description |
| 63 | +----+-------------------------------------+-------------------+------------------------------------------------- |
| 64 | + 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. |
| 65 | + 5 | Harry Potter and the Goblet of Fire | J.K. Rowling | Fourth year of school starts, big drama ensues. |
| 66 | +(2 rows) |
| 67 | + |
| 68 | +select |
| 69 | + * |
| 70 | +from |
| 71 | + books |
| 72 | +where |
| 73 | + to_tsvector(description || ' ' || title) |
| 74 | + @@ to_tsquery('little'); |
| 75 | + id | title | author | description |
| 76 | +----+-----------------------+------------------------+--------------------------------------------- |
| 77 | + 1 | The Poky Little Puppy | Janette Sebring Lowrey | Puppy is slower than other, bigger animals. |
| 78 | + 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. |
| 79 | +(2 rows) |
| 80 | + |
| 81 | +create function title_description(books) returns text as $$ |
| 82 | + select $1.title || ' ' || $1.description; |
| 83 | +$$ language sql immutable; |
| 84 | +select |
| 85 | + * |
| 86 | +from |
| 87 | + books |
| 88 | +where |
| 89 | + to_tsvector(title_description(books.*)) |
| 90 | + @@ to_tsquery('little'); |
| 91 | + id | title | author | description |
| 92 | +----+-----------------------+------------------------+--------------------------------------------- |
| 93 | + 1 | The Poky Little Puppy | Janette Sebring Lowrey | Puppy is slower than other, bigger animals. |
| 94 | + 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. |
| 95 | +(2 rows) |
| 96 | + |
| 97 | +select |
| 98 | + * |
| 99 | +from |
| 100 | + books |
| 101 | +where |
| 102 | + to_tsvector(description) |
| 103 | + @@ to_tsquery('little & big'); |
| 104 | + id | title | author | description |
| 105 | +----+--------+-------------------+---------------------------------- |
| 106 | + 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. |
| 107 | +(1 row) |
| 108 | + |
| 109 | +select |
| 110 | + * |
| 111 | +from |
| 112 | + books |
| 113 | +where |
| 114 | + to_tsvector(description) |
| 115 | + @@ to_tsquery('little | big'); |
| 116 | + id | title | author | description |
| 117 | +----+-------------------------------------+-------------------+------------------------------------------------- |
| 118 | + 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. |
| 119 | + 5 | Harry Potter and the Goblet of Fire | J.K. Rowling | Fourth year of school starts, big drama ensues. |
| 120 | +(2 rows) |
| 121 | + |
| 122 | +select title from books where to_tsvector(title) @@ to_tsquery('Lit:*'); |
| 123 | + title |
| 124 | +----------------------- |
| 125 | + The Poky Little Puppy |
| 126 | +(1 row) |
| 127 | + |
| 128 | +create or replace function search_books_by_title_prefix(prefix text) |
| 129 | +returns setof books AS $$ |
| 130 | +begin |
| 131 | + return query |
| 132 | + select * from books where to_tsvector('english', title) @@ to_tsquery(prefix || ':*'); |
| 133 | +end; |
| 134 | +$$ language plpgsql; |
| 135 | +select * from search_books_by_title_prefix('Lit'); |
| 136 | + id | title | author | description |
| 137 | +----+-----------------------+------------------------+--------------------------------------------- |
| 138 | + 1 | The Poky Little Puppy | Janette Sebring Lowrey | Puppy is slower than other, bigger animals. |
| 139 | +(1 row) |
| 140 | + |
| 141 | +select * from search_books_by_title_prefix('Little+Puppy'); |
| 142 | + id | title | author | description |
| 143 | +----+-----------------------+------------------------+--------------------------------------------- |
| 144 | + 1 | The Poky Little Puppy | Janette Sebring Lowrey | Puppy is slower than other, bigger animals. |
| 145 | +(1 row) |
| 146 | + |
| 147 | +alter table |
| 148 | + books |
| 149 | +add column |
| 150 | + fts tsvector generated always as (to_tsvector('english', description || ' ' || title)) stored; |
| 151 | +create index books_fts on books using gin (fts); |
| 152 | +select id, fts |
| 153 | +from books; |
| 154 | + id | fts |
| 155 | +----+----------------------------------------------------------------------------------------------------------------- |
| 156 | + 1 | 'anim':7 'bigger':6 'littl':10 'poki':9 'puppi':1,11 'slower':3 |
| 157 | + 2 | 'eat':2 'peter':8 'rabbit':1,9 'tale':6 'veget':4 |
| 158 | + 3 | 'big':5 'dream':6 'littl':1 'tootl':7 'toy':2 'train':3 |
| 159 | + 4 | 'chang':3 'color':9 'eat':7 'egg':12 'food':4,10 'green':11 'ham':14 'prefer':5 'sam':1 'unusu':8 |
| 160 | + 5 | 'big':6 'drama':7 'ensu':8 'fire':15 'fourth':1 'goblet':13 'harri':9 'potter':10 'school':4 'start':5 'year':2 |
| 161 | +(5 rows) |
| 162 | + |
| 163 | +select |
| 164 | + * |
| 165 | +from |
| 166 | + books |
| 167 | +where |
| 168 | + fts @@ to_tsquery('little & big'); |
| 169 | + id | title | author | description | fts |
| 170 | +----+--------+-------------------+----------------------------------+--------------------------------------------------------- |
| 171 | + 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. | 'big':5 'dream':6 'littl':1 'tootl':7 'toy':2 'train':3 |
| 172 | +(1 row) |
| 173 | + |
| 174 | +select |
| 175 | + * |
| 176 | +from |
| 177 | + books |
| 178 | +where |
| 179 | + to_tsvector(description) @@ to_tsquery('big <-> dreams'); |
| 180 | + id | title | author | description | fts |
| 181 | +----+--------+-------------------+----------------------------------+--------------------------------------------------------- |
| 182 | + 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. | 'big':5 'dream':6 'littl':1 'tootl':7 'toy':2 'train':3 |
| 183 | +(1 row) |
| 184 | + |
| 185 | +select |
| 186 | + * |
| 187 | +from |
| 188 | + books |
| 189 | +where |
| 190 | + to_tsvector(description) @@ to_tsquery('year <2> school'); |
| 191 | + id | title | author | description | fts |
| 192 | +----+-------------------------------------+--------------+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------- |
| 193 | + 5 | Harry Potter and the Goblet of Fire | J.K. Rowling | Fourth year of school starts, big drama ensues. | 'big':6 'drama':7 'ensu':8 'fire':15 'fourth':1 'goblet':13 'harri':9 'potter':10 'school':4 'start':5 'year':2 |
| 194 | +(1 row) |
| 195 | + |
| 196 | +select |
| 197 | + * |
| 198 | +from |
| 199 | + books |
| 200 | +where |
| 201 | + to_tsvector(description) @@ to_tsquery('big & !little'); |
| 202 | + id | title | author | description | fts |
| 203 | +----+-------------------------------------+--------------+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------- |
| 204 | + 5 | Harry Potter and the Goblet of Fire | J.K. Rowling | Fourth year of school starts, big drama ensues. | 'big':6 'drama':7 'ensu':8 'fire':15 'fourth':1 'goblet':13 'harri':9 'potter':10 'school':4 'start':5 'year':2 |
| 205 | +(1 row) |
| 206 | + |
| 207 | +select |
| 208 | + * |
| 209 | +from |
| 210 | + books |
| 211 | +where |
| 212 | + to_tsvector(title) @@ to_tsquery('harry & potter'); |
| 213 | + id | title | author | description | fts |
| 214 | +----+-------------------------------------+--------------+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------- |
| 215 | + 5 | Harry Potter and the Goblet of Fire | J.K. Rowling | Fourth year of school starts, big drama ensues. | 'big':6 'drama':7 'ensu':8 'fire':15 'fourth':1 'goblet':13 'harri':9 'potter':10 'school':4 'start':5 'year':2 |
| 216 | +(1 row) |
| 217 | + |
| 218 | +select |
| 219 | + * |
| 220 | +from |
| 221 | + books |
| 222 | +where |
| 223 | + to_tsvector(description) @@ to_tsquery('food & !egg'); |
| 224 | + id | title | author | description | fts |
| 225 | +----+--------------------+-----------+--------------------------------------------------------------------+--------------------------------------------------------------------------------------------------- |
| 226 | + 4 | Green Eggs and Ham | Dr. Seuss | Sam has changing food preferences and eats unusually colored food. | 'chang':3 'color':9 'eat':7 'egg':12 'food':4,10 'green':11 'ham':14 'prefer':5 'sam':1 'unusu':8 |
| 227 | +(1 row) |
| 228 | + |
| 229 | +select |
| 230 | + * |
| 231 | +from |
| 232 | + books |
| 233 | +where |
| 234 | + to_tsvector(title || ' ' || description) @@ to_tsquery('train & toy'); |
| 235 | + id | title | author | description | fts |
| 236 | +----+--------+-------------------+----------------------------------+--------------------------------------------------------- |
| 237 | + 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. | 'big':5 'dream':6 'littl':1 'tootl':7 'toy':2 'train':3 |
| 238 | +(1 row) |
| 239 | + |
| 240 | +select |
| 241 | + * |
| 242 | +from |
| 243 | + books |
| 244 | +where |
| 245 | + fts @@ to_tsquery('puppy & slow'); |
| 246 | + id | title | author | description | fts |
| 247 | +----+-------+--------+-------------+----- |
| 248 | +(0 rows) |
| 249 | + |
| 250 | +select |
| 251 | + * |
| 252 | +from |
| 253 | + books |
| 254 | +where |
| 255 | + fts @@ to_tsquery('rabbit | peter'); |
| 256 | + id | title | author | description | fts |
| 257 | +----+--------------------------+----------------+------------------------------+--------------------------------------------------- |
| 258 | + 2 | The Tale of Peter Rabbit | Beatrix Potter | Rabbit eats some vegetables. | 'eat':2 'peter':8 'rabbit':1,9 'tale':6 'veget':4 |
| 259 | +(1 row) |
| 260 | + |
| 261 | +select |
| 262 | + * |
| 263 | +from |
| 264 | + books |
| 265 | +where |
| 266 | + fts @@ to_tsquery('harry <-> potter'); |
| 267 | + id | title | author | description | fts |
| 268 | +----+-------------------------------------+--------------+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------- |
| 269 | + 5 | Harry Potter and the Goblet of Fire | J.K. Rowling | Fourth year of school starts, big drama ensues. | 'big':6 'drama':7 'ensu':8 'fire':15 'fourth':1 'goblet':13 'harri':9 'potter':10 'school':4 'start':5 'year':2 |
| 270 | +(1 row) |
| 271 | + |
| 272 | +select |
| 273 | + * |
| 274 | +from |
| 275 | + books |
| 276 | +where |
| 277 | + fts @@ to_tsquery('fourth <3> year'); |
| 278 | + id | title | author | description | fts |
| 279 | +----+-------+--------+-------------+----- |
| 280 | +(0 rows) |
| 281 | + |
| 282 | +select |
| 283 | + * |
| 284 | +from |
| 285 | + books |
| 286 | +where |
| 287 | + fts @@ to_tsquery('big & !drama'); |
| 288 | + id | title | author | description | fts |
| 289 | +----+--------+-------------------+----------------------------------+--------------------------------------------------------- |
| 290 | + 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. | 'big':5 'dream':6 'littl':1 'tootl':7 'toy':2 'train':3 |
| 291 | +(1 row) |
| 292 | + |
| 293 | +drop function search_books_by_title_prefix(text); |
| 294 | +drop function title_description(books); |
| 295 | +drop table books; |
| 296 | + |
0 commit comments