简体   繁体   English

SQL Server全文搜索带有别名的列

[英]SQL Server full text search on column with alias

I am using full-text search, its working fine on a direct column of table but not on a derived/aliased column. 我正在使用全文搜索,它可以在表的直接列上正常工作,但不能在派生/别名列上正常工作。

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

All these columns ExpectationName , OrganizationName , BeneficiaryName , TeamName are full-text indexed. 所有这些列ExpectationNameOrganizationNameBeneficiaryNameTeamName都是全文索引的。

How can I make it work for ParentName column? 如何使它适用于ParentName列?

You need to create a VIEW based on your query first then add a Full-Text index to it which will include the ParentName column. 您需要首先基于查询创建一个VIEW ,然后向其中添加全文索引,其中将包含ParentName列。 Without the Full-Text index over a column being searched neither FREETEXT nor CONTAINS will work. 如果没有在列上搜索全文索引,那么FREETEXTCONTAINS都将无法工作。

Something like that should help you: 这样的事情应该可以帮助您:

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  

Once the Full-Text index which includes the relevant columns is there you can use FREETEXT or CONTAINS in queries: 一旦包含相关列的全文本索引就可以在查询中使用FREETEXTCONTAINS

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

Note, the above code I've provided off the top of my head because I don't have data schema for your case so couldn't try running it. 请注意,上面提供的代码是我的头等大事,因为我没有针对您的案例的数据架构,因此无法尝试运行它。 However, it should give you the general idea on how to proceed. 但是,它应该为您提供有关如何进行的总体思路。 HTH. HTH。

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

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