SQL 服务器对具有别名的列进行全文搜索
SQL Server full text search on column with alias
我正在使用全文搜索,它在 table 的直接列上运行正常,但在 derived/aliased 列上运行不正常。
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
所有这些列 ExpectationName
、OrganizationName
、BeneficiaryName
、TeamName
都是全文索引。
我怎样才能让它适用于 ParentName
列?
您需要先根据您的查询创建一个 VIEW
,然后向其中添加一个 Full-Text 索引,其中将包含 ParentName
列。如果没有搜索列的 Full-Text 索引,FREETEXT
和 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
一旦包含相关列的 Full-Text 索引存在,您就可以在查询中使用 FREETEXT
或 CONTAINS
:
SELECT ExpectationId, ExpectationName, ParentName FROM ExpectationsView
WHERE FREETEXT(ExpectationName, @Keyword) OR FREETEXT(ParentName, @Keyword)
请注意,上面的代码是我凭空提供的,因为我没有适用于您的情况的数据模式,因此无法尝试 运行。但是,它应该让您大致了解如何进行。 HTH.
我正在使用全文搜索,它在 table 的直接列上运行正常,但在 derived/aliased 列上运行不正常。
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
所有这些列 ExpectationName
、OrganizationName
、BeneficiaryName
、TeamName
都是全文索引。
我怎样才能让它适用于 ParentName
列?
您需要先根据您的查询创建一个 VIEW
,然后向其中添加一个 Full-Text 索引,其中将包含 ParentName
列。如果没有搜索列的 Full-Text 索引,FREETEXT
和 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
一旦包含相关列的 Full-Text 索引存在,您就可以在查询中使用 FREETEXT
或 CONTAINS
:
SELECT ExpectationId, ExpectationName, ParentName FROM ExpectationsView
WHERE FREETEXT(ExpectationName, @Keyword) OR FREETEXT(ParentName, @Keyword)
请注意,上面的代码是我凭空提供的,因为我没有适用于您的情况的数据模式,因此无法尝试 运行。但是,它应该让您大致了解如何进行。 HTH.