< Summary

Class:Azure.Search.Documents.Models.SearchResult`1
Assembly:Azure.Search.Documents
File(s):C:\Git\azure-sdk-for-net\sdk\search\Azure.Search.Documents\src\Models\SearchResult{T}.cs
Covered lines:31
Uncovered lines:0
Coverable lines:31
Total lines:166
Line coverage:100% (31 of 31)
Covered branches:18
Total branches:18
Branch coverage:100% (18 of 18)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_Score()-100%100%
get_Highlights()-100%100%
get_Document()-100%100%
.ctor()-100%100%
DeserializeAsync()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Azure.Search.Documents\src\Models\SearchResult{T}.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Collections.Generic;
 5using System.Diagnostics;
 6using System.IO;
 7using System.Text.Json;
 8using System.Threading;
 9using System.Threading.Tasks;
 10using Azure.Core;
 11#if EXPERIMENTAL_SERIALIZER
 12using Azure.Core.Serialization;
 13#endif
 14
 15#pragma warning disable SA1402 // File may only contain a single type
 16
 17namespace 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>
 1695636        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>
 7843        public IDictionary<string, IList<string>> Highlights { get; internal set; }
 44
 45        /// <summary>
 46        /// The document found by the search query.
 47        /// </summary>
 3374848        public T Document { get; internal set; }
 49
 50        /// <summary>
 51        /// Initializes a new instance of the SearchResult class.
 52        /// </summary>
 3378453        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);
 1689282            SearchResult<T> result = new SearchResult<T>();
 10650283            foreach (JsonProperty prop in element.EnumerateObject())
 84            {
 3635985                if (prop.NameEquals(Constants.SearchScoreKeyJson.EncodedUtf8Bytes) &&
 3635986                    prop.Value.ValueKind != JsonValueKind.Null)
 87                {
 1689288                    result.Score = prop.Value.GetDouble();
 89                }
 1946790                else if (prop.NameEquals(Constants.SearchHighlightsKeyJson.EncodedUtf8Bytes))
 91                {
 292                    result.Highlights = new Dictionary<string, IList<string>>();
 1293                    foreach (JsonProperty highlight in prop.Value.EnumerateObject())
 94                    {
 95                        // Add the highlight values
 496                        List<string> values = new List<string>();
 497                        result.Highlights[highlight.Name] = values;
 2098                        foreach (JsonElement highlightValue in highlight.Value.EnumerateArray())
 99                        {
 6100                            values.Add(highlightValue.GetString());
 101                        }
 102                    }
 103                }
 104            }
 105
 106            // Deserialize the model
 107#if EXPERIMENTAL_SERIALIZER
 16892108            if (serializer != null)
 109            {
 20110                using Stream stream = element.ToStream();
 20111                T document = async ?
 20112                    (T)await serializer.DeserializeAsync(stream, typeof(T), cancellationToken).ConfigureAwait(false) :
 20113                    (T)serializer.Deserialize(stream, typeof(T), cancellationToken);
 20114                result.Document = document;
 20115            }
 116            else
 117            {
 118#endif
 119                T document;
 16872120                if (async)
 121                {
 10553122                    using Stream stream = element.ToStream();
 10553123                    document = await JsonSerializer.DeserializeAsync<T>(stream, options, cancellationToken).ConfigureAwa
 10553124                }
 125                else
 126                {
 6319127                    document = JsonSerializer.Deserialize<T>(element.GetRawText(), options);
 128                }
 16872129                result.Document = document;
 130#if EXPERIMENTAL_SERIALIZER
 131            }
 132#endif
 133
 16892134            return result;
 16892135        }
 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}