Have you ever wondered how some searches result in the actual information within a short amount of execution time? Most of the cases may use some other alternatives like ElasticSearch. Is it possible with just PostgreSQL Database? Yes, it is. For such a faster search, we have a more advanced searching technique in PostgreSQL called Full-text Search. For searching a database for some words which may occur in some column of some table which you are unsure, it is not recommended to use like queries. It might be possible to find out the result with the power of regular expressions, but it is slow. Here comes the importance of Full-text Search.\n\n\n\nWhat is Full-Text Search?\n\n\n\nFull-text search is a technique for searching documents or collections in a database. The text search engine can parse the documents and save the associations of keywords called lexeme with the parent document. These associations are used for searching the documents against query keywords. \n\n\n\nDisadvantages of LIKE operator search\n\n\n\nThe LIKE operator has no linguistic support. The regular expression can support LIKE operator searches up to a certain extent and still lack the mechanism to handle plural words and derived words. A single word can have many derived words that are tedious to find out using regular expressions. They have no ordering on search results which may lead the search worthless if the search matches thousands of records. They need to scan all the rows for finding the result makes the search pretty much slow.\n\n\n\nHow Full-Text Search can be implemented?\n\n\n\nFor a full-text search, documents must be preprocessed to a vector format in which the searching and ranking are performed. The original text will be retrieved only when the user is intended to fetch it. PostgreSQL has two main functions for implementing a full-text search. \n\n\n\nto_tsvector : parses the document to get a list of tokens, reduces tokens to lexemes and produces a list of lexemes with their position as a tsvector. \u2018ts\u2019 in the tsvector represents \u2018text search\u2019. A list of dictionaries will be consulted for each token and thus eliminates multiple normalized lexemes representing a token. to_tsquery : for querying the tsvector for certain words, phrases or complex queries separated by boolean operators & (AND), | (OR) or ! (NOT). \n\n\n\nThe to_tsvector will omit the stop words, articles and conjunctions which carry less meaning in a document. For eg:\n\n\n\nSELECT to_tsvector('The quick brown fox jumped over the lazy dog') \n@@ to_tsquery('fox');\n\n\n\nwhich returns true as output.\n\n\n\nSELECT to_tsvector('The quick brown fox jumped over the lazy dog') \n@@ to_tsquery('foxes');\n\n\n\nalso, return true as output which is the great benefit of considering the semantic meaning of the lexemes.\n\n\n\nRanking the search result\n\n\n\nIf the search operation results in thousands of records, if the results are not ranked or sorted by relevance, the results will be ineffective. Ranking the search results based on some criteria is one of the greatest advantages of a full-text search. The ranking may depend on many factors. PostgreSQL provides two predefined ranking functions, one takes into account lexical, proximity, and structural information and another one on how important is the part of the document where they occur. \n\n\n\nThe ranking criteria needed for your application may differ from that of functions provided by the PostgreSQL. For your specific application needs, you can tailor the ranking functions by writing it own or combining the results with additional factors. The built-in functions for ranking are given below.\n\n\n\nts_rank: ranks the tsvector based on the frequency of matched lexemes. ts_rank_cd: ranks the tsvector based on cover density which needs the positional information as to its input.\n\n\n\nPerformance Improvement on Full-Text Search\n\n\n\nCreating ts_vector on run time for a number of records will not produce the result in lightning speed. For expecting the faster execution, we need to pre-create the vector for each new entry created to our table. For maintaining such vectors as persisted data, either we can save the vector in a column for the same table, or can be created as a materialized view for creating a vector from multiple tables. \n\n\n\nA Materialized view is a cached set of data that allows us to query instead of querying the tables. Accessing the data through a materialized view is much faster than accessing the data through views or tables directly. The materialized views don\u2019t execute the underlying query every time you access them which makes the materialized view faster than normal database views. To update the data, we need to refresh the materialized view.\n\n\n\nIndexing is another powerful way to boost the execution speed to a great extent. Postgres provides two ways for indexes - GiST (Generalized Search Tree) and GIN (Generalized Inverted Index) indexes. If our database is read-intensive, we should use GIN index as it is slower to build but faster to query, while GiST index is faster to build but require additional reads. \n\n\n\nAdvantages of Full-Text Search\n\n\n\nFor such a great search tool, there is no need to install additional software or libraries such as ElasticSearch. Faster execution - When compared to the LIKE operator, the Full-Text Search will return search results in an execution speed 10 or more times faster. The full-text search is performed on the same database where our business data is saved and there is no need to sync the data which might be needed if we use other search techniques like ElasticSearch. Able to rank the search results based on ranking functions and weights we assign to the vector. Thus it produces better results than simply returning the total matched list of records.\n\n\n\nConclusion\n\n\n\nThe full-text search in PostgreSQL is very fast and powerful. This advanced search also allows searching any phrases, plural words, tenses, and other related words. Coupling the full-text search with materialized views and indexes boost the lightning speed of execution. I hope you have a clear basic idea of what is full-text search, why it is used and how it can perform better.