简体   繁体   English

SQL Server使用FREETEXTABLE搜索多列

[英]SQL Server search multiple columns with FREETEXTABLE

GOAL : to return a result based on a search of 3 different columns, each with their own search string value and order by RANK , if possible. GOAL :如果可能,返回基于3个不同列的搜索结果,每个列都有自己的搜索字符串值和RANK顺序。

RULES : 规则

  1. Allow any column to be searched 允许搜索任何
  2. Results must contain all search strings on their respective column, unless null is passed 结果必须在其各自的列上包含所有搜索字符串,除非传递null
  3. IF all search strings are null , return empty results 如果所有搜索字符串为null ,则返回空结果

Current stored procedure: 当前存储过程:

I was able to come up with this after much research in order to abide by the rules stated above. 经过大量研究,我能够得出这个结论,以便遵守上述规则。

ALTER PROCEDURE [Application].[usp_Institution_Search] 
    @RowCount INT = 100,
    @Name NVARCHAR(255), 
    @City NVARCHAR(255),
    @Country NVARCHAR(255) 
AS
BEGIN
    SET NOCOUNT ON;

    IF ISNULL(@Name, '') = '' SET @Name = '""' 
    IF ISNULL(@City, '') = '' SET @City = '""' 
    IF ISNULL(@Country, '') = '' SET @Country = '""' 

    SELECT TOP (@RowCount) 
        [i].* 
    FROM 
        [dbo].[Institutions] [i] 
    WHERE 
        ((NULLIF(@Name, '""') IS NULL OR FREETEXT([i].[InstitutionName], @Name)) 
        AND (NULLIF(@City, '""') IS NULL OR FREETEXT([i].[City], @City)) 
        AND (NULLIF(@Country, '""') IS NULL OR FREETEXT([i].[Country], @Country)) 
        AND 
        (
             NULLIF(@Name, '""') IS NOT NULL 
             OR NULLIF(@City, '""') IS NOT NULL 
             OR NULLIF(@Country, '""') IS NOT NULL)
        ) 
END

Problems : I was about to order the results by InstitutionName but then I thought about using a RANK instead. 问题 :我本打算按InstitutionName排序结果,但是后来我想到了改用RANK After more research I found that it may be best to use FREETEXTTABLE . 经过更多研究,我发现最好使用FREETEXTTABLE At this point, I'm not sure how best to work with my situation as most results always talk about searching one string against multiple columns instead. 在这一点上,我不确定如何最好地处理我的情况,因为大多数结果总是谈论针对多列搜索一个字符串。

I'm not totally sure how this works, but I suppose I would like the highest combined RANK from the search of all columns. 我不确定这是如何工作的,但我想我希望从所有列的搜索中获得最高的RANK

If this is possible, please show me some examples. 如果可以的话,请向我展示一些示例。

So after a lot of testing I think I like this result. 因此,经过大量测试,我认为我喜欢这个结果。 I'm INNER JOIN ing the required column ( InstitutionName ) and LEFT OUTER JOIN ing the others. 我正在INNER JOIN的必填columnInstitutionName )和LEFT OUTER JOIN的其他列。 Then combine their ranks in the ORDER BY . 然后在ORDER BY合并他们的等级。

I'm not sure if it's as efficient as possible. 我不确定它是否尽可能有效。 If anyone has any input, I'd love to hear from you as I always like to learn more. 如果有人有任何意见,我很乐意听取您的意见,因为我一直喜欢学习更多。

ALTER PROCEDURE [Application].[usp_Institution_Search] 
    @RowCount INT = 100,
    @Name NVARCHAR(255), 
    @City NVARCHAR(255),
    @Country NVARCHAR(255) 
AS
BEGIN
    SET NOCOUNT ON;

    IF ISNULL(@Name, '') = '' SET @Name = '""' 
    IF ISNULL(@City, '') = '' SET @City = '""' 
    IF ISNULL(@Country, '') = '' SET @Country = '""' 

    SELECT TOP (@RowCount) 
        [i].* 
    FROM 
        [dbo].[Institutions] [i] 
        INNER JOIN FREETEXTTABLE([Institutions], [InstitutionName], @Name) AS [ft1] ON [ft1].[Key] = [i].[InstitutionId] 
        LEFT OUTER JOIN FREETEXTTABLE([Institutions], [City], @City) AS [ft2] ON [ft2].[Key] = [i].[InstitutionId] 
        LEFT OUTER JOIN FREETEXTTABLE([Institutions], [Country], @Country) AS [ft3] ON [ft3].[Key] = [i].[InstitutionId] 
    ORDER BY 
        [ft1].[Rank] + ISNULL([ft2].[Rank], 0) + ISNULL([ft3].[Rank], 0) DESC 
        , [i].[InstitutionName] ASC 
END

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

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