| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Text.Json; |
| | | 9 | | using System.Text.Json.Serialization; |
| | | 10 | | using Azure.Core; |
| | | 11 | | #if EXPERIMENTAL_SPATIAL |
| | | 12 | | using Azure.Core.Spatial; |
| | | 13 | | #endif |
| | | 14 | | |
| | | 15 | | namespace Azure.Search.Documents.Models |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Represents an untyped document returned from a search or document |
| | | 19 | | /// lookup. It can be accessed as either a dynamic object or a dictionary. |
| | | 20 | | /// </summary> |
| | | 21 | | [JsonConverter(typeof(SearchDocumentConverter))] |
| | | 22 | | public partial class SearchDocument |
| | | 23 | | #if EXPERIMENTAL_DYNAMIC |
| | | 24 | | : DynamicData |
| | | 25 | | #endif |
| | | 26 | | { |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the SearchDocument class. |
| | | 29 | | /// </summary> |
| | 51554 | 30 | | public SearchDocument() : this(null) { } |
| | | 31 | | |
| | | 32 | | #if EXPERIMENTAL_DYNAMIC |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the SearchDocument class with initial |
| | | 35 | | /// values. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="values">Initial values of the document.</param> |
| | | 38 | | public SearchDocument(IDictionary<string, object> values) : base(values) { } |
| | | 39 | | #else |
| | | 40 | | /// <summary> |
| | | 41 | | /// Initializes a new instance of the SearchDocument class with initial |
| | | 42 | | /// values. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="values">Initial values of the document.</param> |
| | 25777 | 45 | | public SearchDocument(IDictionary<string, object> values) => |
| | 25777 | 46 | | _values = values != null ? |
| | 25777 | 47 | | new Dictionary<string, object>(values) : |
| | 25777 | 48 | | new Dictionary<string, object>(); |
| | | 49 | | #endif |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 53 | | /// <see cref="Nullable{Boolean}"/> property called |
| | | 54 | | /// <paramref name="key"/>. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="key">The name of the property.</param> |
| | | 57 | | /// <returns>The value of the property.</returns> |
| | 26 | 58 | | public bool? GetBoolean(string key) => GetValue<bool?>(key); |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 62 | | /// <see cref="Boolean"/> collection property called |
| | | 63 | | /// <paramref name="key"/>. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="key">The name of the property.</param> |
| | | 66 | | /// <returns>The value of the property.</returns> |
| | 13 | 67 | | public IReadOnlyList<bool> GetBooleanCollection(string key) => GetValue<bool[]>(key); |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 71 | | /// <see cref="Nullable{Int32}"/> property called |
| | | 72 | | /// <paramref name="key"/>. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="key">The name of the property.</param> |
| | | 75 | | /// <returns>The value of the property.</returns> |
| | 38 | 76 | | public int? GetInt32(string key) => GetValue<int?>(key); |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 80 | | /// <see cref="Int32"/> collection property called |
| | | 81 | | /// <paramref name="key"/>. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="key">The name of the property.</param> |
| | | 84 | | /// <returns>The value of the property.</returns> |
| | 16 | 85 | | public IReadOnlyList<int> GetInt32Collection(string key) => GetValue<int[]>(key); |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 89 | | /// <see cref="Nullable{Int64}"/> property called |
| | | 90 | | /// <paramref name="key"/>. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="key">The name of the property.</param> |
| | | 93 | | /// <returns>The value of the property.</returns> |
| | 46 | 94 | | public long? GetInt64(string key) => GetValue<long?>(key); |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 98 | | /// <see cref="Int64"/> collection property called |
| | | 99 | | /// <paramref name="key"/>. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="key">The name of the property.</param> |
| | | 102 | | /// <returns>The value of the property.</returns> |
| | 17 | 103 | | public IReadOnlyList<long> GetInt64Collection(string key) => GetValue<long[]>(key); |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 107 | | /// <see cref="Nullable{Double}"/> property called |
| | | 108 | | /// <paramref name="key"/>. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="key">The name of the property.</param> |
| | | 111 | | /// <returns>The value of the property.</returns> |
| | 64 | 112 | | public double? GetDouble(string key) => GetValue<double?>(key); |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 116 | | /// <see cref="Double"/> collection property called |
| | | 117 | | /// <paramref name="key"/>. |
| | | 118 | | /// </summary> |
| | | 119 | | /// <param name="key">The name of the property.</param> |
| | | 120 | | /// <returns>The value of the property.</returns> |
| | 13 | 121 | | public IReadOnlyList<double> GetDoubleCollection(string key) => GetValue<double[]>(key); |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 125 | | /// <see cref="Nullable{DateTimeOffset}"/> property called |
| | | 126 | | /// <paramref name="key"/>. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="key">The name of the property.</param> |
| | | 129 | | /// <returns>The value of the property.</returns> |
| | 42 | 130 | | public DateTimeOffset? GetDateTimeOffset(string key) => GetValue<DateTimeOffset?>(key); |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 134 | | /// <see cref="DateTimeOffset"/> collection property called |
| | | 135 | | /// <paramref name="key"/>. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <param name="key">The name of the property.</param> |
| | | 138 | | /// <returns>The value of the property.</returns> |
| | 13 | 139 | | public IReadOnlyList<DateTimeOffset> GetDateTimeOffsetCollection(string key) => GetValue<DateTimeOffset[]>(key); |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 143 | | /// <see cref="String"/> property called |
| | | 144 | | /// <paramref name="key"/>. |
| | | 145 | | /// </summary> |
| | | 146 | | /// <param name="key">The name of the property.</param> |
| | | 147 | | /// <returns>The value of the property.</returns> |
| | 17 | 148 | | public string GetString(string key) => GetValue<string>(key); |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 152 | | /// <see cref="String"/> collection property called |
| | | 153 | | /// <paramref name="key"/>. |
| | | 154 | | /// </summary> |
| | | 155 | | /// <param name="key">The name of the property.</param> |
| | | 156 | | /// <returns>The value of the property.</returns> |
| | 13 | 157 | | public IReadOnlyList<string> GetStringCollection(string key) => GetValue<string[]>(key); |
| | | 158 | | |
| | | 159 | | #if EXPERIMENTAL_SPATIAL |
| | | 160 | | /// <summary> |
| | | 161 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 162 | | /// <see cref="PointGeometry"/> property called |
| | | 163 | | /// <paramref name="key"/>. |
| | | 164 | | /// </summary> |
| | | 165 | | /// <param name="key">The name of the property.</param> |
| | | 166 | | /// <returns>The value of the property.</returns> |
| | | 167 | | public PointGeometry GetPoint(string key) => GetValue<PointGeometry>(key); |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 171 | | /// <see cref="PointGeometry"/> collection property called |
| | | 172 | | /// <paramref name="key"/>. |
| | | 173 | | /// </summary> |
| | | 174 | | /// <param name="key">The name of the property.</param> |
| | | 175 | | /// <returns>The value of the property.</returns> |
| | | 176 | | public IReadOnlyList<PointGeometry> GetPointCollection(string key) => GetValue<PointGeometry[]>(key); |
| | | 177 | | #endif |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 181 | | /// complex <see cref="SearchDocument"/> property called |
| | | 182 | | /// <paramref name="key"/>. |
| | | 183 | | /// </summary> |
| | | 184 | | /// <param name="key">The name of the property.</param> |
| | | 185 | | /// <returns>The value of the property.</returns> |
| | 18 | 186 | | public SearchDocument GetObject(string key) => GetValue<SearchDocument>(key); |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Get the value of a <see cref="SearchDocument"/>'s |
| | | 190 | | /// complex <see cref="SearchDocument"/> collection property called |
| | | 191 | | /// <paramref name="key"/>. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <param name="key">The name of the property.</param> |
| | | 194 | | /// <returns>The value of the property.</returns> |
| | 15 | 195 | | public IReadOnlyList<SearchDocument> GetObjectCollection(string key) => GetValue<SearchDocument[]>(key); |
| | | 196 | | |
| | | 197 | | /// <inheritdoc /> |
| | | 198 | | public override string ToString() |
| | | 199 | | { |
| | | 200 | | // Write the document as JSON. This is expensive, but helpful. |
| | 0 | 201 | | using var stream = new MemoryStream(); |
| | 0 | 202 | | using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true }); |
| | 0 | 203 | | JsonSerialization.WriteSearchDocument(writer, this, JsonSerialization.SerializerOptions); |
| | 0 | 204 | | writer.Flush(); |
| | 0 | 205 | | return Encoding.UTF8.GetString(stream.ToArray()); |
| | 0 | 206 | | } |
| | | 207 | | } |
| | | 208 | | } |