在别名为

时间:2018-03-15 07:29:27

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

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

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列?

1 个答案:

答案 0 :(得分:1)

您需要先根据查询创建$('buttonLED'+id).attr('onclick','writeLED(1,1)') ,然后为其添加一个全文索引,其中包含VIEW列。如果没有对正在搜索的列的全文索引,则ParentNameFREETEXT都不起作用。

这样的事情可以帮助你:

CONTAINS

在包含相关列的全文索引之后,您可以在查询中使用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 FREETEXT

CONTAINS

请注意,上面的代码我提供了我的头脑,因为我没有针对您的情况的数据架构,所以无法尝试运行它。但是,它应该为您提供有关如何继续的一般想法。 HTH。