/
SearchDocument.cs
203 lines (183 loc) · 8.54 KB
/
SearchDocument.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.Core.GeoJson;
namespace Azure.Search.Documents.Models
{
/// <summary>
/// Represents an untyped document returned from a search or document
/// lookup. It can be accessed as either a dynamic object or a dictionary.
/// </summary>
[JsonConverter(typeof(SearchDocumentConverter))]
public partial class SearchDocument
#if EXPERIMENTAL_DYNAMIC
: DynamicData
#endif
{
/// <summary>
/// Initializes a new instance of the SearchDocument class.
/// </summary>
public SearchDocument() : this(null) { }
#if EXPERIMENTAL_DYNAMIC
/// <summary>
/// Initializes a new instance of the SearchDocument class with initial
/// values.
/// </summary>
/// <param name="values">Initial values of the document.</param>
public SearchDocument(IDictionary<string, object> values) : base(values) { }
#else
/// <summary>
/// Initializes a new instance of the SearchDocument class with initial
/// values.
/// </summary>
/// <param name="values">Initial values of the document.</param>
public SearchDocument(IDictionary<string, object> values) =>
_values = values != null ?
new Dictionary<string, object>(values) :
new Dictionary<string, object>();
#endif
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="Nullable{Boolean}"/> property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public bool? GetBoolean(string key) => GetValue<bool?>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="Boolean"/> collection property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public IReadOnlyList<bool> GetBooleanCollection(string key) => GetValue<bool[]>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="Nullable{Int32}"/> property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public int? GetInt32(string key) => GetValue<int?>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="Int32"/> collection property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public IReadOnlyList<int> GetInt32Collection(string key) => GetValue<int[]>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="Nullable{Int64}"/> property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public long? GetInt64(string key) => GetValue<long?>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="Int64"/> collection property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public IReadOnlyList<long> GetInt64Collection(string key) => GetValue<long[]>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="Nullable{Double}"/> property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public double? GetDouble(string key) => GetValue<double?>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="Double"/> collection property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public IReadOnlyList<double> GetDoubleCollection(string key) => GetValue<double[]>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="Nullable{DateTimeOffset}"/> property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public DateTimeOffset? GetDateTimeOffset(string key) => GetValue<DateTimeOffset?>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="DateTimeOffset"/> collection property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public IReadOnlyList<DateTimeOffset> GetDateTimeOffsetCollection(string key) => GetValue<DateTimeOffset[]>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="String"/> property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public string GetString(string key) => GetValue<string>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="String"/> collection property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public IReadOnlyList<string> GetStringCollection(string key) => GetValue<string[]>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="GeoPoint"/> property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public GeoPoint GetPoint(string key) => GetValue<GeoPoint>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// <see cref="GeoPoint"/> collection property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public IReadOnlyList<GeoPoint> GetPointCollection(string key) => GetValue<GeoPoint[]>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// complex <see cref="SearchDocument"/> property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public SearchDocument GetObject(string key) => GetValue<SearchDocument>(key);
/// <summary>
/// Get the value of a <see cref="SearchDocument"/>'s
/// complex <see cref="SearchDocument"/> collection property called
/// <paramref name="key"/>.
/// </summary>
/// <param name="key">The name of the property.</param>
/// <returns>The value of the property.</returns>
public IReadOnlyList<SearchDocument> GetObjectCollection(string key) => GetValue<SearchDocument[]>(key);
/// <inheritdoc />
public override string ToString()
{
// Write the document as JSON. This is expensive, but helpful.
using var stream = new MemoryStream();
using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true });
JsonSerialization.WriteSearchDocument(writer, this, JsonSerialization.SerializerOptions);
writer.Flush();
return Encoding.UTF8.GetString(stream.ToArray());
}
}
}