Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (中文) » SQL Anywhere 服务器 - SQL 的用法 » 查询和修改数据 » 查询数据 » 全文搜索的类型

 

术语和短语搜索

执行术语列表的全文搜索时,除非多个术语处于一个短语之中,否则术语的顺序不重要。如果将术语放在一个短语之中,数据库服务器将严格按照指定术语时的相同顺序和相对位置来查找这些术语。

执行术语或短语搜索时,如果术语因为超出术语长度设置或位于非索引字表之中而从查询中删除,则返回的行数可能与预期不符。这是因为从查询中删除术语相当于更改搜索条件。例如,如果搜索短语 ['"grown cotton"'] 并且 grown 位于非索引字表中,将得到所有包含 cotton 的索引行。

只要视作 CONTAINS 子句语法关键字的术语在短语之中,即可搜索它们。

术语搜索

在 demo 数据库中,MarketingInformation 表的 Description 列中创建了名为 MarketingTextIndex 的文本索引。以下语句查询 MarketingInformation.Description 列,并返回 Description 列中包含术语 cotton 的值所在的行。

SELECT ID, Description 
   FROM MarketingInformation
   WHERE CONTAINS ( Description, 'cotton' );
ID Description
906 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Visor</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown cotton construction. Shields against sun and precipitation.cotton Metallic ions in the fibers inhibit bacterial growth, and help neutralize odor.</span></p></body></html>
908 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Sweatshirt</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown cotton hooded sweatshirt with taped neck seams. Comes pre-washed for softness and to lessen shrinkage.</span></p></body></html>
909 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Sweatshirt</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Top-notch construction includes durable topstitched seams for strength with low-bulk, resilient rib-knit collar, cuffs and bottom. An 80% cotton/20% polyester blend makes it easy to keep them clean.</span></p></body></html>
910 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Shorts</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>These quick-drying cotton shorts provide all day comfort on or off the trails. Now with a more comfortable and stretchy fabric and an adjustable drawstring waist.</span></p></body></html>

以下示例查询 MarketingInformation 表,并为每一行返回一个指示 Description 列中的值是否包含术语 cotton 的值。

SELECT ID, IF CONTAINS ( Description, 'cotton' )
      THEN 1
      ELSE 0
      ENDIF AS Results
   FROM MarketingInformation;
ID Results
901 0
902 0
903 0
904 0
905 0
906 1
907 0
908 1
909 1
910 1

下一示例在 MarketingInformation 表中查询 Description 列中含有术语 cotton 的项目,然后显示各个匹配结果的分数。

SELECT ID, ct.score, Description
   FROM MarketingInformation CONTAINS ( MarketingInformation.Description, 'cotton' ) as ct
   ORDER BY ct.score DESC;
ID score Description
908 0.9461597363521859 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Sweatshirt</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown cotton hooded sweatshirt with taped neck seams. Comes pre-washed for softness and to lessen shrinkage.</span></p></body></html>
910 0.9244136988525732 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Shorts</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>These quick-drying cotton shorts provide all day comfort on or off the trails. Now with a more comfortable and stretchy fabric and an adjustable drawstring waist.</span></p></body></html>
906 0.9134171046194403 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Visor</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown cotton construction. Shields against sun and precipitation. Metallic ions in the fibers inhibit bacterial growth, and help neutralize odor.</span></p></body></html>
909 0.8856420222728282 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Sweatshirt</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Top-notch construction includes durable topstitched seams for strength with low-bulk, resilient rib-knit collar, cuffs and bottom. An 80% cotton/20% polyester blend makes it easy to keep them clean.</span></p></body></html>

有关在查询的 FROM 子句中使用 CONTAINS 时对结果进行计分的详细信息,请参见对全文搜索结果进行计分

短语搜索

对短语执行全文搜索时,将该短语括在双引号中。如果某一列包含具有指定顺序和相对位置的术语,则该列是匹配的。

不能指定 CONTAINS 关键字(如 AND 或 FUZZY )作为要搜索的术语,除非将它们放在短语之内(允许只包含一个术语的短语)。例如,即使 NOT 是 CONTAINS 关键字,以下语句也是可接受的。有关 CONTAINS 关键字和特殊字符的列表,请参见CONTAINS 搜索条件

SELECT * FROM table-name CONTAINS ( Remarks, '"NOT"' );

如果特殊字符位于短语之中,则不会将其解释为特殊字符,但星号例外。

短语不能用作邻近搜索的参数。

以下语句在 MarketingInformation.Description 中查询短语 ["grown cotton"],然后显示各个匹配结果的分数:

SELECT ID, ct.score, Description
   FROM MarketingInformation CONTAINS ( MarketingInformation.Description, '"grown cotton"' ) as ct
   ORDER BY ct.score DESC;
ID score Description
908 1.6619019465461564 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Sweatshirt</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown cotton hooded sweatshirt with taped neck seams. Comes pre-washed for softness and to lessen shrinkage.</span></p></body></html>
906 1.6043904700786786 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Visor</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown cotton construction. Shields against sun and precipitation. Metallic ions in the fibers inhibit bacterial growth, and help neutralize odor.</span></p></body></html>