What is full-text search?
Full-text search is an information retrieval technology that finds information relevant to a user query quickly and accurately across large volumes of text data. PolarDB for PostgreSQL provides a range of full-text search capabilities.
Background
Unlike traditional keyword search, full-text search aims to find information relevant to a user query quickly and accurately across massive volumes of text data. It processes the entire content of a document rather than only specific fields or tags.
A full-text search system typically performs the following steps:
Text pre-processing: tokenizes the text, removes stop words, and applies stemming to improve retrieval efficiency and accuracy.
Index creation: builds an index for the processed text, usually an inverted index that records where each word appears and which documents contain it.
Query processing: after you submit a query, the system analyzes the query statement and converts it into a format suitable for retrieval.
Result ranking: sorts the results by a relevance algorithm and returns the ones that best match your needs.
Use cases
Full-text search applies to a wide range of use cases. Typical examples include:
Document management systems: help you quickly find internal documents, reports, and clauses to improve work efficiency.
Online search engines: use full-text search to help users find the information they need quickly.
Academic research: researchers can quickly locate relevant literature and data in academic databases and reference management tools.
E-commerce: platforms use full-text search to help customers find the products they need quickly and improve the shopping experience.
Social media: users can search posts, comments, and images by keyword, which makes information easier to find.
Legal document retrieval: lawyers and legal professionals use full-text search tools to find relevant cases and legal clauses quickly.
Medical record management: hospitals can use full-text search to quickly find patient information and history in medical records and reports.
Customer support: online service systems use full-text search to help customers find frequently asked questions (FAQ) and support documents quickly.
Content management systems: websites and blogs use full-text search to help visitors find relevant articles and materials quickly.
Libraries and information retrieval: library retrieval systems use full-text search to make it easy for readers to find books and articles.
Feature overview
Tokenization
PolarDB for PostgreSQL full-text indexes pre-process documents and store an index for fast subsequent searches. Pre-processing includes:
Parsing documents into tokens. The parser identifies several token types, such as numbers, words, compound words, and email addresses, so that each can be processed differently.
Converting tokens into lexemes. A lexeme is a normalized string, similar to a token, that unifies the different forms of the same word into a single representation. Normalization usually converts uppercase letters to lowercase and often removes suffixes such as s or es in English. Searches then recognize the variants of a word, so you do not have to type every possible form.
Storing pre-processed documents for search optimization. For example, each document can be represented as an ordered array of normalized lexemes. Besides the lexemes, position information is usually stored for proximity ranking. A document with a dense cluster of query terms then ranks above a document with scattered query terms.
tsvector
tsvector is a data type that PolarDB for PostgreSQL provides for full-text search. It stores processed text efficiently for fast search and matching. A tsvector value holds the lexical index of a document, usually the vocabulary together with the position of each term in the text.
pg_bigm
pg_bigm is an extension of PolarDB for PostgreSQL that provides fuzzy search, in particular approximate string matching. Applications with large volumes of text data, such as search engines and content management systems, use it most often. Its core idea is to improve the efficiency and accuracy of text search through n-grams.
The pg_bigm extension is effective for fuzzy queries with leading and trailing wildcards (like '%xxxx%').
pg_trgm
pg_trgm is an extension of PolarDB for PostgreSQL that adds support for trigrams. A trigram is a method for string similarity search and works especially well for fuzzy matching and text similarity queries. pg_trgm creates indexes and query operators to speed up queries over large text datasets, and is commonly used for full-text search, auto-completion, and spell correction.
The pg_trgm extension is effective for fuzzy queries with leading and trailing wildcards (like '%xxxx%').
Chinese tokenization
In Chinese, the word is the smallest meaningful unit. Unlike English, written Chinese does not use spaces to separate words. Because of this, it is difficult for the default PostgreSQL full-text search engine to produce tokenization results that match Chinese semantics.
For Chinese tokenization, PolarDB for PostgreSQL supports two full-text search capabilities: pg_jieba and Zhparser.
pg_jieba
Jieba is one of the more popular Chinese tokenization libraries. It accurately identifies the words in a Chinese sentence and segments the sentence accordingly. pg_jieba is an extension that brings Jieba tokenization into the database and tokenizes Chinese text efficiently to support full-text search.
Zhparser
Simple Chinese Word Segmentation (SCWS) is an open source Chinese tokenization engine based on a word frequency dictionary. It segments a block of Chinese text into words with reasonable accuracy.
Zhparser is a Chinese tokenization extension built on SCWS. It stays compatible with the existing PostgreSQL full-text search capabilities while adding a wide range of configuration options and support for custom dictionaries.
Indexes
PolarDB for PostgreSQL supports multiple index structures for full-text search.
GIN index
Generalized Inverted Index (GIN) is a PostgreSQL index type that supports full-text search. GIN indexes make full-text search efficient, especially over large-scale text data, and allow fast query operations for complex text queries such as those that use tsvector and tsquery. GIN indexes also support data types such as JSONB.
RUM index
RUM is a PostgreSQL extension that provides the RUM index type for full-text search and other indexing needs. The index is designed to improve full-text search performance, especially when documents need to be ranked.
A RUM index is an inverted index similar to the built-in Generalized Inverted Index (GIN). The main difference is that a RUM index provides a mechanism for storing additional information, which returns results faster when you sort or perform other operations. In a full-text search, for example, a RUM index can store the positions of words in a document. At query time, it uses these positions to calculate relevance rankings.
Query processing
tsquery
tsquery is a full-text search feature designed for querying text data. It lets you create complex search conditions so that you can find the information you need quickly and effectively in large-scale text data.PolarDB for PostgreSQL also provides the to_tsquery method, which converts text into a tsquery. Combine it with tsvector and the full-text search operators to run a full-text search query.
tsquery supports the @@ (containment) operator and the Boolean operators & (AND), | (OR), and ! (NOT), which make it easy to build search queries with combined conditions.
Ranking
ts_rank
ts_rank is a PostgreSQL function for full-text search. It is mainly used to calculate the relevance score between a document and a query. You can use this score to assess the importance or relevance of a document under specific search conditions.