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