To implement full text searching there must be a function to create atsvector
from a document and atsquery
from a user query. Also, we need to return results in a useful order, so we need a function that compares documents with respect to their relevance to the query. It's also important to be able to display the results nicely.PostgreSQLprovides support for all of these functions.
PostgreSQLprovides the functionto_tsvector
for converting a document to thetsvector
data type.
to_tsvector
parses a textual document into tokens, reduces the tokens to lexemes, and returns atsvector
which lists the lexemes together with their positions in the document. The document is processed according to the specified or default text search configuration. Here is a simple example:
In the example above we see that the resultingtsvector
does not contain the wordsa
,on
, orit
, the wordrats
becamerat
, and the punctuation sign-
was ignored.
Theto_tsvector
function internally calls a parser which breaks the document text into tokens and assigns a type to each token. For each token, a list of dictionaries (Section 12.6) is consulted, where the list can vary depending on the token type. The first dictionary that_recognizes_the token emits one or more normalized_lexemes_to represent the token. For example,rats
becamerat
because one of the dictionaries recognized that the wordrats
is a plural form ofrat
. Some words are recognized as_stop words_(Section 12.6.1), which causes them to be ignored since they occur too frequently to be useful in searching. In our example these area
,on
, andit
. If no dictionary in the list recognizes the token then it is also ignored. In this example that happened to the punctuation sign-
because there are in fact no dictionaries assigned for its token type (Space symbols
), meaning space tokens will never be indexed. The choices of parser, dictionaries and which types of tokens to index are determined by the selected text search configuration (Section 12.7). It is possible to have many different configurations in the same database, and predefined configurations are available for various languages. In our example we used the default configurationenglish
for the English language.
The functionsetweight
can be used to label the entries of atsvector
with a given_weight_, where a weight is one of the lettersA
,B
,C
, orD
. This is typically used to mark entries coming from different parts of a document, such as title versus body. Later, this information can be used for ranking of search results.
Becauseto_tsvector
(NULL
) will returnNULL
, it is recommended to usecoalesce
whenever a field might be null. Here is the recommended method for creating atsvector
from a structured document:
Here we have usedsetweight
to label the source of each lexeme in the finishedtsvector
, and then merged the labeledtsvector
values using thetsvector
concatenation operator||
. (Section 12.4.1gives details about these operations.)
PostgreSQLprovides the functionsto_tsquery
,plainto_tsquery
, andphraseto_tsquery
for converting a query to thetsquery
data type.to_tsquery
offers access to more features than eitherplainto_tsquery
orphraseto_tsquery
, but it is less forgiving about its input.
to_tsquery
creates atsquery
value fromquerytext
, which must consist of single tokens separated by thetsquery
operators&
(AND),|
(OR),!
(NOT), and<->
(FOLLOWED BY), possibly grouped using parentheses. In other words, the input toto_tsquery
must already follow the general rules fortsquery
input, as described inSection 8.11.2. The difference is that while basictsquery
input takes the tokens at face value,to_tsquery
normalizes each token into a lexeme using the specified or default configuration, and discards any tokens that are stop words according to the configuration. For example:
As in basictsquery
input, weight(s) can be attached to each lexeme to restrict it to match onlytsvector
lexemes of those weight(s). For example:
Also,*
can be attached to a lexeme to specify prefix matching:
Such a lexeme will match any word in atsvector
that begins with the given string.
to_tsquery
can also accept single-quoted phrases. This is primarily useful when the configuration includes a thesaurus dictionary that may trigger on such phrases. In the example below, a thesaurus contains the rulesupernovae stars : sn
:
Without quotes,to_tsquery
will generate a syntax error for tokens that are not separated by an AND, OR, or FOLLOWED BY operator.
plainto_tsquery
transforms the unformatted text_querytext
_to atsquery
value. The text is parsed and normalized much as forto_tsvector
, then the&
(AND)tsquery
operator is inserted between surviving words.
Example:
Note thatplainto_tsquery
will not recognizetsquery
operators, weight labels, or prefix-match labels in its input:
Here, all the input punctuation was discarded as being space symbols.
phraseto_tsquery
behaves much likeplainto_tsquery
, except that it inserts the<->
(FOLLOWED BY) operator between surviving words instead of the&
(AND) operator. Also, stop words are not simply discarded, but are accounted for by inserting<N
>operators rather than<->
operators. This function is useful when searching for exact lexeme sequences, since the FOLLOWED BY operators check lexeme order not just the presence of all the lexemes.
Example:
Likeplainto_tsquery
, thephraseto_tsquery
function will not recognizetsquery
operators, weight labels, or prefix-match labels in its input:
Ranking attempts to measure how relevant documents are to a particular query, so that when there are many matches the most relevant ones can be shown first.PostgreSQLprovides two predefined ranking functions, which take into account lexical, proximity, and structural information; that is, they consider how often the query terms appear in the document, how close together the terms are in the document, and how important is the part of the document where they occur. However, the concept of relevancy is vague and very application-specific. Different applications might require additional information for ranking, e.g., document modification time. The built-in ranking functions are only examples. You can write your own ranking functions and/or combine their results with additional factors to fit your specific needs.
The two ranking functions currently available are:
ts_rank([
weights
float4[]
,
]
vector
tsvector
,
query
tsquery
[
,
normalization
integer
]) returns
float4
Ranks vectors based on the frequency of their matching lexemes.
ts_rank_cd([
weights
float4[]
,
]
vector
tsvector
,
query
tsquery
[
,
normalization
integer
]) returns
float4
This function computes the_cover density_ranking for the given document vector and query, as described in Clarke, Cormack, and Tudhope's "Relevance Ranking for One to Three Term Queries" in the journal "Information Processing and Management", 1999. Cover density is similar tots_rank
ranking except that the proximity of matching lexemes to each other is taken into consideration.
This function requires lexeme positional information to perform its calculation. Therefore, it ignores any“stripped”lexemes in thetsvector
. If there are no unstripped lexemes in the input, the result will be zero. (SeeSection 12.4.1for more information about thestrip
function and positional information intsvector
s.)
For both these functions, the optional_weights
_argument offers the ability to weigh word instances more or less heavily depending on how they are labeled. The weight arrays specify how heavily to weigh each category of word, in the order:
If no_weights
_are provided, then these defaults are used:
Typically weights are used to mark words from special areas of the document, like the title or an initial abstract, so they can be treated with more or less importance than words in the document body.
Since a longer document has a greater chance of containing a query term it is reasonable to take into account document size, e.g., a hundred-word document with five instances of a search word is probably more relevant than a thousand-word document with five instances. Both ranking functions take an integer_normalization
_option that specifies whether and how a document's length should impact its rank. The integer option controls several behaviors, so it is a bit mask: you can specify one or more behaviors using|
(for example,2|4
).
0 (the default) ignores the document length
1 divides the rank by 1 + the logarithm of the document length
2 divides the rank by the document length
4 divides the rank by the mean harmonic distance between extents (this is implemented only byts_rank_cd
)
8 divides the rank by the number of unique words in document
16 divides the rank by 1 + the logarithm of the number of unique words in document
32 divides the rank by itself + 1
If more than one flag bit is specified, the transformations are applied in the order listed.
It is important to note that the ranking functions do not use any global information, so it is impossible to produce a fair normalization to 1% or 100% as sometimes desired. Normalization option 32 (rank/(rank+1)
) can be applied to scale all ranks into the range zero to one, but of course this is just a cosmetic change; it will not affect the ordering of the search results.
Here is an example that selects only the ten highest-ranked matches:
This is the same example using normalized ranking:
Ranking can be expensive since it requires consulting thetsvector
of each matching document, which can be I/O bound and therefore slow. Unfortunately, it is almost impossible to avoid since practical queries often result in large numbers of matches.
To present search results it is ideal to show a part of each document and how it is related to the query. Usually, search engines show fragments of the document with marked search terms.PostgreSQLprovides a functionts_headline
that implements this functionality.
ts_headline
accepts a document along with a query, and returns an excerpt from the document in which terms from the query are highlighted. The configuration to be used to parse the document can be specified byconfig
; if_config
_is omitted, thedefault_text_search_config
configuration is used.
If anoptions
_string is specified it must consist of a comma-separated list of one or moreoption=value
_pairs. The available options are:
StartSel
,StopSel
: the strings with which to delimit query words appearing in the document, to distinguish them from other excerpted words. You must double-quote these strings if they contain spaces or commas.
MaxWords
,MinWords
: these numbers determine the longest and shortest headlines to output.
ShortWord
: words of this length or less will be dropped at the start and end of a headline. The default value of three eliminates common English articles.
HighlightAll
: Boolean flag; iftrue
the whole document will be used as the headline, ignoring the preceding three parameters.
MaxFragments
: maximum number of text excerpts or fragments to display. The default value of zero selects a non-fragment-oriented headline generation method. A value greater than zero selects fragment-based headline generation. This method finds text fragments with as many query words as possible and stretches those fragments around the query words. As a result query words are close to the middle of each fragment and have words on each side. Each fragment will be of at mostMaxWords
and words of lengthShortWord
or less are dropped at the start and end of each fragment. If not all query words are found in the document, then a single fragment of the firstMinWords
in the document will be displayed.
FragmentDelimiter
: When more than one fragment is displayed, the fragments will be separated by this string.
Any unspecified options receive these defaults:
For example:
ts_headline
uses the original document, not atsvector
summary, so it can be slow and should be used with care.