RefLib

Reference library for AI

Back

postgresql_16:9.6.35.2 Functions and Operators

PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > pg_trgm — support for similarity of text using trigram matching > Functions and Operators

The functions provided by the pg_trgm module are shown in (see pgtrgm-func-table), the operators in (see pgtrgm-op-table).

| Function Description | | --- | | similarity ( text, text ) real Returns a number that indicates how similar the two arguments are. The range of the result is zero (indicating that the two strings are completely dissimilar) to one (indicating that the two strings are identical). | | show_trgm ( text ) text[] Returns an array of all the trigrams in the given string. (In practice this is seldom useful except for debugging.) | | word_similarity ( text, text ) real Returns a number that indicates the greatest similarity between the set of trigrams in the first string and any continuous extent of an ordered set of trigrams in the second string. For details, see the explanation below. | | strict_word_similarity ( text, text ) real Same as word_similarity, but forces extent boundaries to match word boundaries. Since we don't have cross-word trigrams, this function actually returns greatest similarity between first string and any continuous extent of words of the second string. | | show_limit () real Returns the current similarity threshold used by the % operator. This sets the minimum similarity between two words for them to be considered similar enough to be misspellings of each other, for example. (*Deprecated*; instead use SHOW pg_trgm.similarity_threshold.) | | set_limit ( real ) real Sets the current similarity threshold that is used by the % operator. The threshold must be between 0 and 1 (default is 0.3). Returns the same value passed in. (*Deprecated*; instead use SET pg_trgm.similarity_threshold.) |

Consider the following example: # SELECT word_similarity('word', 'two words'); word_similarity ----------------- 0.8 (1 row) In the first string, the set of trigrams is {" w"," wo","wor","ord","rd "}. In the second string, the ordered set of trigrams is {" t"," tw","two","wo "," w"," wo","wor","ord","rds","ds "}. The most similar extent of an ordered set of trigrams in the second string is {" w"," wo","wor","ord"}, and the similarity is 0.8.

This function returns a value that can be approximately understood as the greatest similarity between the first string and any substring of the second string. However, this function does not add padding to the boundaries of the extent. Thus, the number of additional characters present in the second string is not considered, except for the mismatched word boundaries.

At the same time, strict_word_similarity selects an extent of words in the second string. In the example above, strict_word_similarity would select the extent of a single word 'words', whose set of trigrams is {" w"," wo","wor","ord","rds","ds "}. # SELECT strict_word_similarity('word', 'two words'), similarity('word', 'words'); strict_word_similarity | similarity ------------------------+------------ 0.571429 | 0.571429 (1 row)

Thus, the strict_word_similarity function is useful for finding the similarity to whole words, while word_similarity is more suitable for finding the similarity for parts of words.

| Operator Description | | --- | | text % text boolean Returns true if its arguments have a similarity that is greater than the current similarity threshold set by pg_trgm.similarity_threshold. | | text <% text boolean Returns true if the similarity between the trigram set in the first argument and a continuous extent of an ordered trigram set in the second argument is greater than the current word similarity threshold set by pg_trgm.word_similarity_threshold parameter. | | text %> text boolean Commutator of the <% operator. | | text <<% text boolean Returns true if its second argument has a continuous extent of an ordered trigram set that matches word boundaries, and its similarity to the trigram set of the first argument is greater than the current strict word similarity threshold set by the pg_trgm.strict_word_similarity_threshold parameter. | | text %>> text boolean Commutator of the <<% operator. | | text <-> text real Returns the “distance” between the arguments, that is one minus the similarity() value. | | text <<-> text real Returns the “distance” between the arguments, that is one minus the word_similarity() value. | | text <->> text real Commutator of the <<-> operator. | | text <<<-> text real Returns the “distance” between the arguments, that is one minus the strict_word_similarity() value. | | text <->>> text real Commutator of the <<<-> operator. |

Back