{"id":2004,"date":"2024-06-28T09:36:35","date_gmt":"2024-06-28T09:36:35","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=2004"},"modified":"2024-06-28T09:36:40","modified_gmt":"2024-06-28T09:36:40","slug":"implementing-full-text-search-with-postgresqls-tsvector-and-tsquery","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/","title":{"rendered":"Implementing Full-Text Search with PostgreSQL&#8217;s tsvector and tsquery"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Full-text search is a powerful feature in modern databases that allows users to search through large amounts of text quickly and efficiently. PostgreSQL, one of the most advanced open-source databases, provides robust support for full-text search through its <code>tsvector<\/code> and <code>tsquery<\/code> data types. These tools enable efficient text search capabilities by indexing and querying text data in a way that is both flexible and performant. This tutorial will guide you through the process of implementing full-text search in PostgreSQL using <code>tsvector<\/code> and <code>tsquery<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this tutorial, you will have a comprehensive understanding of how to leverage PostgreSQL&#8217;s full-text search capabilities to enhance your applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Understanding Full-Text Search Concepts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before diving into the implementation, it&#8217;s crucial to understand some key concepts related to full-text search in PostgreSQL.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 <code>tsvector<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>tsvector<\/code> (text search vector) is a data type in PostgreSQL that stores preprocessed text data for efficient searching. It converts the text into a searchable format by breaking it down into tokens (words) and normalizing them (e.g., removing punctuation and converting to lowercase). Each token is then mapped to its positions in the original text.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 <code>tsquery<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>tsquery<\/code> (text search query) is a data type used to represent search queries. It consists of tokens and operators (AND, OR, NOT) that define the search criteria. <code>tsquery<\/code> is used to match against <code>tsvector<\/code> to find relevant text entries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.3 Text Search Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL uses text search configurations to control how text is tokenized and normalized. Configurations include dictionaries and tokenizers that define the rules for processing text. PostgreSQL provides several built-in configurations for different languages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Setting Up PostgreSQL for Full-Text Search<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before you can start using full-text search, you need to set up PostgreSQL. Ensure you have PostgreSQL installed on your system. You can download and install PostgreSQL from <a href=\"https:\/\/www.postgresql.org\/download\/\">the official website<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once installed, you can create a new database and connect to it using the PostgreSQL command-line tool <code>psql<\/code> or any other PostgreSQL client.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-comment\"># Create a new database<\/span>\ncreatedb full_text_search_db\n\n<span class=\"hljs-comment\"># Connect to the database<\/span>\npsql full_text_search_db<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">3. Creating and Populating Tables<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s create a sample table to work with. For this tutorial, we&#8217;ll create a table called <code>documents<\/code> to store text data.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> documents (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">SERIAL<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    title <span class=\"hljs-built_in\">TEXT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    <span class=\"hljs-keyword\">content<\/span> <span class=\"hljs-built_in\">TEXT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Next, let&#8217;s populate the table with some sample data.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> documents (title, <span class=\"hljs-keyword\">content<\/span>) <span class=\"hljs-keyword\">VALUES<\/span>\n(<span class=\"hljs-string\">'Introduction to PostgreSQL'<\/span>, <span class=\"hljs-string\">'PostgreSQL is a powerful, open source object-relational database system.'<\/span>),\n(<span class=\"hljs-string\">'Full-Text Search in PostgreSQL'<\/span>, <span class=\"hljs-string\">'Learn how to implement full-text search using PostgreSQL tsvector and tsquery.'<\/span>),\n(<span class=\"hljs-string\">'Advanced PostgreSQL Features'<\/span>, <span class=\"hljs-string\">'Explore advanced features in PostgreSQL including indexing, partitioning, and more.'<\/span>),\n(<span class=\"hljs-string\">'PostgreSQL Performance Tuning'<\/span>, <span class=\"hljs-string\">'Tips and techniques for optimizing PostgreSQL performance.'<\/span>),\n(<span class=\"hljs-string\">'Scaling PostgreSQL'<\/span>, <span class=\"hljs-string\">'Strategies for scaling PostgreSQL databases to handle large amounts of data.'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">4. Generating <code>tsvector<\/code> Columns<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To enable full-text search, we need to generate <code>tsvector<\/code> columns that store the tokenized and normalized text data. We&#8217;ll add a <code>tsvector<\/code> column to the <code>documents<\/code> table.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> documents <span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> tsv_content tsvector;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Next, we&#8217;ll populate the <code>tsvector<\/code> column with data from the <code>title<\/code> and <code>content<\/code> columns.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">UPDATE<\/span> documents <span class=\"hljs-keyword\">SET<\/span> tsv_content = to_tsvector(<span class=\"hljs-string\">'english'<\/span>, title || <span class=\"hljs-string\">' '<\/span> || <span class=\"hljs-keyword\">content<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Here, we&#8217;re using the <code>to_tsvector<\/code> function to convert the combined <code>title<\/code> and <code>content<\/code> into a <code>tsvector<\/code> using the &#8216;english&#8217; text search configuration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Creating Indexes on <code>tsvector<\/code> Columns<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To make full-text search queries fast, we need to create an index on the <code>tsvector<\/code> column. PostgreSQL provides the <code>GIN<\/code> (Generalized Inverted Index) index type, which is well-suited for full-text search.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">INDEX<\/span> idx_tsv_content <span class=\"hljs-keyword\">ON<\/span> documents <span class=\"hljs-keyword\">USING<\/span> GIN (tsv_content);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This index will allow PostgreSQL to quickly search through the tokenized text data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Querying with <code>tsquery<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With the <code>tsvector<\/code> column and index in place, we can start querying the data using <code>tsquery<\/code>. Let&#8217;s perform some basic full-text search queries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6.1 Simple Search<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> title, <span class=\"hljs-keyword\">content<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> documents\n<span class=\"hljs-keyword\">WHERE<\/span> tsv_content @@ to_tsquery(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-string\">'PostgreSQL'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This query searches for documents that contain the term &#8220;PostgreSQL&#8221;. The <code>@@<\/code> operator is used to match the <code>tsvector<\/code> against the <code>tsquery<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6.2 Phrase Search<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> title, <span class=\"hljs-keyword\">content<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> documents\n<span class=\"hljs-keyword\">WHERE<\/span> tsv_content @@ to_tsquery(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-string\">'full &amp; text &amp; search'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This query searches for documents that contain the phrase &#8220;full text search&#8221;. The <code>&amp;<\/code> operator specifies an AND condition between terms.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6.3 Negation<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> title, <span class=\"hljs-keyword\">content<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> documents\n<span class=\"hljs-keyword\">WHERE<\/span> tsv_content @@ to_tsquery(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-string\">'PostgreSQL &amp; !performance'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This query searches for documents that contain &#8220;PostgreSQL&#8221; but not &#8220;performance&#8221;. The <code>!<\/code> operator specifies a NOT condition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Advanced Search Techniques<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PostgreSQL&#8217;s full-text search capabilities go beyond simple term matching. Let&#8217;s explore some advanced search techniques.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7.1 Weighting and Ranking<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can assign different weights to different parts of the document and rank the search results based on relevance.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">UPDATE<\/span> documents\n<span class=\"hljs-keyword\">SET<\/span> tsv_content = setweight(to_tsvector(<span class=\"hljs-string\">'english'<\/span>, title), <span class=\"hljs-string\">'A'<\/span>) ||\n                  setweight(to_tsvector(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-keyword\">content<\/span>), <span class=\"hljs-string\">'B'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Here, we assign a higher weight (&#8216;A&#8217;) to the <code>title<\/code> and a lower weight (&#8216;B&#8217;) to the <code>content<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To rank the results, use the <code>ts_rank<\/code> function.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> title, <span class=\"hljs-keyword\">content<\/span>, ts_rank(tsv_content, to_tsquery(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-string\">'PostgreSQL'<\/span>)) <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">rank<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> documents\n<span class=\"hljs-keyword\">WHERE<\/span> tsv_content @@ to_tsquery(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-string\">'PostgreSQL'<\/span>)\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-keyword\">rank<\/span> <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">7.2 Highlighting Search Terms<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Highlighting search terms in the results can improve user experience. PostgreSQL provides the <code>ts_headline<\/code> function for this purpose.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> title, ts_headline(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-keyword\">content<\/span>, to_tsquery(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-string\">'PostgreSQL'<\/span>)) <span class=\"hljs-keyword\">AS<\/span> highlighted_content\n<span class=\"hljs-keyword\">FROM<\/span> documents\n<span class=\"hljs-keyword\">WHERE<\/span> tsv_content @@ to_tsquery(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-string\">'PostgreSQL'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This query returns the content with search terms highlighted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7.3 Synonyms and Dictionaries<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can enhance full-text search by using synonyms and custom dictionaries. This allows you to account for variations in search terms.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, create a synonym dictionary.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-built_in\">TEXT<\/span> <span class=\"hljs-keyword\">SEARCH<\/span> DICTIONARY synonym_dict (\n    <span class=\"hljs-keyword\">TEMPLATE<\/span> = <span class=\"hljs-keyword\">synonym<\/span>,\n    SYNONYMS = <span class=\"hljs-string\">'path\/to\/synonym\/file'<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Next, create a text search configuration that uses the synonym dictionary.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-built_in\">TEXT<\/span> <span class=\"hljs-keyword\">SEARCH<\/span> CONFIGURATION custom_english (COPY = english);\n<span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-built_in\">TEXT<\/span> <span class=\"hljs-keyword\">SEARCH<\/span> CONFIGURATION custom_english\n    <span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">MAPPING<\/span> <span class=\"hljs-keyword\">FOR<\/span> asciiword <span class=\"hljs-keyword\">WITH<\/span> synonym_dict, english_stem;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Finally, use the custom configuration in your queries.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">UPDATE<\/span> documents <span class=\"hljs-keyword\">SET<\/span> tsv_content = to_tsvector(<span class=\"hljs-string\">'custom_english'<\/span>, title || <span class=\"hljs-string\">' '<\/span> || <span class=\"hljs-keyword\">content<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">8. Performance Considerations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Full-text search can be resource-intensive, especially with large datasets. Here are some performance optimization tips.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">8.1 Index Maintenance<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Regularly maintain your indexes to keep them efficient.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">REINDEX INDEX idx_tsv_content;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">8.2 Partitioning<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For very large datasets, consider partitioning your tables to improve query performance.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> documents_2023 <span class=\"hljs-keyword\">PARTITION<\/span> <span class=\"hljs-keyword\">OF<\/span> documents <span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">VALUES<\/span> <span class=\"hljs-keyword\">FROM<\/span> (<span class=\"hljs-string\">'2023-01-01'<\/span>) <span class=\"hljs-keyword\">TO<\/span> (<span class=\"hljs-string\">'2024-01-01'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">8.3 Configuration Tuning<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Tune PostgreSQL configurations to optimize full-text search performance. Key parameters include <code>work_mem<\/code>, <code>maintenance_work_mem<\/code>, and <code>shared_buffers<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> work_mem = <span class=\"hljs-string\">'64MB'<\/span>;\n<span class=\"hljs-keyword\">SET<\/span> maintenance_work_mem = <span class=\"hljs-string\">'256MB'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">9. Real-World Examples<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s look at a couple of real-world examples to see how full-text search can be applied.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">9.1 Blog Search<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine you have a blog with thousands of articles. You want to implement a search feature that allows users to find articles by keywords.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, create the articles table.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> articles (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">SERIAL<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    title <span class=\"hljs-built_in\">TEXT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    <span class=\"hljs-keyword\">body<\/span> <span class=\"hljs-built_in\">TEXT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    tsv_body tsvector\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Next, populate the <code>tsvector<\/code> column.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">UPDATE<\/span> articles <span class=\"hljs-keyword\">SET<\/span> tsv_body = to_tsvector(<span class=\"hljs-string\">'english'<\/span>, title || <span class=\"hljs-string\">' '<\/span> || <span class=\"hljs-keyword\">body<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Create an index on the <code>tsvector<\/code> column.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">CREATE INDEX idx_tsv_body ON articles USING GIN (tsv_body);<\/code><\/span><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Finally, implement the search functionality.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> title, ts_headline(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-keyword\">body<\/span>, to_tsquery(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-string\">'search &amp; feature'<\/span>)) <span class=\"hljs-keyword\">AS<\/span> highlighted_body\n<span class=\"hljs-keyword\">FROM<\/span> articles\n<span class=\"hljs-keyword\">WHERE<\/span> tsv_body @@ to_tsquery(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-string\">'search &amp; feature'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">9.2 E-commerce Product Search<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For an e-commerce platform, you want to implement a search feature that allows users to find products by name and description.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, create the products table.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">SERIAL<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    <span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-built_in\">TEXT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    description <span class=\"hljs-built_in\">TEXT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    tsv_description tsvector\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Next, populate the <code>tsvector<\/code> column.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">UPDATE<\/span> products <span class=\"hljs-keyword\">SET<\/span> tsv_description = to_tsvector(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-keyword\">name<\/span> || <span class=\"hljs-string\">' '<\/span> || description);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Create an index on the <code>tsvector<\/code> column.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">INDEX<\/span> idx_tsv_description <span class=\"hljs-keyword\">ON<\/span> products <span class=\"hljs-keyword\">USING<\/span> GIN (tsv_description);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Finally, implement the search functionality.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">name<\/span>, ts_headline(<span class=\"hljs-string\">'english'<\/span>, description, to_tsquery(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-string\">'search &amp; product'<\/span>)) <span class=\"hljs-keyword\">AS<\/span> highlighted_description\n<span class=\"hljs-keyword\">FROM<\/span> products\n<span class=\"hljs-keyword\">WHERE<\/span> tsv_description @@ to_tsquery(<span class=\"hljs-string\">'english'<\/span>, <span class=\"hljs-string\">'search &amp; product'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">10. Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Implementing full-text search with PostgreSQL&#8217;s <code>tsvector<\/code> and <code>tsquery<\/code> provides a powerful and efficient way to search through large text data. By understanding the concepts, setting up the database, creating indexes, and utilizing advanced search techniques, you can enhance your applications with robust search capabilities.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Remember to optimize your configurations and maintain your indexes to ensure optimal performance. With practice and experimentation, you&#8217;ll be able to leverage PostgreSQL&#8217;s full-text search to its fullest potential.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Full-text search is a powerful feature in modern databases that allows users to search through large amounts of text quickly and efficiently. PostgreSQL, one of the most advanced open-source databases, provides robust support for full-text search through its tsvector and tsquery data types. These tools enable efficient text search capabilities by indexing and querying [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[14,22],"tags":[],"class_list":["post-2004","post","type-post","status-publish","format-standard","category-database-development","category-postgresql","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Implementing Full-Text Search with PostgreSQL&#039;s tsvector, tsquery<\/title>\n<meta name=\"description\" content=\"A tsvector (text search vector) is a data type in PostgreSQL that stores preprocessed text data for efficient searching.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Implementing Full-Text Search with PostgreSQL&#039;s tsvector, tsquery\" \/>\n<meta property=\"og:description\" content=\"A tsvector (text search vector) is a data type in PostgreSQL that stores preprocessed text data for efficient searching.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-06-28T09:36:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-28T09:36:40+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"Implementing Full-Text Search with PostgreSQL&#8217;s tsvector and tsquery\",\"datePublished\":\"2024-06-28T09:36:35+00:00\",\"dateModified\":\"2024-06-28T09:36:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\\\/\"},\"wordCount\":930,\"articleSection\":[\"Database Development\",\"PostgreSQL\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\\\/\",\"name\":\"Implementing Full-Text Search with PostgreSQL's tsvector, tsquery\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2024-06-28T09:36:35+00:00\",\"dateModified\":\"2024-06-28T09:36:40+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"description\":\"A tsvector (text search vector) is a data type in PostgreSQL that stores preprocessed text data for efficient searching.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Database Development\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/database-development\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Implementing Full-Text Search with PostgreSQL&#8217;s tsvector and tsquery\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1783167470\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1783167470\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1783167470\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Implementing Full-Text Search with PostgreSQL's tsvector, tsquery","description":"A tsvector (text search vector) is a data type in PostgreSQL that stores preprocessed text data for efficient searching.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/","og_locale":"en_US","og_type":"article","og_title":"Implementing Full-Text Search with PostgreSQL's tsvector, tsquery","og_description":"A tsvector (text search vector) is a data type in PostgreSQL that stores preprocessed text data for efficient searching.","og_url":"https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/","article_published_time":"2024-06-28T09:36:35+00:00","article_modified_time":"2024-06-28T09:36:40+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"Implementing Full-Text Search with PostgreSQL&#8217;s tsvector and tsquery","datePublished":"2024-06-28T09:36:35+00:00","dateModified":"2024-06-28T09:36:40+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/"},"wordCount":930,"articleSection":["Database Development","PostgreSQL"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/","url":"https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/","name":"Implementing Full-Text Search with PostgreSQL's tsvector, tsquery","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2024-06-28T09:36:35+00:00","dateModified":"2024-06-28T09:36:40+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"description":"A tsvector (text search vector) is a data type in PostgreSQL that stores preprocessed text data for efficient searching.","breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/implementing-full-text-search-with-postgresqls-tsvector-and-tsquery\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Database Development","item":"https:\/\/www.w3computing.com\/articles\/database-development\/"},{"@type":"ListItem","position":3,"name":"Implementing Full-Text Search with PostgreSQL&#8217;s tsvector and tsquery"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1783167470","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1783167470","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1783167470","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/2004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=2004"}],"version-history":[{"count":1,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/2004\/revisions"}],"predecessor-version":[{"id":2005,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/2004\/revisions\/2005"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=2004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=2004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=2004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}