| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Diagnostics; |
| | 5 | | using System.IO; |
| | 6 | | using System.Text.Json; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Azure.Core; |
| | 10 | | #if EXPERIMENTAL_SERIALIZER |
| | 11 | | using Azure.Core.Serialization; |
| | 12 | | #endif |
| | 13 | |
|
| | 14 | | #pragma warning disable SA1402 // File may only contain a single type |
| | 15 | |
|
| | 16 | | namespace Azure.Search.Documents.Models |
| | 17 | | { |
| | 18 | | // Hide the untyped SuggestResult |
| | 19 | | [CodeGenModel("SuggestResult")] |
| | 20 | | internal partial class SuggestResult { } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// A result containing a document found by a suggestion query, plus |
| | 24 | | /// associated metadata. |
| | 25 | | /// </summary> |
| | 26 | | /// <typeparam name="T"> |
| | 27 | | /// The .NET type that maps to the index schema. Instances of this type can |
| | 28 | | /// be retrieved as documents from the index. |
| | 29 | | /// </typeparam> |
| | 30 | | public class SearchSuggestion<T> |
| | 31 | | { |
| | 32 | | /// <summary> |
| | 33 | | /// The text of the suggestion result. |
| | 34 | | /// </summary> |
| 54 | 35 | | public string Text { get; internal set; } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// The document being suggested. |
| | 39 | | /// </summary> |
| 76 | 40 | | public T Document { get; internal set; } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Initializes a new instance of the SearchSuggestion class. |
| | 44 | | /// </summary> |
| 88 | 45 | | internal SearchSuggestion() { } |
| | 46 | |
|
| | 47 | | #pragma warning disable CS1572 // Not all parameters will be used depending on feature flags |
| | 48 | | /// <summary> |
| | 49 | | /// Deserialize a SearchSuggestion and its model. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="element">A JSON element.</param> |
| | 52 | | /// <param name="serializer"> |
| | 53 | | /// Optional serializer that can be used to customize the serialization |
| | 54 | | /// of strongly typed models. |
| | 55 | | /// </param> |
| | 56 | | /// <param name="options">JSON serializer options.</param> |
| | 57 | | /// <param name="async">Whether to execute sync or async.</param> |
| | 58 | | /// <param name="cancellationToken"> |
| | 59 | | /// Optional <see cref="CancellationToken"/> to propagate notifications |
| | 60 | | /// that the operation should be canceled. |
| | 61 | | /// </param> |
| | 62 | | /// <returns>Deserialized SearchSuggestion.</returns> |
| | 63 | | internal static async Task<SearchSuggestion<T>> DeserializeAsync( |
| | 64 | | JsonElement element, |
| | 65 | | #if EXPERIMENTAL_SERIALIZER |
| | 66 | | ObjectSerializer serializer, |
| | 67 | | #endif |
| | 68 | | JsonSerializerOptions options, |
| | 69 | | bool async, |
| | 70 | | CancellationToken cancellationToken) |
| | 71 | | #pragma warning restore CS1572 |
| | 72 | | { |
| | 73 | | Debug.Assert(options != null); |
| | 74 | |
|
| 44 | 75 | | SearchSuggestion<T> suggestion = new SearchSuggestion<T>(); |
| 132 | 76 | | foreach (JsonProperty prop in element.EnumerateObject()) |
| | 77 | | { |
| 44 | 78 | | if (prop.NameEquals(Constants.SearchTextKeyJson.EncodedUtf8Bytes)) |
| | 79 | | { |
| 44 | 80 | | suggestion.Text = prop.Value.GetString(); |
| 44 | 81 | | break; // We only have one non-model property |
| | 82 | | } |
| | 83 | | } |
| | 84 | |
|
| | 85 | | // Deserialize the model |
| | 86 | | #if EXPERIMENTAL_SERIALIZER |
| 44 | 87 | | if (serializer != null) |
| | 88 | | { |
| 0 | 89 | | using Stream stream = element.ToStream(); |
| 0 | 90 | | T document = async ? |
| 0 | 91 | | (T)await serializer.DeserializeAsync(stream, typeof(T), cancellationToken).ConfigureAwait(false) : |
| 0 | 92 | | (T)serializer.Deserialize(stream, typeof(T), cancellationToken); |
| 0 | 93 | | suggestion.Document = document; |
| 0 | 94 | | } |
| | 95 | | else |
| | 96 | | { |
| | 97 | | #endif |
| | 98 | | T document; |
| 44 | 99 | | if (async) |
| | 100 | | { |
| 22 | 101 | | using Stream stream = element.ToStream(); |
| 22 | 102 | | document = await JsonSerializer.DeserializeAsync<T>(stream, options, cancellationToken).ConfigureAwa |
| 22 | 103 | | } |
| | 104 | | else |
| | 105 | | { |
| 22 | 106 | | document = JsonSerializer.Deserialize<T>(element.GetRawText(), options); |
| | 107 | | } |
| 44 | 108 | | suggestion.Document = document; |
| | 109 | | #if EXPERIMENTAL_SERIALIZER |
| | 110 | | } |
| | 111 | | #endif |
| | 112 | |
|
| 44 | 113 | | return suggestion; |
| 44 | 114 | | } |
| | 115 | | } |
| | 116 | |
|
| | 117 | | public static partial class SearchModelFactory |
| | 118 | | { |
| | 119 | | /// <summary> Initializes a new instance of SearchSuggestion. </summary> |
| | 120 | | /// <typeparam name="T"> |
| | 121 | | /// The .NET type that maps to the index schema. Instances of this type can |
| | 122 | | /// be retrieved as documents from the index. |
| | 123 | | /// </typeparam> |
| | 124 | | /// <param name="document">The document being suggested.</param> |
| | 125 | | /// <param name="text">The text of the suggestion result.</param> |
| | 126 | | /// <returns>A new SuggestResults instance for mocking.</returns> |
| | 127 | | public static SearchSuggestion<T> SearchSuggestion<T>( |
| | 128 | | T document, |
| | 129 | | string text) => |
| | 130 | | new SearchSuggestion<T>() { Document = document, Text = text }; |
| | 131 | | } |
| | 132 | | } |