全文搜索文档和相关数据mssql

时间:2010-09-28 08:49:27

标签: sql-server full-text-search freetext

目前正在构建知识库应用程序,并且对于存储和索引文档信息的最佳方式有点不确定。

用户上传文档,在这样做时从下拉列表中选择多个选项(例如类别,主题,区域......,请注意这些并非都是强制性的),他们还会输入一些关键字和文档描述。此时,使用类别表中的id将选定的类别(和其他)作为外键存储在文档表中。 我们希望能够做的是对文档所在的varchar(max)列中的信息以及类别名称,主题名称和区域名称等进行FREETEXTTABLE或CONTAINSTABLE等。

我查看了创建索引视图的选项,但由于对类别列的LEFT JOIN,这是不可能的。因此,我不确定如何能够做到这一点,任何想法都会受到最高的赞赏。

2 个答案:

答案 0 :(得分:0)

我假设你想和两个搜索一起进行。例如,在“汽车维修”类别中查找包含文本“foo”AND的所有文档。

也许您不需要全文发送附加数据,只能使用=或类似?如果附加数据相当小,则可能无法保证全文的复杂性。

但是,如果要在两者上使用全文,请使用存储过程为您提取结果。这里的诀窍是分阶段结果而不是试图直接得到结果集。

这是一个粗略的起点。

-- a staging table variable for the document results
declare @documentResults table (
    Id int,       
    Rank int
)

insert into @documentResults
select d.Id, results.[rank]
from containstable (documents, (text), '"foo*"') results
inner join documents d on results.[key] = d.Id

-- now you have all of the primary keys that match the search criteria
-- whittle this list down to only include keys that are in the correct categories

-- a staging table variable for each the metadata results
declare @categories table (
    Id int        
)

insert into @categories
select results.[KEY]
from containstable (Categories, (Category), '"Automotive Repair*"') results

declare @topics table (
    Id int        
)

insert into @topics
select results.[KEY]
from containstable (Topics, (Topic), '"Automotive Repair*"') results

declare @areas table (
    Id int        
)

insert into @areas
select results.[KEY]
from containstable (Areas, (Area), '"Automotive Repair*"') results


select d.text, c.category, t.topic, a.area
from @results r
inner join documents d on d.Id = r.Id
inner join @categories c on c.Id = d.CategoryId
inner join @topics t on t.Id = d.TopicId
inner join @areas a on a.Id = d.AreaId

答案 1 :(得分:0)

您可以为全文索引创建一个新列,其中包含原始文档以及作为元数据附加的类别。然后,对该列的搜索可以同时搜索文档和类别。您需要创建一个标记系统,使其在文档中保持唯一,但标记本身不可能用作搜索短语。也许是这样的事情:

This is my regular document text. <FTCategory: Automotive Repair> <FTCategory: Transmissions>