From 1f2abef27f76e37a293cbf5970d43f013299dc95 Mon Sep 17 00:00:00 2001 From: Paul Copplestone Date: Thu, 1 Jul 2021 14:28:00 +0800 Subject: [PATCH] docs: fts - add javascript snippets --- web/docs/guides/database/full-text-search.mdx | 176 ++++++++++++++++-- 1 file changed, 164 insertions(+), 12 deletions(-) diff --git a/web/docs/guides/database/full-text-search.mdx b/web/docs/guides/database/full-text-search.mdx index 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 match Take the following example: + + + ```sql select * from books where name = 'Harry'; ``` + + + +```js +const { data, error } = supabase + .from('books') + .select() + .eq('name', 'Harry') +``` + + + + + 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 the example above: + + + ```sql select * from books where to_tsvector(name) @@ to_tsquery('Harry'); ``` + + + +```js +const { data, error } = supabase + .from('books') + .select() + .textSearch('name', `'Harry'`) +``` + + + + ## 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'}, ]}> @@ -127,6 +171,16 @@ where @@ to_tsquery('big'); ``` + + + +```js +const { data, error } = supabase + .from('books') + .select() + .textSearch('description', `'big'`) +``` + @@ -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: @@ -189,8 +244,18 @@ select from books where - 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 +``` + + + + +```js +const { data, error } = supabase + .from('books') + .select() + .textSearch('description', `'little' & 'big'`) ``` @@ -203,16 +268,15 @@ where -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: @@ -223,8 +287,18 @@ select from books where - 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 +``` + + + + +```js +const { data, error } = supabase + .from('books') + .select() + .textSearch('description', `'little' | 'big'`) ``` @@ -238,6 +312,8 @@ where +Notice how searching for `big` includes results with the word `bigger` (or `biggest`, etc). + ## Creating Indexes Now 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; -### Searching using the new column +### Search using the new column Now that we've created and populated our index, we can search it using the same techniques as before: - @@ -303,7 +379,17 @@ select from books where - fts @@ to_tsquery('little & dream'); + fts @@ to_tsquery('little & big'); +``` + + + + +```js +const { data, error } = supabase + .from('books') + .select() + .textSearch('fts', `'little' & 'big'`) ``` @@ -326,17 +412,48 @@ to learn about additional query operators you can use to do more advanced `full The 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": + + + + ```sql select * from books where - to_tsvector(description) @@ to_tsquery('big <-> dreams'); + to_tsvector(description) @@ to_tsquery('little <-> big'); ``` + + + +```js +const { data, error } = supabase + .from('books') + .select() + .textSearch('description', `'little' <-> 'big'`) +``` + + + + + 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: + + + ```sql select * @@ -346,11 +463,33 @@ where to_tsvector(description) @@ to_tsquery('year <2> school'); ``` + + + +```js +const { data, error } = supabase + .from('books') + .select() + .textSearch('description', `'year' <2> 'school'`) +``` + + + + + ### 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`: + + + ```sql select * @@ -360,6 +499,19 @@ where to_tsvector(description) @@ to_tsquery('big & !little'); ``` + + + +```js +const { data, error } = supabase + .from('books') + .select() + .textSearch('description', `'big' & !'little'`) +``` + + + + ## Resources