SearchClient.GetDocumentAsync<T>(String, GetDocumentOptions, CancellationToken) Method

Definition

Retrieves a document from Azure Cognitive Search. This is useful when a user clicks on a specific search result, and you want to look up specific details about that document. You can only get one document at a time. Use Search to get multiple documents in a single request. Lookup Document

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))

Type Parameters

T

The .NET type that maps to the index schema. Instances of this type can be retrieved as documents from the index.

Parameters

key
System.String

Required. An string value that uniquely identifies each document in the index. The key is sometimes referred to as a document ID. See Naming rules for the rules for constructing valid document keys.

options
GetDocumentOptions

Options to customize the operation's behavior.

cancellationToken
System.Threading.CancellationToken

Optional System.Threading.CancellationToken to propagate notifications that the operation should be canceled.

Returns

System.Threading.Tasks.Task<Response<T>>

The document corresponding to the key.

Exceptions

Thrown when a failure is returned by the Search Service.

Remarks

The GetDocument<T>(String, GetDocumentOptions, CancellationToken) and GetDocumentAsync<T>(String, GetDocumentOptions, CancellationToken) methods support mapping of Azure Search field types to .NET types via the type parameter T. Note that all search field types except collections are nullable, so we recommend using nullable types for the properties of T. The type mapping is as follows:

Search field type.NET type
Edm.StringSystem.String (string in C# and F#)
Edm.BooleanSystem.Nullable`1 (bool? in C#,\ Nullable<bool> in F#)
Edm.DoubleSystem.Nullable`1 (double? in C#, Nullable<float> in F#)
Edm.Int32System.Nullable`1 (int? in C#, Nullable<int> in F#)
Edm.Int64System.Nullable`1 (long? in C#, Nullable<int64> in F#)
Edm.DateTimeOffsetSystem.Nullable`1 (DateTimeOffset? in C#, Nullable<DateTimeOffset> in F#) or System.Nullable<System.DateTime> (DateTime? in C#, Nullable<DateTime> in F#). Both types work, although we recommend using DateTimeOffset. When retrieving documents, DateTime values will always be in UTC. When indexing documents, DateTime values are interpreted as follows:
UTC DateTimeSent as-is to the index.
Local DateTimeConverted to UTC before being sent to the index.
DateTime with unspecified time zoneAssumed to be UTC and sent as-is to the index.
Edm.GeographyPoint Azure.Core.GeoJson.GeoPoint
Edm.ComplexType Any type that can be deserialized from the JSON objects in the complex field. This can be a value type or a reference type, but we recommend using a reference type since complex fields are nullable in Azure Cognitive Search.
Collection(Edm.String)System.Collections.Generic.IEnumerable`1 (seq<string> in F#)
Collection(Edm.Boolean)System.Collections.Generic.IEnumerable`1 (seq<bool> in F#)
Collection(Edm.Double)System.Collections.Generic.IEnumerable`1 (seq<float> in F#)
Collection(Edm.Int32)System.Collections.Generic.IEnumerable`1 (seq<int> in F#)
Collection(Edm.Int64)System.Collections.Generic.IEnumerable`1 (seq<int64> in F#)
Collection(Edm.DateTimeOffset)System.Collections.Generic.IEnumerable`1 or System.Collections.Generic.IEnumerable`1 (seq<DateTimeOffset> or seq<DateTime> in F#). Both types work, although we recommend using System.Collections.Generic.IEnumerable`1. See the notes above on Edm.DateTimeOffset for details.
Collection(Edm.GeographyPoint)sequence of Azure.Core.GeoJson.GeoPoint (seq<GeoPoint> in F#)
Collection(Edm.ComplexType)System.Collections.Generic.IEnumerable`1 (seq<T> in F#) where T is any type that can be deserialized from the JSON objects in the complex collection field. This can be a value type or a reference type.
You can also use the dynamic SearchDocument as your T and we will attempt to map JSON types in the response payload to .NET types. This mapping does not have the benefit of precise type information from the index, so the mapping is not always correct. In particular, be aware of the following cases:
  • Any numeric value without a decimal point will be deserialized to a System.Int32 (int in C#, int32 in F#) if it can be converted or a System.Int64 (long in C#, int64 in F#) otherwise.
  • Special double-precision floating point values such as NaN and Infinity will be deserialized as type System.String rather than System.Double, even if they are in arrays with regular floating point values.
  • Any Edm.DateTimeOffset field will be deserialized as a System.DateTimeOffset, not System.DateTime.
  • Any empty JSON array will be deserialized as an array of System.Object (object[] in C#, obj[] in F#).
  • Complex fields will be recursively deserialized into instances of type SearchDocument. Similarly, complex collection fields will be deserialized into arrays of such instances.

Applies to