使用语义搜索来查找相似和相关文档Find Similar and Related Documents with Semantic Search

适用对象:是SQL Server 否Azure SQL 数据库 否Azure Synapse Analytics (SQL DW) 否并行数据仓库APPLIES TO: yesSQL Server noAzure SQL Database noAzure Synapse Analytics (SQL DW) noParallel Data Warehouse

说明在为统计语义索引配置的列上如何查找相似或相关的文档或文本值,以及如何查找其相似或相关程度的信息。Describes how to find similar or related documents or text values, and information about how they are similar or related, in columns that are configured for statistical semantic indexing.

使用 SEMANTICSIMILARITYTABLE 查找相似或相关文档Find similar or related documents with SEMANTICSIMILARITYTABLE

若要标识特定列中相似或相关文档,请查询函数 semanticsimilaritytable (Transact-SQL)To identify similar or related documents in a specific column, query the function semanticsimilaritytable (Transact-SQL).

SEMANTICSIMILARITYTABLE 返回一个表,该表由指定列中其内容在语义上类似于指定文档的零个、一个或多个行构成。SEMANTICSIMILARITYTABLE returns a table of zero, one, or more rows whose content in the specified column is semantically similar to the specified document. 可以在 SELECT 语句的 FROM 子句中像引用常规表名那样引用此行集函数。This rowset function can be referenced in the FROM clause of a SELECT statement like a regular table name.

不能跨列查询相似的文档。You cannot query across columns for similar documents. SEMANTICSIMILARITYTABLE 函数只从与源列相同的列检索结果,源列由 source_key 参数标识。The SEMANTICSIMILARITYTABLE function only retrieves results from the same column as the source column, which is identified by the source_key argument.

有关 SEMANTICSIMILARITYTABLE 函数所需的参数和它返回的结果表的详细信息,请参阅 semanticsimilaritytable (Transact-SQL)For detailed information about the parameters required by the SEMANTICSIMILARITYTABLE function, and about the table of results that it returns, see semanticsimilaritytable (Transact-SQL).

重要

针对的列必须启用了全文索引和语义索引。The columns that you target must have full-text and semantic indexing enabled.

示例:查找与另一个文档最相似的文档Example: Find the top documents that are similar to another document

以下示例从 AdventureWorks2012 示例数据库的 HumanResources.JobCandidate 表中检索与按 @CandidateID 指定的候选人最相似的 10 个候选人。The following example retrieves the top 10 candidates who are similar to the candidate specified by @CandidateID from the HumanResources.JobCandidate table in the AdventureWorks2012 sample database.

SELECT TOP(10) KEY_TBL.matched_document_key AS Candidate_ID  
FROM SEMANTICSIMILARITYTABLE  
    (  
    HumanResources.JobCandidate,  
    Resume,  
    @CandidateID  
    ) AS KEY_TBL  
ORDER BY KEY_TBL.score DESC;  
GO  

使用 SEMANTICSIMILARITYDETAILSTABLE 查找有关文档相似或相关程度的信息Find info about how documents are similar or related with SEMANTICSIMILARITYDETAILSTABLE

若要获取使文档相似或相关的关键短语的信息,可以查询函数 semanticsimilaritydetailstable (Transact-SQL)To get information about the key phrases that make documents similar or related, you can query the function semanticsimilaritydetailstable (Transact-SQL).

SEMANTICSIMILARITYDETAILSTABLE 返回一个表,该表包含其内容在语义上相似的两个文档(源文档和匹配的文档)共有的关键短语的零个、一个或多个行。SEMANTICSIMILARITYDETAILSTABLE returns a table of zero, one, or more rows of key phrases common across two documents (a source document and a matched document) whose content is semantically similar. 可以在 SELECT 语句的 FROM 子句中像引用常规表名那样引用此行集函数。This rowset function can be referenced in the FROM clause of a SELECT statement like a regular table name.

有关 SEMANTICSIMILARITYDETAILSTABLE 函数所需的参数和它返回的结果表的详细信息,请参阅 semanticsimilaritydetailstable (Transact-SQL)For detailed information about the parameters required by the SEMANTICSIMILARITYDETAILSTABLE function, and about the table of results that it returns, see semanticsimilaritydetailstable (Transact-SQL).

重要

针对的列必须启用了全文索引和语义索引。The columns that you target must have full-text and semantic indexing enabled.

示例:查找在文档间最相似的关键短语Example: Find the top key phrases that are similar between documents

以下示例检索 5 个关键短语,它们在 AdventureWorks2012 示例数据库的 HumanResources.JobCandidate 表中的两个指定候选人间具有最高的相似性得分。The following example retrieves the 5 key phrases that have the highest similarity score between the specified candidates in HumanResources.JobCandidate table of the AdventureWorks2012 sample database.

SELECT TOP(5) KEY_TBL.keyphrase, KEY_TBL.score  
FROM SEMANTICSIMILARITYDETAILSTABLE  
    (  
    HumanResources.JobCandidate,  
    Resume, @CandidateID,  
    Resume, @MatchedID  
    ) AS KEY_TBL  
ORDER BY KEY_TBL.score DESC;  
GO