sql全文检索(full-text)用法与注意事项
本文章来总结了SQL Server全文检索(full-text)用法与注意事项,有需要学习与了解sqlserver全文搜索的朋友可参考本文章。
SQL Server全文搜索 一项强大而又让人无可奈何的技术!说他强大,是因为他的检索速度极快,比like快几十上百倍。说他让人无可奈何,是因为很多场合对准确性要求很高,使用者又不能自己很好的组织查询语句,所以查出来的结果让人觉得没有确定性。
原本觉得这技术很不错,但真正研究起来,发现问题还是很多,研究到最后觉得这技术对柳永法(yongfa365)'Blog来说,只会用到对精确度不高的场合,像:CMS系统,大量文章,根据tag进行快速检索,这时他的快速检索优势能发挥出来,其它地方,真不敢用。像OA,各种业务系统,用他搜索不出要的东西,或者尝试很多次搜索,那用户还不郁闷死。暂且把研究结果放上来,以备后查。
前段时间做了个系统,记录客户的地址,下次再输入类似的地址,系统会进行分词,然后搜索看这个客户是不是已经在我们系统里了。感觉全文搜索能实现这样的功能就好了:就是,根据哪些词索引由用户自己定义(如:朝阳区|海淀区|东城区|左家庄|中关村 等,整个北京的地名,也没多少关键词),只对这些词进行索引,这样一来查询速度会更快,全文目录所占的地方也会更小,不知为何SQL Server没提供这样的功能,也许这只是 个例 吧。
全文查询只需要几秒或更少的时间,具体取决于返回的行数。
可以对包含 char、varchar 和 nvarchar 数据的列创建全文索引。也可以对包含格式化二进制数据(如存储在 varbinary(max) 或 image 列中的 Microsoft Word 文档)的列创建全文索引。不能使用 LIKE 谓词来查询格式化的二进制数据。
创建全文索引 创建全文索引的一个前提是,表中必须有这样的一个索引“unique, single-column, non-nullable index”。大多数情况下,主键都会满足这样的要求。 SQL Server这样做的实际要求是: 1. 必须有非空索引(主键都会满足这样的要求)。 2. 索引必须作用在单独一个列上。复合主键是不满足这个要求,一个work around是可以新建立一列,如textid,int型,自增,并建立索引。 提示:如果你的主键过长,笔者也建议新建一个int型自增列,例如主键是GUID的话,那么创建全文索引开销会是非常巨大的。
最简步骤:(以AdventureWorks数据库中的databaselog表中的event字段为例)
代码如下 | 复制代码 |
1、 启用全文索引: use AdventureWorks exec sp_fulltext_database 'enable' 2、 全文索引是存储在指定的文件系统中的,而不是SQLServer中。 exec sp_fulltext_catalog 'Cat_Desc', 'create', 'f:ft' 创建全文索引的目录 3、 对表创建全文索引 exec sp_fulltext_table 'databaselog', 'create', 'Cat_Desc', 'PK_DatabaseLog_DatabaseLogID' 在已有的表上根据已有的索引创建全文索引 4、 对表中的列添加全文索引 exec sp_fulltext_column 'databaselog', 'event', 'add' 5、 表启动完全填充 exec sp_fulltext_table 'databaselog', 'start_full' 6、 执行全文检索 select * from freetexttable(databaselog, event,'ALTER_TABLE'); |
注意事项:
•为了让全文搜索更好的运行,请选用nvarchar,放弃使用varchar 原因见:SQL Server全文搜索关于varchar与nvarchar的问题
•全文搜索不适合对精准度要求很高的场合,如,查找"show.aspx?id=",是查不出来的,推荐使用SQL Server CLR, 教程:SQL Server CLR 极速入门,启用、设计、部署、运行
•全文搜索最好是配合分词组件工作,分词后再使用全文搜索查找,分词组件见:盘古分词
操作:
1.确保服务已启用:在"SQL Server 配置管理器"里设置"SQL Server FullText Search"启动状态为"自动"
2.建全文索引最直观的方法是:右击相关表-->全文索引-->定义全文索引-->然后基本上是下一步就完成了。
最常用语法:
代码如下 | 复制代码 |
SELECT * FROM [test] WHERE CONTAINS(UserName,'柳永法'); |
SQL Server全文搜索关于varchar与nvarchar的问题
SQL Server全文搜索有一个问题,就是记录不全,上周末经过分析发现,
1.字段类型是varchar 且 如果搜索的的词正好是文章的结尾,就搜索不到,解决方法是在他后边再加上一个.让他不是在文章最后就行
2.字段类型换成nvarchar后,问题解决
代码如下 | 复制代码 |
CREATE DATABASE test USE test |
--对test表建全文索引,列选择txtTitle1, txtTitle2。方法如下:
--右击要建全文索引的表-->全文索引-->定义全文索引-->点几下"下一步"直到“选择表列”
-->选中要建立全文索引的列-->下一步-->自动-->创建新目录(写上名称,选位置,其它自便)
-->点几下"下一步"直到完成,这时系统会开始建全文目录,建好没有可以在当前数据库所在:
--展开当前数据库-->存储-->全文目录-->右击,你刚才起的名,如果“重新生成”是灰色,
--说明系统当前正在生成,如果可以点,说明生成完了
代码如下 | 复制代码 |
--针对varchar的列txtTitle1 只要查询里是以 '法' 结尾,都查不出记录来 --在txttitle列末尾随便加一个字符 |
--重建索引后,再执行上面的查询,结果都 是50
--通过以上示例得出结论:只要在设计字段时把varchar改成nvarchar,就可以解决我们的问题,且nvarchar有个好处就是:如果数据库服务器部署在非中文的系统上时,不会出现乱码问题。
详细的全文搜索参考http://msdn.microsoft.com/zh-cn/library/ms142571(v=SQL.100).aspx
全文检索很容易建立,一旦建立,快速的响应将给使用者和用户带来惊喜!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java development: How to implement search engine and full-text retrieval functions, specific code examples are required Search engines and full-text retrieval are important functions in the modern Internet era. Not only do they help users find what they want quickly, they also provide a better user experience for websites and apps. This article will introduce how to use Java to develop search engines and full-text retrieval functions, and provide some specific code examples. Full-text search using Lucene library Lucene is an open source full-text search engine library, developed by ApacheSo

