| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Collections.ObjectModel; |
| | 6 | | using System.IO; |
| | 7 | | using System.Text.Json; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using Azure.Core; |
| | 11 | | #if EXPERIMENTAL_SERIALIZER |
| | 12 | | using Azure.Core.Serialization; |
| | 13 | | #endif |
| | 14 | |
|
| | 15 | | #pragma warning disable SA1402 // File may only contain a single type |
| | 16 | |
|
| | 17 | | namespace Azure.Search.Documents.Models |
| | 18 | | { |
| | 19 | | // Hide the untyped SuggestDocumentsResult |
| | 20 | | [CodeGenModel("SuggestDocumentsResult")] |
| | 21 | | internal partial class SuggestDocumentsResult { } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Response containing suggestion query results from an index. |
| | 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 partial class SuggestResults<T> |
| | 31 | | { |
| | 32 | | /// <summary> |
| | 33 | | /// A value indicating the percentage of the index that was included in |
| | 34 | | /// the query, or null if minimumCoverage was not set in the request. |
| | 35 | | /// </summary> |
| 8 | 36 | | public double? Coverage { get; internal set; } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// The sequence of suggestions returned by the query. |
| | 40 | | /// </summary> |
| 66 | 41 | | public IReadOnlyList<SearchSuggestion<T>> Results { get; internal set; } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Initializes a new instance of the SuggestResults class. |
| | 45 | | /// </summary> |
| 44 | 46 | | internal SuggestResults() { } |
| | 47 | |
|
| | 48 | | #pragma warning disable CS1572 // Not all parameters will be used depending on feature flags |
| | 49 | | /// <summary> |
| | 50 | | /// Deserialize the SuggestResults. |
| | 51 | | /// </summary> |
| | 52 | | /// <param name="json">A JSON stream.</param> |
| | 53 | | /// <param name="serializer"> |
| | 54 | | /// Optional serializer that can be used to customize the serialization |
| | 55 | | /// of strongly typed models. |
| | 56 | | /// </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 SuggestResults.</returns> |
| | 63 | | internal static async Task<SuggestResults<T>> DeserializeAsync( |
| | 64 | | Stream json, |
| | 65 | | #if EXPERIMENTAL_SERIALIZER |
| | 66 | | ObjectSerializer serializer, |
| | 67 | | #endif |
| | 68 | | bool async, |
| | 69 | | CancellationToken cancellationToken) |
| | 70 | | #pragma warning restore CS1572 |
| | 71 | | { |
| | 72 | | // Parse the JSON |
| 22 | 73 | | using JsonDocument doc = async ? |
| 22 | 74 | | await JsonDocument.ParseAsync(json, cancellationToken: cancellationToken).ConfigureAwait(false) : |
| 22 | 75 | | JsonDocument.Parse(json); |
| | 76 | |
|
| 22 | 77 | | JsonSerializerOptions defaultSerializerOptions = JsonSerialization.SerializerOptions; |
| | 78 | |
|
| 22 | 79 | | SuggestResults<T> suggestions = new SuggestResults<T>(); |
| 92 | 80 | | foreach (JsonProperty prop in doc.RootElement.EnumerateObject()) |
| | 81 | | { |
| 24 | 82 | | if (prop.NameEquals(Constants.SearchCoverageKeyJson.EncodedUtf8Bytes) && |
| 24 | 83 | | prop.Value.ValueKind != JsonValueKind.Null) |
| | 84 | | { |
| 2 | 85 | | suggestions.Coverage = prop.Value.GetDouble(); |
| | 86 | | } |
| 22 | 87 | | else if (prop.NameEquals(Constants.ValueKeyJson.EncodedUtf8Bytes)) |
| | 88 | | { |
| 22 | 89 | | List<SearchSuggestion<T>> results = new List<SearchSuggestion<T>>(); |
| 132 | 90 | | foreach (JsonElement element in prop.Value.EnumerateArray()) |
| | 91 | | { |
| 44 | 92 | | SearchSuggestion<T> suggestion = await SearchSuggestion<T>.DeserializeAsync( |
| 44 | 93 | | element, |
| 44 | 94 | | #if EXPERIMENTAL_SERIALIZER |
| 44 | 95 | | serializer, |
| 44 | 96 | | #endif |
| 44 | 97 | | defaultSerializerOptions, |
| 44 | 98 | | async, |
| 44 | 99 | | cancellationToken) |
| 44 | 100 | | .ConfigureAwait(false); |
| 44 | 101 | | results.Add(suggestion); |
| | 102 | | } |
| 22 | 103 | | suggestions.Results = new ReadOnlyCollection<SearchSuggestion<T>>(results); |
| 22 | 104 | | } |
| | 105 | | } |
| 22 | 106 | | return suggestions; |
| 22 | 107 | | } |
| | 108 | | } |
| | 109 | |
|
| | 110 | | public static partial class SearchModelFactory |
| | 111 | | { |
| | 112 | | /// <summary> Initializes a new instance of SearchResult. </summary> |
| | 113 | | /// <typeparam name="T"> |
| | 114 | | /// The .NET type that maps to the index schema. Instances of this type |
| | 115 | | /// can be retrieved as documents from the index. |
| | 116 | | /// </typeparam> |
| | 117 | | /// <param name="results"> |
| | 118 | | /// The sequence of suggestions returned by the query. |
| | 119 | | /// </param> |
| | 120 | | /// <param name="coverage"> |
| | 121 | | /// A value indicating the percentage of the index that was included in |
| | 122 | | /// the query, or null if minimumCoverage was not set in the request. |
| | 123 | | /// </param> |
| | 124 | | /// <returns>A new SuggestResults instance for mocking.</returns> |
| | 125 | | public static SuggestResults<T> SuggestResults<T>( |
| | 126 | | IReadOnlyList<SearchSuggestion<T>> results, |
| | 127 | | double? coverage) => |
| | 128 | | new SuggestResults<T>() { Coverage = coverage, Results = results }; |
| | 129 | | } |
| | 130 | | } |