【问题标题】:SQL Server full text search on column with aliasSQL Server 对具有别名的列进行全文搜索
【发布时间】:2018-03-15 07:29:27
【问题描述】:

我正在使用全文搜索,它在表的直接列上工作正常,但在派生/别名列上工作正常。

SELECT ExpectationId
    ,ExpectationName
    ,(
       CASE 
          WHEN ExpectationOrganization_OrganizationId IS NOT NULL
                THEN (
                       SELECT OrganizationName
                       FROM Organizations
                       WHERE OrganizationId = ExpectationOrganization_OrganizationId
                       )
          WHEN ExpectationBeneficiary_BeneficiaryId IS NOT NULL
                THEN (
                       SELECT BeneficiaryName
                       FROM Beneficiaries
                       WHERE BeneficiaryId = ExpectationBeneficiary_BeneficiaryId
                       )
          ELSE (
                    SELECT TeamName
                    FROM Teams
                    WHERE TeamId = ExpectationTeam_TeamId
                    )
          END
       ) AS ParentName
FROM Expectations
WHERE
    FREETEXT(ExpectationName, @Keyword) ---Working
    OR FREETEXT(ParentName, @Keyword) ---Not working

ExpectationNameOrganizationNameBeneficiaryNameTeamName 的所有这些列都被全文索引。

如何使它适用于ParentName 列?

【问题讨论】:

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


    【解决方案1】:

    您需要首先根据您的查询创建一个VIEW,然后向其中添加一个全文索引,其中将包括ParentName 列。如果没有对正在搜索的列的全文索引,FREETEXTCONTAINS 都将不起作用。

    类似的东西应该可以帮助你:

    CREATE VIEW ExpectationsView AS
    SELECT ExpectationId
        ,ExpectationName
        ,(
           CASE 
              WHEN ExpectationOrganization_OrganizationId IS NOT NULL
                    THEN (
                           SELECT OrganizationName
                           FROM Organizations
                           WHERE OrganizationId = ExpectationOrganization_OrganizationId
                           )
              WHEN ExpectationBeneficiary_BeneficiaryId IS NOT NULL
                    THEN (
                           SELECT BeneficiaryName
                           FROM Beneficiaries
                           WHERE BeneficiaryId = ExpectationBeneficiary_BeneficiaryId
                           )
              ELSE (
                        SELECT TeamName
                        FROM Teams
                        WHERE TeamId = ExpectationTeam_TeamId
                        )
              END
           ) AS ParentName
    FROM Expectations
    GO
    
    -- This index is needed for FTS index.
    -- Note, I trust ExpectationId column is unique in your SELECT above,
    -- if it's not, the below CREATE INDEX will fail and you will need to provide 
    -- a new column to your VIEW which will uniquely identify each row, then use 
    -- that PK-like column in the below index
    CREATE UNIQUE CLUSTERED INDEX PK_ExpectationsView   
        ON ExpectationsView (ExpectationId);  
    GO  
    
    CREATE FULLTEXT CATALOG fts_catalog;  
    GO  
    CREATE FULLTEXT INDEX ON ExpectationsView  
     (
        ExpectationName Language 1033,   
        ParentName Language 1033
     )   
      KEY INDEX PK_ExpectationsView 
          ON fts_catalog;
          WITH (CHANGE_TRACKING = AUTO)
    GO  
    

    一旦包含相关列的全文索引在那里,您就可以在查询中使用FREETEXTCONTAINS

    SELECT ExpectationId, ExpectationName, ParentName FROM ExpectationsView
        WHERE FREETEXT(ExpectationName, @Keyword) OR FREETEXT(ParentName, @Keyword)
    

    请注意,上面的代码是我在脑海中提供的,因为我没有适用于您的案例的数据架构,因此无法尝试运行它。但是,它应该为您提供有关如何进行的总体思路。 HTH。

    【讨论】:

    • 还有一个问题:"FREETEXT(ExpectationName, @Keyword) OR FREETEXT(ParentName, @Keyword)""FREETEXT((ExpectationName ,ParentName), @Keyword)" 还是会给出相同的结果?
    • @AliShahzad 我认为不同之处在于性能。在第一种情况下,您将不得不发出 2 个FREETEXT 调用,而在第二种情况下只有 1 个。您需要实际尝试一下,看看实际结果是否不同。请注意,您还可以在视图级别组合 2 列,并在视图中已组合的列上构建和索引。
    • @AliShahzad 如果答案有帮助将不胜感激接受/投票:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    相关资源
    最近更新 更多