EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Full Text Search
 

PostgreSQL Full Text Search

Updated May 22, 2023

PostgreSQL Full Text Search

 

 

Introduction to PostgreSQL Full Text Search

The PostgreSQL helped us find the Record and the document; the document included text columns and row along with Metadata. The document consists of data, URL, and Title. Search speed and search accuracy is the main factor of PostSQL search. Basically, we use the LIKE expression for find or search purposes but require an exact match, we have another path for search is trigrams, it is applicable for spelling mistakes or inexact matches depending on the similarity of the Word, but this isn’t easy to search multiple words. So the best option to avoid this limitation is PostgreSQL Search. It provides a search with a large document with the help of natural language. In this topic, we will learn about PostgreSQL Full Text Search.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Full-Text Search Methodologies

In PostgreSQL, we use tsvector data type for full-text search, tsvector create lexemes. Let’s see how tsvector is working. Mainly tsvector has two functions.

1. to_tsvector

It creates a list of tokens, where t stands for text, and s stands for search. We create optimized searching with the help of tsvector, in which we add a column in a table to save the index. We can do a fast search with the help of tsvector but remember, one thinks data should be up to date.

Syntax:

Select                to_tsvector               (‘Title’,                         ‘document’);

This is a simple syntax of to_tsvector in which that to_tsvector is the data type, the Title is language, and the document is search text. After execution of the above syntax, we got lexemes.

Examples

Select to_tsvector('English', 'This is my old company, and this is company  very good' );

Illustrate the end result of the above declaration by using the use of the following snapshot.

PostgreSQL Full Text Search output 1

In the above Snapshot in which the phrase takes place, however, two instances appear as soon as their unique position.

select to_tsvector('The white dog jumped over the lazy cat ');

Illustrate the end result of the above declaration by using the use of the following snapshot.

PostgreSQL Full Text Search output 2

The above result returns a vector; every token is a lexeme with its position in the document, and the article (the) is removed.

2. to_tsquery

This is a very interesting function of searching. It is used to search for specific Words in a document. Accepts the document created by to_tsvector. It uses the @@ operator for search purpose.

Syntax:

Select              to_tsvector                (‘document’)              @@     to_tsquery                 (‘search word’);

Above syntax to_tsvector is the data type; a document is text and searches Word for a specific search.

Examples

select to_tsvector(‘The white dog jumped over the lazy cat ‘) @@ to_tsquery(‘cat’);

Illustrate the end result of the above declaration by using the use of the following snapshot

PostgreSQL Full Text Search output 3

It shows the result is true because the cat word present in the document

In the same example, we perform another search of cats

select to_tsvector(' The white dog jumped over the lazy cat ') @@ to_tsquery('cats');

Illustrate the end result of the above declaration by using the use of the following snapshot

PostgreSQL Full Text Search output 4

The result of the above query is true. Because cats is a plural form of cat

Now another query we write for cated

select to_tsvector(' The white dog jumped over the lazy cat ') @@ to_tsquery('cated');

Illustrate the end result of the above declaration by using the use of the following snapshot

PostgreSQL Full Text Search output 5

The result of the above statement is false because the meaning of the cated Word is different; it does not belong to the same cluster

  • Operators and Uses

tsquery provides a different operator to the user to make a fixable search on the document, reducing the user’s time and complexity. PostgreSQL provides the following operator

  • AND Operator (&)

Using this operator, we can return two words from the document.

Example

Select to_tsvector(' The white dog jumped over the lazy cat ') @@ to_tsquery('cat & dog');

Illustrate the end result of the above declaration by using the use of the following snapshot.

 PostgreSQL Full Text Search output 6

  • OR operator(|)

By using this operator, we can return at least one Word from the document.

Example

SELECT to_tsvector('The white dog jumped over the lazy cat')  @@ to_tsquery('cat |monkey');

Illustrate the end result of the above declaration by using the use of the following snapshot.

PostgreSQL Full Text Search output 7

  • NAVIGATION Operator (!)

By using this operator, we can check that Word is absent in the given document.

Example

SELECT to_tsvector('The white dog jumped over the lazy cat') @@ to_tsquery('!monkey');

Illustrate the end result of the above declaration by using the use of the following snapshot

PostgreSQL Full Text Search output 8

3. Stop Word

In the case of tsvector, it misses some words, but by using Stop Word, we can regain that Word.

Example

SELECT to_tsvector('pg_catalog.simple','Sky is blue and roses are red');

Illustrate the end result of the above declaration by using the use of the following snapshot

output 9

4. Normalization

Search dictionaries deal with natural language with the complexity of human language. Sometimes, the meaning is similar to a different word, so we use normalization to avoid the complexity of a word that differs from the same Word to one.

Example

SELECT to_tsvector('pg_catalog.English','Jon is very brillent studtent''from his class''he got first class from last semister');

Illustrate the end result of the above declaration by using the use of the following snapshot

output 10

5. Create Document/ Record

Here we create a simple table name as a record by using create a statement

Example

CREATE TABLE Record (                     record_id SERIAL,          record_text TEXT,    record_tokens TSVECTOR,        CONSTRAINT record_pkey PRIMARY KEY (record_id)  );

Illustrate the end result of the above declaration by using the use of the following snapshot.

  • Then insert the Record into a document.
INSERT INTO record (record_text) VALUES
('Ram is playing cricket with his friends.'),
('I want to go abroad for master studies'),
('PostgreSQL is popular technology.'),
('Full text search gives fast result');
Select * from Record;

Illustrate the result of the above statement by using the following snapshot.

 output 11

  • Now do update the command with the respective vector of each Record
UPDATE record r1   SET record_tokens = to_tsvector(r1.record_text)               FROM record r2;
Select * from record;

Illustrate the end result of the above declaration by using the use of the following snapshot

output 12

  • Now phrase search Record
SELECT            record_id,                   record_text FROM record   WHERE record_tokens @@ to_tsquery('play & friend');

Illustrate the end result of the above declaration by using the use of the following snapshot.

output 13

Conclusion

From the above article, we hope you understand what Full-Text Search in PostgreSQL is and how it is used. In the above article, we learn different methods of full-text search like To_tsvector and To_tsquery; with the different example, we also have seen how we can use different operators in tsquery. The full-text search can avoid the repetition of a word with normalizing. This is a very fast and advanced searching methodology in PostgreSQL.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL Full Text Search” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL OID
  2. Limit Offset in PostgreSQL
  3. PostgreSQL WITH Clause
  4. PostgreSQL Formatter

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW