Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 March PostgreSQL: Full Text Search using tsvector

PostgreSQL: Full Text Search using tsvector

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing few examples of full text searching and pattern matching in PostgreSQL.
PostgreSQL provides the different options for Full text searching and pattern matching.

Here, I am using tsvector for full text search which is document type and uses match operator like @@.

PostgreSQL: Example of Trigram Index for Full Text Search using pg_trgm Extension

Below are the few examples of Full Text Search:

Casting in tsvector:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
SELECT 'I am owner of dbrnd.com'::tsvector @@ 'am & of'::tsquery as Result
 
result
---------
true
 
 
SELECT 'I am owner of dbrnd.com'::tsvector @@ 'am & dbrnd.com'::tsquery as Result
 
result
---------
true
 
SELECT 'I am owner of dbrnd.com'::tsvector @@ 'am & dbrnd'::tsquery as Result
 
result
---------
false

Use to_tsvector():

1
2
3
4
5
SELECT to_tsvector('I am owner of dbrnd.com') @@ to_tsquery('owner & dbrnd.com') as Result
 
result
---------
true

Search adjacent string combination:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SELECT to_tsvector('anvesh patel') @@ to_tsquery('anvesh <-> patel') as Result
 
result
---------
true
 
SELECT to_tsvector('anvesh m patel') @@ to_tsquery('anvesh <-> patel') as Result
 
result
---------
false
 
SELECT to_tsvector('anvesh m patel') @@ to_tsquery('anvesh <2> patel') as Result
 
result
---------
true

Mar 31, 2018Anvesh Patel
PostgreSQL: Script to check the status of AutoVacuum for all TablesSQL Server: Never Apply INNER JOIN or EQUI JOIN on NULL Values
Comments: 3
  1. Ritanjali
    January 22, 2019 at 11:08 am

    Hi Anvesh,

    I have 20 millions of record in postgresql table (i am using partition too) from the table i have to search more than one word like (‘project manager’) with full text search.I have created tokens using to_tsvector particular column and i stored in one more column.
    now i am trying to search from the token column more than one word using poximity operator
    like to_tsquery(‘project manager’) but for 10 record fecthing it is taking more that 15 mins which is too slow to populate the in webpage.Is there any other way in postgresql to achieve this type of search in less time?

    Can you please help me solve this issue. Let us know if you need any details from me.

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      February 8, 2019 at 6:50 pm

      Is this related to custom paging?

      ReplyCancel
  2. Ritanjali
    February 6, 2019 at 12:00 pm

    Hi Anvesh,

    SELECT to_tsvector(‘anvesh patel’) @@ to_tsquery(‘anvesh patel’) it returns true

    i am trying search some resumes using (“Marketing Coordinator” OR “Marketing Specialist” OR “Marketing Associate” ) AND “Creative Suite” search string .I am getting the result but not as expected. it working like “Marketing & Coordinator” .If one resume is having in the first paragraph “Marketing” and other paragraph having “Coordinator” it is returning those resumes where as i need only “Marketing Coordinator” together string will available those resumes only i am expecting.

    Can we able to get result search string like (“Marketing Coordinator” OR “Marketing Specialist” OR “Marketing Associate” ) AND “Creative Suite”. using operator and to_tsvector can we achieve complex search string full text search in postgresql.

    Please help me on this issue.

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

March 31, 2018 3 Comments PostgreSQLAnvesh Patel, database, database research and development, dbrnd, Full Text Search, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Monitoring, PostgreSQL Performance Tuning, PostgreSQL Programming, PostgreSQL Tips and Tricks, to_tsquery, tsquery
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....