我很快就需要在我的一个项目中进行全文搜索,因此我开始研究一些搜索引擎..例如Azure搜索,但是...从我到目前为止所看到的来看,没有一个引擎是能够跨多个表搜索以及这些表之间的关系。
例如..我不能简单地要求引擎搜索单词“red”,然后它会“爬行”所有表(或索引表),并返回命中,让我可以逐步浏览其关系数据..
举例来说……假设我们有一张名为“汽车”的表。我们还有另外两个表,一个称为“颜色”,另一个称为“品牌”。现在“汽车”与“颜色”表和“品牌”表有关系/FK。当我使用搜索引擎时,我希望能够搜索所有内容具有所有有效关系设置且颜色为“红色”的汽车,然后能够获取汽车(不仅是颜色数据,而且是实际的汽车数据)。
From what I have seen must search engines (Azure Search included), requires me to "flatten" the relational structure of my data into one single record.. which seems strange since that would end up in quite a lot of data and would be a lot harder to "map" back to my data-entities with relations.
So... any ideas of how to do a full-text search through a relational database (in my case the database would be an SQL Server database)?
Br, Inx
创建扁平化实体是最好的方法,如果有的话,为了正确地跨多个表的全文搜索进行评分。我的意思是,可以在 1 个查询中搜索多个表(例如:),SELECT * FROM CONTAINSTABLE(Cars,*,'sedan') join CONTAINSTABLE(Brands,*,'honda') on...
但每次搜索的分数范围会有很大差异,因此计算可靠的统一分数是极其困难到不可能的。对于扁平化的实体来说,这不是问题。
扁平化实体不会(也不应该)替换您现有的表。它应该是数据库中的一个单独的表。为了保持同步,您可以 1) 创建一个定期插入/更新它的作业,或者 2) 您可以利用更改跟踪来实现更实时的功能。
展平的实体应包含允许您映射回其他实体的列。汽车扁平化实体示例:
Cars_FullText
-------------
CarId <-- these 3 columns from the Cars table
CarName*
CarDescription
ColorId <-- these 2 columns from the Colors lookup table
ColorName*
BrandId <-- these 2 columns from the Brands lookup table
BrandName* ...
* this column is full text indexed
是的,您的数据库将需要更多存储空间。是的,这确实为系统添加了另一个部分。但考虑到 SQL Server 全文索引的局限性以及我之前提到的跨多个表的分数问题,我认为这是最好的方法。