Azure 认知搜索中的 OData $orderby 语法OData $orderby syntax in Azure Cognitive Search

可以使用 OData $orderby 参数对 Azure 认知搜索中的搜索结果应用自定义排序顺序。You can use the OData $orderby parameter to apply a custom sort order for search results in Azure Cognitive Search. 本文详细介绍 $orderby 的语法。This article describes the syntax of $orderby in detail. 有关如何在呈现搜索结果时使用 $orderby 的更多常规信息,请参阅如何在 Azure 认知搜索中使用搜索结果For more general information about how to use $orderby when presenting search results, see How to work with search results in Azure Cognitive Search.


$orderby 参数接受最多 32 个 order-by 子句的逗号分隔列表。The $orderby parameter accepts a comma-separated list of up to 32 order-by clauses. 以下 EBNF(扩展巴科斯-瑙尔范式)描述了 order-by 子句的语法:The syntax of an order-by clause is described by the following EBNF (Extended Backus-Naur Form):

order_by_clause ::= (field_path | sortable_function) ('asc' | 'desc')?

sortable_function ::= geo_distance_call | 'search.score()'

下面还提供了交互式语法图:An interactive syntax diagram is also available:

每个子句都有排序条件,可以选择后跟排序方向(asc 表示升序,或 desc 表示降序)。Each clause has sort criteria, optionally followed by a sort direction (asc for ascending or desc for descending). 如果不指定方向,则默认为升序。If you don't specify a direction, the default is ascending. 在字段中有 NULL 值的情况下,如果排序是 asc,则 NULL 值首先出现;如果排序是 desc,则 NULL 值最后出现。If there are null values in the field, null values appear first if the sort is asc and last if the sort is desc.

排序条件可以是 sortable 字段的路径,也可以是对 geo.distancesearch.score 函数的调用。The sort criteria can either be the path of a sortable field or a call to either the geo.distance or the search.score functions.

如果多个文档具有相同的排序条件且未使用 search.score 函数(例如,如果按数字 Rating 字段排序,并且三个文档的评分均为 4 分),则会通过以降序按文档分数排序来打破平分的局面。If multiple documents have the same sort criteria and the search.score function isn't used (for example, if you sort by a numeric Rating field and three documents all have a rating of 4), ties will be broken by document score in descending order. 当文档分数相同时(例如,当请求中没有指定全文搜索查询时),平分文档的相对排序是不确定的。When document scores are the same (for example, when there's no full-text search query specified in the request), then the relative ordering of the tied documents is indeterminate.

可以指定多个排序条件。You can specify multiple sort criteria. 表达式的顺序决定最终排序顺序。The order of expressions determines the final sort order. 例如,若要按分数降序排序,然后按评分排序,语法将为 $orderby=search.score() desc,Rating descFor example, to sort descending by score, followed by Rating, the syntax would be $orderby=search.score() desc,Rating desc.

$orderbygeo.distance 的语法与其在 $filter 中的语法相同。The syntax for geo.distance in $orderby is the same as it is in $filter. 如果在 $orderby 中使用 geo.distance,其应用到的字段必须为 Edm.GeographyPoint 类型,且还必须 sortableWhen using geo.distance in $orderby, the field to which it applies must be of type Edm.GeographyPoint and it must also be sortable.

$orderbysearch.score 的语法为 search.score()The syntax for search.score in $orderby is search.score(). 函数 search.score 不接受任何参数。The function search.score doesn't take any parameters.


按基准费率对酒店进行升序排序:Sort hotels ascending by base rate:

    $orderby=BaseRate asc

按评分对酒店进行降序排序,然后按基准费率对酒店进行升序排序(请记住,升序是默认值):Sort hotels descending by rating, then ascending by base rate (remember that ascending is the default):

    $orderby=Rating desc,BaseRate

按评分对酒店进行降序排序,然后按距给定坐标的距离进行升序排序:Sort hotels descending by rating, then ascending by distance from the given coordinates:

    $orderby=Rating desc,geo.distance(Location, geography'POINT(-122.131577 47.678581)') asc

按 search.score 和评分对酒店进行降序排序,然后按距给定坐标的距离进行升序排序。Sort hotels in descending order by search.score and rating, and then in ascending order by distance from the given coordinates. 在相关性评分和评分相同的两家酒店之间,将距离最近的酒店列在前面:Between two hotels with identical relevance scores and ratings, the closest one is listed first:

    $orderby=search.score() desc,Rating desc,geo.distance(Location, geography'POINT(-122.131577 47.678581)') asc

后续步骤Next steps