<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pg_trgm &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/pg_trgm/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Fri, 12 May 2017 11:55:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>PostgreSQL: Compare two String Similarity in percentage (pg_trgm module)</title>
		<link>https://www.dbrnd.com/2017/05/postgresql-compare-two-string-similarity-in-percentage-pg_trgm-module/</link>
					<comments>https://www.dbrnd.com/2017/05/postgresql-compare-two-string-similarity-in-percentage-pg_trgm-module/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 20 May 2017 16:16:50 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[pg_trgm]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[similarity]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5090</guid>

					<description><![CDATA[In this post, I am sharing small demonstration on, how to find similarity between Postgres strings in percentage? PostgreSQL is a well known for a variety of string functions which are used for data analysis. One of our developers is generating random token string manually for two columns, and now he is required to find [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-compare-two-string-similarity-in-percentage-pg_trgm-module%2F&amp;linkname=PostgreSQL%3A%20Compare%20two%20String%20Similarity%20in%20percentage%20%28pg_trgm%20module%29" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-compare-two-string-similarity-in-percentage-pg_trgm-module%2F&amp;linkname=PostgreSQL%3A%20Compare%20two%20String%20Similarity%20in%20percentage%20%28pg_trgm%20module%29" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-compare-two-string-similarity-in-percentage-pg_trgm-module%2F&amp;linkname=PostgreSQL%3A%20Compare%20two%20String%20Similarity%20in%20percentage%20%28pg_trgm%20module%29" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-compare-two-string-similarity-in-percentage-pg_trgm-module%2F&amp;linkname=PostgreSQL%3A%20Compare%20two%20String%20Similarity%20in%20percentage%20%28pg_trgm%20module%29" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-compare-two-string-similarity-in-percentage-pg_trgm-module%2F&amp;linkname=PostgreSQL%3A%20Compare%20two%20String%20Similarity%20in%20percentage%20%28pg_trgm%20module%29" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-compare-two-string-similarity-in-percentage-pg_trgm-module%2F&amp;linkname=PostgreSQL%3A%20Compare%20two%20String%20Similarity%20in%20percentage%20%28pg_trgm%20module%29" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F05%2Fpostgresql-compare-two-string-similarity-in-percentage-pg_trgm-module%2F&#038;title=PostgreSQL%3A%20Compare%20two%20String%20Similarity%20in%20percentage%20%28pg_trgm%20module%29" data-a2a-url="https://www.dbrnd.com/2017/05/postgresql-compare-two-string-similarity-in-percentage-pg_trgm-module/" data-a2a-title="PostgreSQL: Compare two String Similarity in percentage (pg_trgm module)"></a></p><p>In this post, I am sharing small demonstration on, how to find similarity between Postgres strings in percentage?<br />
PostgreSQL is a well known for a variety of string functions which are used for data analysis.</p>
<blockquote><p>One of our developers is generating random token string manually for two columns, and now he is required to find similarity between this string. </p></blockquote>
<p>In the PostgreSQL, you can use a pg_trgm module to find similarity based on trigram matching. </p>
<p>Below is a demonstration of this:</p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_SimilarString
(
	Str1 TEXT
	,Str2 TEXT
);

INSERT INTO tbl_SimilarString VALUES
('Anvesh Patel','Anvesh Pat')
,('dbrnd','dbrnd blog')
,('database dev','database developer')
,('postgres database','database postgres');</pre> </p>
<p><strong>Install pg_trgm module:</strong></p>
<pre class="crayon-plain-tag">CREATE EXTENSION pg_trgm;</pre> </p>
<p><strong>Use similarity():</strong></p>
<pre class="crayon-plain-tag">SELECT similarity(Str1,Str2) FROM tbl_SimilarString;</pre> </p>
<p><strong>The result:</strong></p>
<pre class="crayon-plain-tag">similarity
------------
   0.714286
   0.545455
   0.578947
          1
(4 rows)</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/05/postgresql-compare-two-string-similarity-in-percentage-pg_trgm-module/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: Example of Trigram Index for Full Text Search using pg_trgm Extension</title>
		<link>https://www.dbrnd.com/2016/08/postgresql-example-of-trigram-index-for-full-text-search-using-pg_trgm-extension/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 23 Aug 2016 17:51:41 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Full Text Search]]></category>
		<category><![CDATA[pg_trgm]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[Trigram Index]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3551</guid>

					<description><![CDATA[In the PostgreSQL, we can use Trigram to enhance the use of Full Text Search. The Trigram algorithm divides string like &#8220;dog&#8221; = &#8220;d&#8221;,&#8221;do&#8221;,&#8221;dog&#8221;,&#8221;og&#8221;. The Trigram matching comes with pg_trgm extension. We can also apply Trigram operators on Gist and GIN indexes which increase the search speed and enhance the performance. We have already different [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-trigram-index-for-full-text-search-using-pg_trgm-extension%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20Trigram%20Index%20for%20Full%20Text%20Search%20using%20pg_trgm%20Extension" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-trigram-index-for-full-text-search-using-pg_trgm-extension%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20Trigram%20Index%20for%20Full%20Text%20Search%20using%20pg_trgm%20Extension" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-trigram-index-for-full-text-search-using-pg_trgm-extension%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20Trigram%20Index%20for%20Full%20Text%20Search%20using%20pg_trgm%20Extension" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-trigram-index-for-full-text-search-using-pg_trgm-extension%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20Trigram%20Index%20for%20Full%20Text%20Search%20using%20pg_trgm%20Extension" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-trigram-index-for-full-text-search-using-pg_trgm-extension%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20Trigram%20Index%20for%20Full%20Text%20Search%20using%20pg_trgm%20Extension" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-trigram-index-for-full-text-search-using-pg_trgm-extension%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20Trigram%20Index%20for%20Full%20Text%20Search%20using%20pg_trgm%20Extension" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-trigram-index-for-full-text-search-using-pg_trgm-extension%2F&#038;title=PostgreSQL%3A%20Example%20of%20Trigram%20Index%20for%20Full%20Text%20Search%20using%20pg_trgm%20Extension" data-a2a-url="https://www.dbrnd.com/2016/08/postgresql-example-of-trigram-index-for-full-text-search-using-pg_trgm-extension/" data-a2a-title="PostgreSQL: Example of Trigram Index for Full Text Search using pg_trgm Extension"></a></p><p>In the PostgreSQL, we can use Trigram to enhance the use of Full Text Search.<br />
The Trigram algorithm divides string like &#8220;dog&#8221; = &#8220;d&#8221;,&#8221;do&#8221;,&#8221;dog&#8221;,&#8221;og&#8221;.</p>
<blockquote><p>The Trigram matching comes with pg_trgm extension.<br />
We can also apply Trigram operators on Gist and GIN indexes which increase the search speed and enhance the performance. </p></blockquote>
<p>We have already different thesaurus or synomyms for the Full Text Search, but we can use Trigram for implementing typing corrections and suggestions. </p>
<p>Here, you can find different examples of this. </p>
<p><strong>Create a sample table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_TestTriGramIndex
(
	ID INT
	,TextData TEXT
);</pre> </p>
<p><strong>Create a Trigram Index on Text Column:</strong></p>
<pre class="crayon-plain-tag">CREATE INDEX trgm_idx_tbl_TestTriGramIndex_TextData ON tbl_TestTriGramIndex USING GIST (TextData gist_trgm_ops);</pre> </p>
<p><strong>If you get below error, install require pg_trgm extension:</strong></p>
<pre class="crayon-plain-tag">ERROR:  operator class "gist_trgm_ops" does not exist for access method "gist"</pre> </p>
<p><strong>Install pg_trgm extension:</strong></p>
<pre class="crayon-plain-tag">CREATE EXTENSION pg_trgm;</pre> </p>
<p><strong>Insert few sample records:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_TestTriGramIndex
VALUES 
(1,'Anvesh Patel')
,(2,'Database Research &amp; Development')
,(3,'PostgreSQL is better than SQL Server')
,(4,'PostgreSQL is supported powerful BRIN Indexes');</pre> </p>
<p><strong>Execute few SELECTs for text comparison:</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM tbl_TestTriGramIndex
WHERE TextData % 'PostgreSQL is';

SELECT *FROM tbl_TestTriGramIndex
WHERE TextData LIKE '%SQL%';

SELECT *FROM tbl_TestTriGramIndex
WHERE TextData ~* 'ql';</pre> </p>
<p><strong>Execute below SELECTs and check Trigram generated result of given string:</strong></p>
<pre class="crayon-plain-tag">SELECT SHOW_TRGM('Anvesh') AS Anvesh;
SELECT SHOW_TRGM('Anvesh Patel') AS AnveshPatel;</pre> </p>
<p><strong>Execute below SELECTs and check percentage of similarity:</strong></p>
<pre class="crayon-plain-tag">SELECT SIMILARITY('Anvesh','anv') AS SimilarPercentage;
SELECT SIMILARITY('Anvesh','anve') AS SimilarPercentage;</pre> </p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
