Search functions for Db2 Text Search
After you update a text search index, you can search using the CONTAINS or SCORE SQL scalar search function or using the xmlcolumn-contains function.
Searches on text search indexes can range from the simple, such as queries for the occurrence of a single word in a title, to the complex, such as queries that use Boolean operators or term boosting. In addition to the operators that allow you to refine the complexity of your search, features such as synonym dictionaries and linguistic support can enhance searches on text search indexes.
- The SQL function CONTAINS and the XML function xmlcolumn-contains, to create queries for specific words or phrases
- The SQL function SCORE, to obtain the relevancy of a found text document
The scalar text search functions, CONTAINS and SCORE, are seamlessly integrated within SQL. You can use the search functions in the same places that you would use standard SQL expressions within SQL queries. The SQL SCORE scalar function returns an indicator of how well the text documents matched a given text search condition. The SELECT phrase of the SQL query determines which information is returned to you.
The CONTAINS function searches for matches of a word or phrase
and can be used with wildcard characters to search for substring matches
in a manner similar to the SQL LIKE predicate and can search for exact
string matches in a manner similar to the SQL = operator. However,
there are key distinctions between using the CONTAINS function and
using the SQL LIKE predicate or the = operator. The LIKE predicate
and the = operator search for patterns in a document, while CONTAINS
uses linguistic processing: that is, it searches for different forms
of the search term. For example, even without using wildcard characters,
searches for the term work
also return documents
containing working
and worked
. Moreover,
you can add a synonym dictionary to the text search index, increasing
the scope of a search. For example, you can group laptop
and ThinkPad
together so they are returned from
searches for notebook computers
. For XML documents,
the XML search argument syntax allows you to search for text inside
tags and attributes. As well, XQuery searches are case sensitive.
Note that the Db2 optimizer estimates how many text documents can be expected to match a CONTAINS predicate and how costly different access plan alternatives will be. The optimizer chooses the cheapest access plan.
The function xmlcolumn-contains is a built-in Db2 function that returns
XML documents from a Db2 XML data column based
on a text search performed by the Db2 Text Search engine. You can use
xmlcolumn-contains in XQuery expressions to retrieve documents based on a search
of specific document elements. For example, if your XML documents contain product descriptions and
prices for toys that you sell, you can use xmlcolumn-contains in an XQuery
expression to search the description and price elements and return only the documents that have the
term outdoors
but not pool
and cost less than $25.00.
There are key distinctions between using the xmlcolumn-contains function and the XQuery contains function. The XQuery contains function searches for a substring inside a string; it looks for an exact match of the search term or phrase. The XQuery xmlcolumn-contains function, however, has similar functionality to the CONTAINS function, except that it operates on XML columns only. As well, it returns XML documents containing the search term or phrase, whereas contains returns only a value such as 1, 0, or NULL to indicate whether the search term was found.