Azure 认知搜索中的 moreLikeThis(预览版)moreLikeThis (preview) in Azure Cognitive Search


此功能目前处于公开预览状态。This feature is currently in public preview. 提供的预览版功能不附带服务级别协议,我们不建议将其用于生产工作负荷。Preview functionality is provided without a service level agreement, and is not recommended for production workloads. REST API 版本 2019-05-06-Preview 提供了此功能。The REST API version 2019-05-06-Preview provides this feature. 目前不支持门户或 .NET SDK。There is currently no portal or .NET SDK support.

moreLikeThis=[key]搜索文档 API 中的查询参数,用于查找与文档键所指定文档类似的文档。moreLikeThis=[key] is a query parameter in the Search Documents API that finds documents similar to the document specified by the document key. 当使用 moreLikeThis 生成搜索请求时,使用从最匹配的给定文档中提取的搜索项来生成查询。When a search request is made with moreLikeThis, a query is generated with search terms extracted from the given document that describe that document best. 然后使用生成的查询来生成搜索请求。The generated query is then used to make the search request. 默认情况下,会考虑所有可搜索字段的内容,使用 searchFields 参数指定的限制字段除外。By default, the contents of all searchable fields are considered, minus any restricted fields that you specified using the searchFields parameter. moreLikeThis 参数不能与搜索参数 search=[string] 一起使用。The moreLikeThis parameter cannot be used with the search parameter, search=[string].

默认情况下,会考虑所有顶级的可搜索字段的内容。By default, the contents of all top-level searchable fields are considered. 若要改为指定特定字段,则可使用 searchFields 参数。If you want to specify particular fields instead, you can use the searchFields parameter.

不能在复杂类型的可搜索子字段上使用 MoreLikeThisYou cannot use MoreLikeThis on searchable sub-fields in a complex type.


以下所有示例均使用快速入门:在 Azure 门户中创建搜索索引中的酒店示例。All following examples use the hotels sample from Quickstart: Create a search index in the Azure portal.

简单查询Simple query

以下查询会查找其描述字段与 moreLikeThis 参数指定的源文档字段最相似的文档:The following query finds documents whose description fields are most similar to the field of the source document as specified by the moreLikeThis parameter:

GET /indexes/hotels-sample-index/docs?moreLikeThis=29&searchFields=Description&api-version=2019-05-06-Preview

在此示例中,请求会搜索与 HotelId 为 29 的酒店类似的酒店。In this example, the request searches for hotels similar to the one with HotelId 29. 还可以使用 HTTP POST 调用 MoreLikeThis,而不是使用 HTTP GET:Rather than using HTTP GET, you can also invoke MoreLikeThis using HTTP POST:

POST /indexes/hotels-sample-index/docs/search?api-version=2019-05-06-Preview
      "moreLikeThis": "29",
      "searchFields": "Description"

应用筛选器Apply filters

MoreLikeThis 可以与其他常见查询参数(如 $filter)组合。MoreLikeThis can be combined with other common query parameters like $filter. 例如,可以将查询限制为仅返回类别为“Budget”且评级高于 3.5 的酒店:For instance, the query can be restricted to only hotels whose category is 'Budget' and where the rating is higher than 3.5:

GET /indexes/hotels-sample-index/docs?moreLikeThis=20&searchFields=Description&$filter=(Category eq 'Budget' and Rating gt 3.5)&api-version=2019-05-06-Preview

选择字段并限制结果Select fields and limit results

$top 选择器可用于限制 MoreLikeThis 查询中应返回的结果数。The $top selector can be used to limit how many results should be returned in a MoreLikeThis query. 此外,还可以使用 $select 选择字段。Also, fields can be selected with $select. 此处选择了前三个酒店及其 ID、名称和评级:Here the top three hotels are selected along with their ID, Name, and Rating:

GET /indexes/hotels-sample-index/docs?moreLikeThis=20&searchFields=Description&$filter=(Category eq 'Budget' and Rating gt 3.5)&$top=3&$select=HotelId,HotelName,Rating&api-version=2019-05-06-Preview

后续步骤Next steps

可以使用任何 Web 测试工具来试验此功能。You can use any web testing tool to experiment with this feature. 建议使用 Postman 完成此练习。We recommend using Postman for this exercise.