简体   繁体   English

在SQL Server 2008中对联接的层次记录进行全文本搜索

[英]Full-text Search on Joined, Hierarchical Records in SQL Server 2008

Probably a noob question, but I'll go for it nevertheless. 可能是一个菜鸟问题,不过我还是去解决。

For sake of example, I have a Person table, a Tag table and a ContactMethod table. 例如,我有一个Person表,一个Tag表和一个ContactMethod表。 A Person will have multiple Tag records and multiple ContactMethod records associated with them. 一个Person将具有多个Tag记录和与之关联的多个ContactMethod记录。

I'd like to have a forgiving search which will search among several fields from each table. 我想要一个宽容的搜索,它将在每个表的几个字段中搜索。 So I can find a person by their email (via ContactMethod), their name (via Person) or a tag assigned to them. 因此,我可以通过他们的电子邮件(通过ContactMethod),他们的名字(通过Person)或分配给他们的标签找到一个人。

As a complete noob to FTS, two approaches come to mind: 作为FTS的完整入门者,我想到两种方法:

  1. Build some complex query which addresses each field individually 建立一些复杂的查询,分别查询每个字段
  2. Build some sort of lookup table which concatenates the fields I want to index and just do a full-text query on that derived table. 建立某种查找表,将我要索引的字段连接起来,然后对该派生表进行全文查询。

(Feel free to edit for clarity; I'm not in it for the rep points.) (为了清晰起见,可以随意进行编辑;代表点不在我这里。)

One possibility is to make a view which has these columns: PersonID, ContentType, Content. 一种可能是制作一个包含以下列的视图:PersonID,ContentType,Content。 ContentType would be something like "Email", "PhoneNumber", etc... and Content would hold that. ContentType类似于“ Email”,“ PhoneNumber”等,而Content可以保存。 You'd be searching on the Content column, and you'd be able to see what the person's ID is. 您将在“内容”列上进行搜索,并且可以看到此人的ID。 I'm not 100% sure how full text search works though, so I'm not sure if you could use that on a view. 我不确定100%如何进行全文搜索,因此不确定是否可以在视图中使用全文搜索。

The FTS can search multiple fields out-of-the-box. FTS可以开箱即用地搜索多个字段 The CONTAINS predicate accepts a list of columns to search. CONTAINS谓词接受要搜索的列的列表 Also CONTAINSTABLE . CONTAINSTABLE

If your sql server supports it you can create an indexed view and full text search that; 如果您的sql server支持,则可以创建索引视图和全文搜索; you can use containstable(*,'"chris"') to read all the columns. 您可以使用containstable(*,'“ chris”')读取所有列。

If it doesn't support it as the fields are all coming from different tables I think for scalability; 如果它不支持它,因为这些字段都来自不同的表,我认为是为了可扩展性。 if you can easily populate the fields into a single row per record in a separate table I would full text search that rather than the individual records. 如果您可以轻松地将字段填充到单独表中每条记录的一行中,那么我将进行全文搜索,而不是单个记录。 You will end up with a less complex FTS catalog and your queries will not need to do 4 full text searches at a time. 您最终将获得一个不太复杂的FTS目录,并且您的查询将不需要一次进行4个全文搜索。 Running lots of separate FTS queries over different tables at the same time is a ticket to query performance issues in my experience. 在我的经验中,同时在不同的表上运行许多单独的FTS查询是一张查询性能问题的票证。 The downside with doing this is you lose the ability to search for Surname on its own; 这样做的缺点是您无法自行搜索姓氏; if that is something you need you might need to look at an alternative. 如果那是您需要的东西,则可能需要寻找替代方法。

In our app we found that the single table was quicker (we can't rely on customers having enterprise sql at hand); 在我们的应用程序中,我们发现单个表更快(我们不能依赖拥有企业sql的客户)。 so we populate the data with spaces into an FTS table through an update sp then our main contact lookup runs a search over the list. 因此,我们通过update sp将带有空格的数据填充到FTS表中,然后我们的主联系人查找对列表进行搜索。 We have two separate searches to handle finding things with precision (ie names or phone numbers) or just for free text. 我们有两个单独的搜索来处理精确查找内容(例如姓名或电话号码)或仅查找自由文本的内容。 The other nice thing about the table is it is relatively easy and low cost to add further columns to the lookup (we have been asked for social security number for example; to do it we just added the column to the update SP and we were away with little or no impact. 该表的另一个好处是,在查询中添加更多列相对容易且成本较低(例如,我们被要求提供社会保险号;为此,我们仅将列添加到了更新SP中,而我们离开了几乎没有影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM