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.
所有这些列ExpectationName,OrganizationName,BeneficiaryName,TeamName都是全文索引。
How can I make it work for ParentName
column?
如何使它适用于ParentName列?
1 个解决方案
#1
1
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. Without the Full-Text index over a column being searched neither FREETEXT
nor CONTAINS
will work.
您需要先根据查询创建一个VIEW,然后为其添加一个Full-Text索引,其中包含ParentName列。如果没有对正在搜索的列的全文索引,则FREETEXT和CONTAINS都不起作用。
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:
一旦包含相关列的全文索引存在,您可以在查询中使用FREETEXT或CONTAINS:
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。
#1
1
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. Without the Full-Text index over a column being searched neither FREETEXT
nor CONTAINS
will work.
您需要先根据查询创建一个VIEW,然后为其添加一个Full-Text索引,其中包含ParentName列。如果没有对正在搜索的列的全文索引,则FREETEXT和CONTAINS都不起作用。
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:
一旦包含相关列的全文索引存在,您可以在查询中使用FREETEXT或CONTAINS:
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。