Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

added Full Text Search guide

Mark Burggrafba06d61b8794
diff --git a/web/docs/guides/database/full-text-search.mdx b/web/docs/guides/database/full-text-search.mdx
new file mode 100644
index 00000000000..cb61318c4b0
--- /dev/null
+++ b/web/docs/guides/database/full-text-search.mdx
@@ -0,0 +1,316 @@
+---
+id: full-text-search
+title: "Full Text Search"
+description: How to use full text search in PostgreSQL.
+---
+
+import ExtensionsComponent from '../../../../web/src/components/Extensions'
+import Tabs from '@theme/Tabs';
+import TabItem from '@theme/TabItem';
+
+
+`PostgreSQL` has built-in functions to handle flexible and performant `Full Text Search` queries.
+
+
+## Overview: The Data
+
+Let's query the following data from the following table using `Full Text Search` functions:
+
+
+<Tabs
+defaultValue="Data"
+values={[
+ {label: 'SQL', value: 'SQL'},
+ {label: 'Data', value: 'Data'},
+]}>
+
+<TabItem value="SQL">
+
+```sql
+CREATE TABLE IF NOT EXISTS books (
+ id SERIAL PRIMARY KEY,
+ title TEXT,
+ author TEXT,
+ description TEXT
+);
+INSERT INTO books (title, author, description) VALUES
+('The Poky Little Puppy','Janette Sebring Lowrey','Puppy is slower than other, bigger animals.'),
+('The Tale of Peter Rabbit','Beatrix Potter','Rabbit eats some vegetables.'),
+('Tootle','Gertrude Crampton','Little toy train has big dreams.'),
+('Green Eggs and Ham','Dr. Seuss','Sam has changing food preferences and eats unusally colored food.'),
+('Harry Potter and the Goblet of Fire','J.K. Rowling','Fourth year of school starts, big drama ensues.');
+SELECT * FROM books;
+```
+
+</TabItem>
+<TabItem value="Data">
+
+| id | title | author | description |
+| ---- | ---- | ---- | ----------- |
+| 1 | The Poky Little Puppy | Janette Sebring Lowrey | Puppy is slower than other, bigger animals. |
+| 2 | The Tale of Peter Rabbit | Beatrix Potter | Rabbit eats some vegetables. |
+| 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. |
+| 4 | Green Eggs and Ham | Dr. Seuss | Sam has changing food preferences and eats unusally colored food. |
+| 5 | Harry Potter and the Goblet of Fire | J.K. Rowling | Fourth year of school starts, big drama ensues. |
+
+</TabItem>
+
+</Tabs>
+
+## Full Text Search Functions
+
+`PostgreSQL` uses the following functions to perform `full text search` queries:
+
+- `to_tsvector()`
+- `to_tsquery()`
+
+## Basic Full Text Queries
+
+### Search a single column for a single word
+
+To find all `books` where the `description` contain the word `big`:
+
+<Tabs
+defaultValue="SQL"
+values={[
+ {label: 'SQL', value: 'SQL'},
+ {label: 'Data', value: 'Data'},
+]}>
+<TabItem value="SQL">
+
+```sql
+SELECT * FROM books
+WHERE to_tsvector(description)
+@@ to_tsquery('big');
+```
+
+</TabItem>
+<TabItem value="Data">
+
+| id | title | author | description |
+| -- | ----------------------------------- | ----------------- | ----------------------------------------------- |
+| 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. |
+| 5 | Harry Potter and the Goblet of Fire | J.K. Rowling | Fourth year of school starts, big drama ensues. |
+
+</TabItem>
+</Tabs>
+
+### Search multiple columns for a single word
+
+To find all `books` where `description` or `title` contain the word `little`:
+
+<Tabs
+defaultValue="SQL"
+values={[
+ {label: 'SQL', value: 'SQL'},
+ {label: 'Data', value: 'Data'},
+]}>
+<TabItem value="SQL">
+
+```sql
+SELECT * FROM books
+WHERE to_tsvector(description || ' ' || title) -- concat columns, but be sure to include a space to separate them!
+@@ to_tsquery('little');
+```
+
+</TabItem>
+<TabItem value="Data">
+
+| id | title | author | description |
+| -- | --------------------- | ---------------------- | ------------------------------------------- |
+| 1 | The Poky Little Puppy | Janette Sebring Lowrey | Puppy is slower than other, bigger animals. |
+| 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. |
+
+</TabItem>
+</Tabs>
+
+### Search multiple columns for multiple words (`AND`: contains both words)
+
+To find all `books` where (`description` or `title`) contain the word (`little` and `dream`):
+
+<Tabs
+defaultValue="SQL"
+values={[
+ {label: 'SQL', value: 'SQL'},
+ {label: 'Data', value: 'Data'},
+]}>
+<TabItem value="SQL">
+
+```sql
+SELECT * FROM books
+WHERE to_tsvector(description || ' ' || title)
+@@ to_tsquery('little & dream'); -- use & for AND in the search query
+```
+
+</TabItem>
+<TabItem value="Data">
+
+| id | title | author | description |
+| -- | ------ | ----------------- | -------------------------------- |
+| 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. |
+
+</TabItem>
+</Tabs>
+
+Notice how searching for `dream` includes results with the word `dreams` (or `dreaming`, `dreamed`, etc.)
+
+### Search multiple columns for multiple words (`OR`: contains any of the search words)
+
+To find all `books` where (`description` or `title`) contain the word (`little` or `dream`):
+
+<Tabs
+defaultValue="SQL"
+values={[
+ {label: 'SQL', value: 'SQL'},
+ {label: 'Data', value: 'Data'},
+]}>
+<TabItem value="SQL">
+
+```sql
+SELECT * FROM books
+WHERE to_tsvector(description || ' ' || title)
+@@ to_tsquery('little | dream'); -- use | for AND in the search query
+```
+
+</TabItem>
+<TabItem value="Data">
+
+| id | title | author | description |
+| -- | --------------------- | ---------------------- | ------------------------------------------- |
+| 1 | The Poky Little Puppy | Janette Sebring Lowrey | Puppy is slower than other, bigger animals. |
+| 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. |
+
+</TabItem>
+</Tabs>
+
+## Creating Indexes to Increase Performance
+
+Now that we have `Full Text Search` working, let's create an `index` that will speed things up and allow this to scale to millions of records and users.
+
+### Add a column to hold the tsvector (full text search) data
+<Tabs
+defaultValue="SQL"
+values={[
+ {label: 'SQL', value: 'SQL'},
+ {label: 'Data', value: 'Data'},
+]}>
+<TabItem value="SQL">
+
+```sql
+ALTER TABLE books ADD COLUMN fts tsvector; -- we'll call our new column "fts" (full text search)
+SELECT id, fts FROM books;
+```
+
+</TabItem>
+<TabItem value="Data">
+
+| id | fts |
+| -- | --- |
+| 1 | null |
+| 2 | null |
+| 3 | null |
+| 4 | null |
+| 5 | null |
+
+</TabItem>
+</Tabs>
+
+### Populate the `fts` column with data using `to_tsvector()`
+
+<Tabs
+defaultValue="SQL"
+values={[
+ {label: 'SQL', value: 'SQL'},
+ {label: 'Data', value: 'Data'},
+]}>
+<TabItem value="SQL">
+
+```sql
+UPDATE books SET fts = (to_tsvector(description || ' ' || title));
+-- the `fts` column will now always (and only) allow us to search 'description' and 'title'
+SELECT id, fts FROM books;
+```
+
+</TabItem>
+<TabItem value="Data">
+
+(The data stored in the `fts` column is interesting, but we'll never interact with it directly.)
+
+| id | fts |
+| -- | --------------------------------------------------------------------------------------------------------------- |
+| 1 | 'anim':7 'bigger':6 'littl':10 'poki':9 'puppi':1,11 'slower':3 |
+| 2 | 'eat':2 'peter':8 'rabbit':1,9 'tale':6 'veget':4 |
+| 3 | 'big':5 'dream':6 'littl':1 'tootl':7 'toy':2 'train':3 |
+| 4 | 'chang':3 'color':9 'eat':7 'egg':12 'food':4,10 'green':11 'ham':14 'prefer':5 'sam':1 'unus':8 |
+| 5 | 'big':6 'drama':7 'ensu':8 'fire':15 'fourth':1 'goblet':13 'harri':9 'potter':10 'school':4 'start':5 'year':2 |
+
+</TabItem>
+</Tabs>
+
+### Searching Using the New Index
+
+Now that we've created and populated our index, we can search it using the same techniques as before:
+
+
+<Tabs
+defaultValue="SQL"
+values={[
+ {label: 'SQL', value: 'SQL'},
+ {label: 'Data', value: 'Data'},
+]}>
+<TabItem value="SQL">
+
+```sql
+SELECT * FROM books
+WHERE fts -- now we just search the fts column instead of calling to_tsvector()
+@@ to_tsquery('little & dream');
+```
+
+</TabItem>
+<TabItem value="Data">
+
+| id | title | author | description | fts |
+| -- | ------ | ----------------- | -------------------------------- | ------------------------------------------------------- |
+| 3 | Tootle | Gertrude Crampton | Little toy train has big dreams. | 'big':5 'dream':6 'littl':1 'tootl':7 'toy':2 'train':3 |
+
+</TabItem>
+</Tabs>
+
+### Keeping the Index Up-to-Date
+
+It's important that whenever your data changes, you update your index (recalculate your `fts` field).
+If we add a new `book` to the table, or if we change the `title` or `description` field for any record, we need to calculate/recalculate the `fts` column.
+
+There are two ways to do this:
+
+1. **Manually** add this to your database code that does any inserts or updates to the table.
+2. Create a **trigger** on your table to handle this chore automatically.
+
+
+## Bonus Query Operators
+
+Visit [PostgreSQL: Text Search Functions and Operators](https://www.postgresql.org/docs/12/functions-textsearch.html)
+to learn about additional query operators you can use to do more advanced `full text queries`, such as:
+
+Find the `exact phrase`: `big dreams`:
+
+```@@ to_tsquery('big <-> dreams')```
+
+`Proximity`: find `year` and `school` within 2 words of each other:
+
+```@@ to_tsquery('year <2> school')```
+
+`Negate` a query term (find records that have the word `big` but `not little`):
+
+```@@ to_tsquery('big & !little');```
+
+`Mix and Match` query terms:
+
+```@@ to_tsquery('(food & eat) | (eat & vegetable) & !school');```
+
+## Resources
+
+* [PostgreSQL: Text Search Functions and Operators](https://www.postgresql.org/docs/12/functions-textsearch.html)
+
+
+
diff --git a/web/sidebars.js b/web/sidebars.js
index 3cbfd04b22d..f8517298f1f 100755
--- a/web/sidebars.js
+++ b/web/sidebars.js
@@ -110,6 +110,7 @@ module.exports = {
label: 'Database',
collapsed: false,
items: [
+ 'guides/database/full-text-search',
'guides/database/arrays',
'guides/database/json',
'guides/database/password-reset',