With the advent of the Internet era, full-text search engines have attracted more and more attention. Among countless web pages, documents and data, we need to quickly find the required content, which requires the use of efficient full-text search engines. Go language is a programming language known for its efficiency. Its design goal is to improve code execution efficiency and performance. Therefore, using Go language to write a full-text search engine can greatly improve its operating efficiency and performance. This article will introduce how to use Go language to write a high-performance full-text search engine. 1. Understand full-text search engines

With the development of the Internet, enterprises are faced with increasingly large amounts of text data. How to quickly and accurately retrieve relevant content has become one of the important issues for enterprises in the information field. As an open source search engine based on Lucene, Elasticsearch has the characteristics of high availability, high scalability and fast retrieval, making it one of the preferred solutions for enterprise full-text retrieval. As a popular server-side programming language, PHP can also quickly carry out web development and API development, becoming a partner with Elasticsea

In modern web applications, data volumes are growing, but so are user expectations and access to data. Therefore, search technology is becoming increasingly important to meet user expectations and provide a better user experience. Full-text search is a powerful technology that can quickly index, search, and sort large amounts of data. In this regard, Elasticsearch is a leading open source search engine that provides many advanced features as well as high availability, easy scalability and other advantages. In this article, we will introduce how to use

PHP Study Guide: How to Implement Full-Text Search Function Full-text search function is widely used in modern websites and applications, which allows users to search and retrieve relevant content by keywords. In this article, we will discuss how to implement full-text search functionality using PHP. 1. Preparation Before starting to write code, we need to ensure that Elasticsearch is installed on the server. Elasticsearch is an open source server-side tool for full-text search and analysis. It provides a powerful search engine,

PHP develops full-text retrieval and message search technology for real-time chat function. With the popularity of instant messaging and the expansion of applications, real-time chat function has become an essential feature of many websites and applications. In live chat, users can send and receive messages, and users are allowed to search historical messages to review and find. In order to achieve this function, we can use full-text retrieval and message search technology. Full-text search refers to the technology of quickly searching for keywords in large amounts of text. It can effectively improve the efficiency and accuracy of message search. In PHP development we

Use PHP to develop and implement full-text search and keyword filtering of Baidu Wenxin Yiyan API interface. When developing web applications, we often need to integrate third-party APIs into our projects to provide more functions and services. This article will introduce how to use PHP to develop and implement the full-text search and keyword filtering functions of Baidu Wenxinyiyan API interface. Baidu Wenxin Yiyan is an API interface that provides various categories of sentences. We can perform full-text search based on keywords and keyword filter the returned results. At first, we

Overview of how PHP uses MongoDB to implement full-text retrieval: Full-text retrieval refers to the technology of searching based on keywords or phrases in text data. In traditional relational databases, full-text search usually relies on complex SQL query statements, but using MongoDB can realize the full-text search function more conveniently and efficiently. This article will introduce how to use PHP and MongoDB to realize the full-text search function, and provide code examples. Install the MongoDB extension: Before you begin, you first need to install MongoDB
