The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
docs: fts - add javascript snippets
Paul Copplestone1f2abef27f76
diff --git a/web/docs/guides/database/full-text-search.mdx b/web/docs/guides/database/full-text-search.mdxindex d93122317b7..76154176661 100644--- a/web/docs/guides/database/full-text-search.mdx+++ b/web/docs/guides/database/full-text-search.mdx@@ -88,21 +88,64 @@ The `@@` symbol is the "match" symbol for Full Text Search. It returns any matchTake the following example:+<Tabs+defaultValue="SQL"+values={[+ {label: 'SQL', value: 'SQL'},+ {label: 'Javascript', value: 'JS'},+]}>+<TabItem value="SQL">+```sqlselect *from bookswhere name = 'Harry';```+</TabItem>+<TabItem value="JS">++```js+const { data, error } = supabase+ .from('books')+ .select()+ .eq('name', 'Harry')+```++</TabItem>+</Tabs>++The equality symbol above (`=`) is very "strict" on what it matches. In a full text search context, we might want to find all "Harry Potter" books and so we can rewrite theexample above:+<Tabs+defaultValue="SQL"+values={[+ {label: 'SQL', value: 'SQL'},+ {label: 'Javascript', value: 'JS'},+]}>+<TabItem value="SQL">+```sqlselect *from bookswhere to_tsvector(name) @@ to_tsquery('Harry');```+</TabItem>+<TabItem value="JS">++```js+const { data, error } = supabase+ .from('books')+ .select()+ .textSearch('name', `'Harry'`)+```++</TabItem>+</Tabs>+## Basic Full Text Queries### Search a single column@@ -113,6 +156,7 @@ To find all `books` where the `description` contain the word `big`:defaultValue="SQL"values={[{label: 'SQL', value: 'SQL'},+ {label: 'Javascript', value: 'JS'},{label: 'Data', value: 'Data'},]}><TabItem value="SQL">@@ -127,6 +171,16 @@ where@@ to_tsquery('big');```+</TabItem>+<TabItem value="JS">++```js+const { data, error } = supabase+ .from('books')+ .select()+ .textSearch('description', `'big'`)+```+</TabItem><TabItem value="Data">@@ -173,12 +227,13 @@ where### Match all search words-To find all `books` where `description` or `title` contain BOTH of the words `little` and `dream`, we can use the `&` symbol:+To find all `books` where `description` contains BOTH of the words `little` and `big`, we can use the `&` symbol:<TabsdefaultValue="SQL"values={[{label: 'SQL', value: 'SQL'},+ {label: 'Javascript', value: 'JS'},{label: 'Data', value: 'Data'},]}><TabItem value="SQL">@@ -189,8 +244,18 @@ selectfrombookswhere- to_tsvector(description || ' ' || title)- @@ to_tsquery('little & dream'); -- use & for AND in the search query+ to_tsvector(description)+ @@ to_tsquery('little & big'); -- use & for AND in the search query+```++</TabItem>+<TabItem value="JS">++```js+const { data, error } = supabase+ .from('books')+ .select()+ .textSearch('description', `'little' & 'big'`)```</TabItem>@@ -203,16 +268,15 @@ where</TabItem></Tabs>-Notice how searching for `dream` includes results with the word `dreams` (or `dreaming`, `dreamed`, etc).-### Match any search words-To find all `books` where `description` or `title` contain ANY of the words `little` or `dream`, use the `|` symbol:+To find all `books` where `description` contain ANY of the words `little` or `big`, use the `|` symbol:<TabsdefaultValue="SQL"values={[{label: 'SQL', value: 'SQL'},+ {label: 'Javascript', value: 'JS'},{label: 'Data', value: 'Data'},]}><TabItem value="SQL">@@ -223,8 +287,18 @@ selectfrombookswhere- to_tsvector(description || ' ' || title)- @@ to_tsquery('little | dream'); -- use | for OR in the search query+ to_tsvector(description)+ @@ to_tsquery('little | big'); -- use | for OR in the search query+```++</TabItem>+<TabItem value="JS">++```js+const { data, error } = supabase+ .from('books')+ .select()+ .textSearch('description', `'little' | 'big'`)```</TabItem>@@ -238,6 +312,8 @@ where</TabItem></Tabs>+Notice how searching for `big` includes results with the word `bigger` (or `biggest`, etc).+## Creating IndexesNow that we have Full Text Search working, let's create an `index`. This will allow Postgres to "build" the documents pre-emptively so that they@@ -284,15 +360,15 @@ from books;</TabItem></Tabs>-### Searching using the new column+### Search using the new columnNow that we've created and populated our index, we can search it using the same techniques as before:-<TabsdefaultValue="SQL"values={[{label: 'SQL', value: 'SQL'},+ {label: 'Javascript', value: 'JS'},{label: 'Data', value: 'Data'},]}><TabItem value="SQL">@@ -303,7 +379,17 @@ selectfrombookswhere- fts @@ to_tsquery('little & dream');+ fts @@ to_tsquery('little & big');+```++</TabItem>+<TabItem value="JS">++```js+const { data, error } = supabase+ .from('books')+ .select()+ .textSearch('fts', `'little' & 'big'`)```</TabItem>@@ -326,17 +412,48 @@ to learn about additional query operators you can use to do more advanced `fullThe proximity symbol is useful for searching for terms that are a certain "distance" apart.For example, to find the phrase `big dreams`, where the a match for "big" is followed immediately by a match for "dreams":++<Tabs+defaultValue="SQL"+values={[+ {label: 'SQL', value: 'SQL'},+ {label: 'Javascript', value: 'JS'},+]}>+<TabItem value="SQL">+```sqlselect*frombookswhere- to_tsvector(description) @@ to_tsquery('big <-> dreams');+ to_tsvector(description) @@ to_tsquery('little <-> big');+```++</TabItem>+<TabItem value="JS">++```js+const { data, error } = supabase+ .from('books')+ .select()+ .textSearch('description', `'little' <-> 'big'`)```+</TabItem>+</Tabs>++We can also use the `<->` to find words within a certain distance of eachother. For example to find `year` and `school` within 2 words of each other:+<Tabs+defaultValue="SQL"+values={[+ {label: 'SQL', value: 'SQL'},+ {label: 'Javascript', value: 'JS'},+]}>+<TabItem value="SQL">+```sqlselect*@@ -346,11 +463,33 @@ whereto_tsvector(description) @@ to_tsquery('year <2> school');```+</TabItem>+<TabItem value="JS">++```js+const { data, error } = supabase+ .from('books')+ .select()+ .textSearch('description', `'year' <2> 'school'`)+```++</TabItem>+</Tabs>++### Negation: `!`The negation symbol can be used to find phrases which _don't_ contain a search term.For example, to find records that have the word `big` but not `little`:+<Tabs+defaultValue="SQL"+values={[+ {label: 'SQL', value: 'SQL'},+ {label: 'Javascript', value: 'JS'},+]}>+<TabItem value="SQL">+```sqlselect*@@ -360,6 +499,19 @@ whereto_tsvector(description) @@ to_tsquery('big & !little');```+</TabItem>+<TabItem value="JS">++```js+const { data, error } = supabase+ .from('books')+ .select()+ .textSearch('description', `'big' & !'little'`)+```++</TabItem>+</Tabs>+## Resources