| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Diagnostics; |
| | 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 SearchResult |
| | 20 | | [CodeGenModel("SearchResult")] |
| | 21 | | internal partial class SearchResult { } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Contains a document found by a search query, plus 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 SearchResult<T> |
| | 31 | | { |
| | 32 | | /// <summary> |
| | 33 | | /// The relevance score of the document compared to other documents |
| | 34 | | /// returned by the query. |
| | 35 | | /// </summary> |
| 16956 | 36 | | public double? Score { get; internal set; } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Text fragments from the document that indicate the matching search |
| | 40 | | /// terms, organized by each applicable field; null if hit highlighting |
| | 41 | | /// was not enabled for the query. |
| | 42 | | /// </summary> |
| 78 | 43 | | public IDictionary<string, IList<string>> Highlights { get; internal set; } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// The document found by the search query. |
| | 47 | | /// </summary> |
| 33748 | 48 | | public T Document { get; internal set; } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Initializes a new instance of the SearchResult class. |
| | 52 | | /// </summary> |
| 33784 | 53 | | internal SearchResult() { } |
| | 54 | |
|
| | 55 | | #pragma warning disable CS1572 // Not all parameters will be used depending on feature flags |
| | 56 | | /// <summary> |
| | 57 | | /// Deserialize a SearchResult and its model. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="element">A JSON element.</param> |
| | 60 | | /// <param name="serializer"> |
| | 61 | | /// Optional serializer that can be used to customize the serialization |
| | 62 | | /// of strongly typed models. |
| | 63 | | /// </param> |
| | 64 | | /// <param name="options">JSON serializer options.</param> |
| | 65 | | /// <param name="async">Whether to execute sync or async.</param> |
| | 66 | | /// <param name="cancellationToken"> |
| | 67 | | /// Optional <see cref="CancellationToken"/> to propagate notifications |
| | 68 | | /// that the operation should be canceled. |
| | 69 | | /// </param> |
| | 70 | | /// <returns>Deserialized SearchResults.</returns> |
| | 71 | | internal static async Task<SearchResult<T>> DeserializeAsync( |
| | 72 | | JsonElement element, |
| | 73 | | #if EXPERIMENTAL_SERIALIZER |
| | 74 | | ObjectSerializer serializer, |
| | 75 | | #endif |
| | 76 | | JsonSerializerOptions options, |
| | 77 | | bool async, |
| | 78 | | CancellationToken cancellationToken) |
| | 79 | | #pragma warning restore CS1572 |
| | 80 | | { |
| | 81 | | Debug.Assert(options != null); |
| 16892 | 82 | | SearchResult<T> result = new SearchResult<T>(); |
| 106502 | 83 | | foreach (JsonProperty prop in element.EnumerateObject()) |
| | 84 | | { |
| 36359 | 85 | | if (prop.NameEquals(Constants.SearchScoreKeyJson.EncodedUtf8Bytes) && |
| 36359 | 86 | | prop.Value.ValueKind != JsonValueKind.Null) |
| | 87 | | { |
| 16892 | 88 | | result.Score = prop.Value.GetDouble(); |
| | 89 | | } |
| 19467 | 90 | | else if (prop.NameEquals(Constants.SearchHighlightsKeyJson.EncodedUtf8Bytes)) |
| | 91 | | { |
| 2 | 92 | | result.Highlights = new Dictionary<string, IList<string>>(); |
| 12 | 93 | | foreach (JsonProperty highlight in prop.Value.EnumerateObject()) |
| | 94 | | { |
| | 95 | | // Add the highlight values |
| 4 | 96 | | List<string> values = new List<string>(); |
| 4 | 97 | | result.Highlights[highlight.Name] = values; |
| 20 | 98 | | foreach (JsonElement highlightValue in highlight.Value.EnumerateArray()) |
| | 99 | | { |
| 6 | 100 | | values.Add(highlightValue.GetString()); |
| | 101 | | } |
| | 102 | | } |
| | 103 | | } |
| | 104 | | } |
| | 105 | |
|
| | 106 | | // Deserialize the model |
| | 107 | | #if EXPERIMENTAL_SERIALIZER |
| 16892 | 108 | | if (serializer != null) |
| | 109 | | { |
| 20 | 110 | | using Stream stream = element.ToStream(); |
| 20 | 111 | | T document = async ? |
| 20 | 112 | | (T)await serializer.DeserializeAsync(stream, typeof(T), cancellationToken).ConfigureAwait(false) : |
| 20 | 113 | | (T)serializer.Deserialize(stream, typeof(T), cancellationToken); |
| 20 | 114 | | result.Document = document; |
| 20 | 115 | | } |
| | 116 | | else |
| | 117 | | { |
| | 118 | | #endif |
| | 119 | | T document; |
| 16872 | 120 | | if (async) |
| | 121 | | { |
| 10553 | 122 | | using Stream stream = element.ToStream(); |
| 10553 | 123 | | document = await JsonSerializer.DeserializeAsync<T>(stream, options, cancellationToken).ConfigureAwa |
| 10553 | 124 | | } |
| | 125 | | else |
| | 126 | | { |
| 6319 | 127 | | document = JsonSerializer.Deserialize<T>(element.GetRawText(), options); |
| | 128 | | } |
| 16872 | 129 | | result.Document = document; |
| | 130 | | #if EXPERIMENTAL_SERIALIZER |
| | 131 | | } |
| | 132 | | #endif |
| | 133 | |
|
| 16892 | 134 | | return result; |
| 16892 | 135 | | } |
| | 136 | | } |
| | 137 | |
|
| | 138 | | public static partial class SearchModelFactory |
| | 139 | | { |
| | 140 | | /// <summary> Initializes a new instance of SearchResult. </summary> |
| | 141 | | /// <typeparam name="T"> |
| | 142 | | /// The .NET type that maps to the index schema. Instances of this type can |
| | 143 | | /// be retrieved as documents from the index. |
| | 144 | | /// </typeparam> |
| | 145 | | /// <param name="document">The document found by the search query.</param> |
| | 146 | | /// <param name="score"> |
| | 147 | | /// The relevance score of the document compared to other documents |
| | 148 | | /// returned by the query. |
| | 149 | | /// </param> |
| | 150 | | /// <param name="highlights"> |
| | 151 | | /// Text fragments from the document that indicate the matching search |
| | 152 | | /// terms, organized by each applicable field; null if hit highlighting |
| | 153 | | /// was not enabled for the query. |
| | 154 | | /// </param> |
| | 155 | | /// <returns>A new SearchResult instance for mocking.</returns> |
| | 156 | | public static SearchResult<T> SearchResult<T>( |
| | 157 | | T document, |
| | 158 | | double? score, |
| | 159 | | IDictionary<string, IList<string>> highlights) => |
| | 160 | | new SearchResult<T>() |
| | 161 | | { |
| | 162 | | Score = score, |
| | 163 | | Highlights = highlights, |
| | 164 | | Document = document }; |
| | 165 | | } |
| | 166 | | } |