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

可以使用 OData $select 参数选择要包含在 Azure 认知搜索的搜索结果中的字段。You can use the OData $select parameter to choose which fields to include in search results from Azure Cognitive Search. 本文详细介绍 $select 的语法。This article describes the syntax of $select in detail. 有关如何在呈现搜索结果时使用 $select 的更多常规信息,请参阅如何在 Azure 认知搜索中使用搜索结果For more general information about how to use $select when presenting search results, see How to work with search results in Azure Cognitive Search.

语法Syntax

$select 参数确定在查询结果集中返回每个文档的哪些字段。The $select parameter determines which fields for each document are returned in the query result set. 以下 EBNF(扩展巴科斯-瑙尔范式)定义了 $select 参数的语法:The following EBNF (Extended Backus-Naur Form) defines the grammar for the $select parameter:

select_expression ::= '*' | field_path(',' field_path)*

field_path ::= identifier('/'identifier)*

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

$select 参数有两种形式:The $select parameter comes in two forms:

  1. 单个星 (*),指示应返回所有可检索字段,或A single star (*), indicating that all retrievable fields should be returned, or
  2. 以逗号分隔的字段路径列表,标识应返回的字段。A comma-separated list of field paths, identifying which fields should be returned.

使用第二种形式时,只能在列表中指定可检索字段。When using the second form, you may only specify retrievable fields in the list.

如果列出一个复杂字段而未显式指定其子字段,则所有可检索的子字段都将包含在查询结果集中。If you list a complex field without specifying its sub-fields explicitly, all retrievable sub-fields will be included in the query result set. 例如,假设索引有一个 Address 字段,其中 StreetCityCountry 子字段都是可检索的。For example, assume your index has an Address field with Street, City, and Country sub-fields that are all retrievable. 如果在 $select 中指定 Address,查询结果将包括所有三个子字段。If you specify Address in $select, the query results will include all three sub-fields.

示例Examples

在结果中包括 HotelIdHotelNameRating 顶级字段,以及 AddressCity 子字段:Include the HotelId, HotelName, and Rating top-level fields in the results, as well as the City sub-field of Address:

    $select=HotelId, HotelName, Rating, Address/City

示例结果可能如下所示:An example result might look like this:

{
  "HotelId": "1",
  "HotelName": "Secret Point Motel",
  "Rating": 4,
  "Address": {
    "City": "New York"
  }
}

在结果中包括 HotelName 顶级字段,以及 Address 的所有子字段,以及 Rooms 集合中每个对象的 TypeBaseRate 子字段:Include the HotelName top-level field in the results, as well as all sub-fields of Address, and the Type and BaseRate sub-fields of each object in the Rooms collection:

    $select=HotelName, Address, Rooms/Type, Rooms/BaseRate

示例结果可能如下所示:An example result might look like this:

{
  "HotelName": "Secret Point Motel",
  "Rating": 4,
  "Address": {
    "StreetAddress": "677 5th Ave",
    "City": "New York",
    "StateProvince": "NY",
    "Country": "USA",
    "PostalCode": "10022"
  },
  "Rooms": [
    {
      "Type": "Budget Room",
      "BaseRate": 9.69
    },
    {
      "Type": "Budget Room",
      "BaseRate": 8.09
    }
  ]
}

后续步骤Next steps