Share via


你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

SearchClient.GetDocumentAsync<T> 方法

定义

从Azure 认知搜索检索文档。 当用户单击特定搜索结果并且希望查找关于该文档的特定详细信息时,这很有用。 一次只能获取一个文档。 使用搜索在单个请求中获取多个文档。 查找文档

public virtual System.Threading.Tasks.Task<Azure.Response<T>> GetDocumentAsync<T> (string key, Azure.Search.Documents.GetDocumentOptions options = default, System.Threading.CancellationToken cancellationToken = default);
abstract member GetDocumentAsync : string * Azure.Search.Documents.GetDocumentOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<'T>>
override this.GetDocumentAsync : string * Azure.Search.Documents.GetDocumentOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<'T>>
Public Overridable Function GetDocumentAsync(Of T) (key As String, Optional options As GetDocumentOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of Response(Of T))

类型参数

T

映射到索引架构的 .NET 类型。 此类型的实例可以作为文档从索引中检索。

参数

key
String

必需。 一个字符串值,用于唯一标识索引中的每个文档。 该密钥有时称为文档 ID。 有关构造有效文档密钥的规则,请参阅 命名 规则。

options
GetDocumentOptions

用于自定义操作行为的选项。

cancellationToken
CancellationToken

可选 CancellationToken ,用于传播应取消操作的通知。

返回

对应于 key的文档。

例外

搜索服务返回失败时引发。

注解

GetDocument<T>(String, GetDocumentOptions, CancellationToken)GetDocumentAsync<T>(String, GetDocumentOptions, CancellationToken) 方法支持通过类型参数 T将 Azure 搜索字段类型映射到 .NET 类型。 请注意,除集合之外的所有搜索字段类型都是可以为 null 的,因此我们建议对 的属性使用可为 null 的类型 T。 类型映射如下所示:

搜索字段类型.NET 类型
Edm.StringString C# 和 F# ) 中的 (字符串
Edm.BooleanNullable<T> (布尔?in C#,\ F#) 中为 Nullable<bool>
Edm.DoubleNullable<T> (双精度?在 C# 中,在 F#) 中为 Null 的浮<点数>
Edm.Int32Nullable<T> (int?在 C# 中,在 F#) 中为 Null 的 int<>
Edm.Int64Nullable<T> (长?在 C#中,在 F#) 中为 Null 的 int64<>
Edm.DateTimeOffsetNullable<T> (DateTimeOffset?在 C# 中,在 F#) 中为 Nullable<DateTimeOffset> ,或 C# 中的 System.Nullable<System.DateTime> (DateTime?,在 F#) 中为 Nullable<DateTime> 。 虽然我们建议使用 DateTimeOffset,但这两种类型都起作用。 检索文档时,DateTime 值将始终采用 UTC 格式。 为文档编制索引时,DateTime 值的解释如下:
UTC DateTime按原样发送到索引。
本地日期时间在发送到索引之前已转换为 UTC。
具有未指定时区的 DateTime假定为 UTC,并按原样发送到索引。
Edm.GeographyPoint Azure.Core.GeoJson.GeoPoint
Edm.ComplexType 可从复杂字段中的 JSON 对象反序列化的任何类型。 这可以是值类型或引用类型,但我们建议使用引用类型,因为复杂字段在Azure 认知搜索中可为 null。
集合 (Edm.String)IEnumerable<T>F#) 中的 (seq<字符串>
集合 (Edm.Boolean) IEnumerable<T>F#) 中的 (seq<bool>
收藏 (Edm.Double) IEnumerable<T>F#) 中的 (seq<float>
Collection (Edm.Int32) IEnumerable<T>F#) 中的 (seq<int>
Collection (Edm.Int64) IEnumerable<T>F#) 中的 (seq<int64>
集合 (Edm.DateTimeOffset) IEnumerable<T> 或在 IEnumerable<T> F#) 中< (seq DateTimeOffset> 或 seq<DateTime> 。 这两种类型都起作用,但我们建议使用 IEnumerable<T>。 有关详细信息,请参阅上述 Edm.DateTimeOffset 上的说明。
集合 (Edm.GeographyPoint) Azure.Core.GeoJson.GeoPoint 的序列 (f#) 中的 seq<GeoPoint>
集合 (Edm.ComplexType) IEnumerable<T> 在 F#) 中 (seq<T> ,其中 T 是从复杂集合字段中的 JSON 对象反序列化的任何类型。 这可以是值类型或引用类型。
还可以使用动态 SearchDocument 作为 , T 我们将尝试将响应有效负载中的 JSON 类型映射到 .NET 类型。 此映射没有索引中精确类型信息的好处,因此映射并不总是正确的。 具体而言,请注意以下情况:
  • 如果没有小数点,则任何没有小数点的数值都将反序列化为 Int32 C# 中的 (int、F#) 中的 int32 或 Int64 C# 中的 (长,否则在 F# ) 中为 int64。
  • 特殊的双精度浮点值(如 NaN 和 Infinity)将反序列化为类型 String 而不是 Double,即使它们位于具有常规浮点值的数组中。
  • 任何 Edm.DateTimeOffset 字段都将反序列化为 DateTimeOffset,而不是 DateTime
  • 任何空 JSON 数组都将反序列化为 C# 中的 (object[] 数组 Object ,在 F#) 中 obj[] 。
  • 复杂字段将以递归方式反序列化为 类型的 SearchDocument实例。 同样,复杂集合字段将反序列化为此类实例的数组。

适用于