在查询的 FROM 子句中使用 CONTAINS 子句时,各个匹配都会有与之相关联的分数。分数表明了匹配的近似程度,因此可以使用分数信息来排序数据。
计分过程基于两个主要标准:
术语在索引行中出现的次数 术语在索引行中出现的次数越多,其分数就越高。
术语在文本索引中出现的次数 术语在文本索引中出现的次数越多,其分数就越低。在 Sybase Central 中,可通过查看文本索引的 [词汇] 选项卡来了解术语在文本索引中的出现次数。选择 term 列可按字母顺序对术语进行排序。查看 freq 列可了解术语在文本索引中出现的次数。
根据全文搜索类型的不同,其它标准也可能影响计分过程。例如,在邻近搜索中,搜索术语的接近程度会影响计分过程。
缺省情况下,CONTAINS 子句的结果集具有相关名 contains,其中包含一个名为 score 的列。可以引用 SELECT 列表、ORDER BY 子句或查询的其它部分中的 "contains".score
。但是,由于 contains 是 SQL 保留字,因此一定要将其放在双引号之中。也可以指定另一相关名(例如 [CONTAINS ( expression ) AS ct
])。在全文搜索的文档示例中,分数列引用为 ct.score
。
以下语句在 MarketingInformation.Description 中搜索以 stretch 开头或以 comfort 开头的术语:
SELECT ID, ct.score, Description FROM MarketingInformation CONTAINS ( MarketingInformation.Description, 'stretch* | comfort*' ) AS ct ORDER BY ct.score DESC; |
ID | score | Description |
---|---|---|
910 | 5.570408968026068 | <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> |
907 | 3.658418186470189 | <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'>A polycarbonate visor with an abrasion-resistant coating on the outside. Great
for jogging in the spring, summer, and early fall. The elastic headband has plenty of stretch to give you a snug yet comfortable fit every time you wear it.</span></p></body></html> |
905 | 1.6750365447462499 | <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Baseball Cap</title></head><body
lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>A lightweight wool cap with mesh side vents for breathable
comfort during aerobic activities. Moisture-absorbing headband liner.</span></p></body></html> |
项目 910 的分数最高,因为它包含前缀术语 comfort 的两个实例,而其它项目仅包含一个实例。同样,项目 910 包含前缀术语 stretch. 的一个实例。
以下示例说明了如何在多列之间执行全文搜索并对结果进行计分:
在 Products 表上创建快速文本索引,如下所示:
CREATE TEXT INDEX scoringExampleMult ON Products ( Description, Name ); |
对 Description 和 Name 列执行全文搜索,查找术语 cap 或 visor,如下所示。CONTAINS 子句的结果会被赋予相关名 ct,并在 SELECT 列表中引用,因此包括在结果集中。此外,ORDER BY 子句中也引用 ct.score 列,以按照分数对结果进行降序排序。
SELECT Products.Description, Products.Name, ct.score FROM Products CONTAINS ( Products.Description, Products.Name, 'cap OR visor' ) ct ORDER BY ct.score DESC; |
Description | Name | score |
---|---|---|
Cloth Visor | Visor | 3.5635154905713042 |
Plastic Visor | Visor | 3.4507856451176244 |
Wool cap | Baseball Cap | 3.2340501745357333 |
Cotton Cap | Baseball Cap | 3.090467108972918 |
计算多列搜索的分数,就好像将列值连接一起并作为单个值建立索引一样。但是,请注意,短语和 NEAR 运算符的匹配范围从来都不会跨越列边界,并且在多个列中出现的搜索术语会使分数增加,使其大于在单个连接值中的分数。
为使本文档中的其它示例能够正确执行,必须删除在 Products 表上创建的文本索引。为此,请执行以下语句:
DROP TEXT INDEX scoringExampleMult ON Products; |
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |