| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 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 IndexAction |
| | 20 | | [CodeGenModel("IndexAction")] |
| | 21 | | internal partial class IndexAction { } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Represents an index action that operates on a document. |
| | 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 IndexDocumentsAction<T> |
| | 31 | | { |
| | 32 | | /// <summary> |
| | 33 | | /// The operation to perform on a document in an indexing batch. |
| | 34 | | /// </summary> |
| | 35 | | public IndexActionType ActionType { get; private set; } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// The document to index. |
| | 39 | | /// </summary> |
| | 40 | | public T Document { get; private set; } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Private constructor to prevent direct instantiation. |
| | 44 | | /// </summary> |
| | 45 | | private IndexDocumentsAction() { } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Initializes a new instance of the IndexDocumentsAction class. |
| | 49 | | /// </summary> |
| | 50 | | /// <param name="type"> |
| | 51 | | /// The operation to perform on the document. |
| | 52 | | /// </param> |
| | 53 | | /// <param name="doc">The document to index.</param> |
| | 54 | | public IndexDocumentsAction(IndexActionType type, T doc) |
| | 55 | | { |
| | 56 | | Debug.Assert(Enum.IsDefined(typeof(IndexActionType), type)); |
| | 57 | |
|
| | 58 | | ActionType = type; |
| | 59 | | Document = doc; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | #pragma warning disable CS1572 // Not all parameters will be used depending on feature flags |
| | 63 | | #pragma warning disable CA1801 // Not all parameters are used depending on feature flags |
| | 64 | | #pragma warning disable CS1998 // Won't await depending on feature flags |
| | 65 | | /// <summary> |
| | 66 | | /// Serialize the document write action. |
| | 67 | | /// </summary> |
| | 68 | | /// <param name="writer">The JSON writer.</param> |
| | 69 | | /// <param name="serializer"> |
| | 70 | | /// Optional serializer that can be used to customize the serialization |
| | 71 | | /// of strongly typed models. |
| | 72 | | /// </param> |
| | 73 | | /// <param name="options">JSON serializer options.</param> |
| | 74 | | /// <param name="async">Whether to execute sync or async.</param> |
| | 75 | | /// <param name="cancellationToken"> |
| | 76 | | /// Optional <see cref="CancellationToken"/> to propagate notifications |
| | 77 | | /// that the operation should be canceled. |
| | 78 | | /// </param> |
| | 79 | | /// <returns>A task representing the serialization.</returns> |
| | 80 | | internal async Task SerializeAsync( |
| | 81 | | Utf8JsonWriter writer, |
| | 82 | | #if EXPERIMENTAL_SERIALIZER |
| | 83 | | ObjectSerializer serializer, |
| | 84 | | #endif |
| | 85 | | JsonSerializerOptions options, |
| | 86 | | bool async, |
| | 87 | | CancellationToken cancellationToken) |
| | 88 | | #pragma warning restore CS1998 |
| | 89 | | #pragma warning restore CA1801 |
| | 90 | | #pragma warning restore CS1572 |
| | 91 | | { |
| | 92 | | Debug.Assert(writer != null); |
| | 93 | |
|
| | 94 | | writer.WriteStartObject(); |
| | 95 | | writer.WriteString(Constants.SearchActionKeyJson, ActionType.ToSerialString()); |
| | 96 | |
|
| | 97 | | // HACK: Serialize the user's model, parse it, and then write each |
| | 98 | | // of its properties as if they were our own. |
| | 99 | | byte[] json; |
| | 100 | | #if EXPERIMENTAL_SERIALIZER |
| | 101 | | if (serializer != null) |
| | 102 | | { |
| | 103 | | using MemoryStream stream = new MemoryStream(); |
| | 104 | | if (async) |
| | 105 | | { |
| | 106 | | await serializer.SerializeAsync(stream, Document, typeof(T), cancellationToken).ConfigureAwait(false |
| | 107 | | } |
| | 108 | | else |
| | 109 | | { |
| | 110 | | serializer.Serialize(stream, Document, typeof(T), cancellationToken); |
| | 111 | | } |
| | 112 | | json = stream.ToArray(); |
| | 113 | | } |
| | 114 | | else |
| | 115 | | { |
| | 116 | | #endif |
| | 117 | | json = JsonSerializer.SerializeToUtf8Bytes<T>(Document, options); |
| | 118 | | #if EXPERIMENTAL_SERIALIZER |
| | 119 | | } |
| | 120 | | #endif |
| | 121 | | using JsonDocument nested = JsonDocument.Parse(json); |
| | 122 | | foreach (JsonProperty property in nested.RootElement.EnumerateObject()) |
| | 123 | | { |
| | 124 | | property.WriteTo(writer); |
| | 125 | | } |
| | 126 | |
|
| | 127 | | writer.WriteEndObject(); |
| | 128 | | } |
| | 129 | | } |
| | 130 | |
|
| | 131 | | /// <summary> |
| | 132 | | /// Creates <see cref="IndexDocumentsAction{T}"/> instances to add to an |
| | 133 | | /// <see cref="IndexDocumentsBatch{T}"/> and submit via |
| | 134 | | /// <see cref="SearchClient.IndexDocuments"/>. |
| | 135 | | /// </summary> |
| | 136 | | public static class IndexDocumentsAction |
| | 137 | | { |
| | 138 | | /// <summary> |
| | 139 | | /// Create an <see cref="IndexDocumentsAction{T}"/> to upload. |
| | 140 | | /// </summary> |
| | 141 | | /// <typeparam name="T"> |
| | 142 | | /// The .NET type that maps to the index schema. Instances of this type |
| | 143 | | /// can be retrieved as documents from the index. |
| | 144 | | /// </typeparam> |
| | 145 | | /// <param name="document">The document to upload.</param> |
| | 146 | | /// <returns> |
| | 147 | | /// An <see cref="IndexDocumentsAction{T}"/> to upload. |
| | 148 | | /// </returns> |
| | 149 | | public static IndexDocumentsAction<T> Upload<T>(T document) => |
| 21 | 150 | | new IndexDocumentsAction<T>(IndexActionType.Upload, document); |
| | 151 | |
|
| | 152 | | /// <summary> |
| | 153 | | /// Create an <see cref="IndexDocumentsAction{T}"/> to merge. |
| | 154 | | /// </summary> |
| | 155 | | /// <typeparam name="T"> |
| | 156 | | /// The .NET type that maps to the index schema. Instances of this type |
| | 157 | | /// can be retrieved as documents from the index. |
| | 158 | | /// </typeparam> |
| | 159 | | /// <param name="document">The document to merge.</param> |
| | 160 | | /// <returns> |
| | 161 | | /// An <see cref="IndexDocumentsAction{T}"/> to merge. |
| | 162 | | /// </returns> |
| | 163 | | public static IndexDocumentsAction<T> Merge<T>(T document) => |
| 15 | 164 | | new IndexDocumentsAction<T>(IndexActionType.Merge, document); |
| | 165 | |
|
| | 166 | | /// <summary> |
| | 167 | | /// Create an <see cref="IndexDocumentsAction{T}"/> to merge or upload. |
| | 168 | | /// </summary> |
| | 169 | | /// <typeparam name="T"> |
| | 170 | | /// The .NET type that maps to the index schema. Instances of this type |
| | 171 | | /// can be retrieved as documents from the index. |
| | 172 | | /// </typeparam> |
| | 173 | | /// <param name="document">The document to merge or upload.</param> |
| | 174 | | /// <returns> |
| | 175 | | /// An <see cref="IndexDocumentsAction{T}"/> to merge or upload. |
| | 176 | | /// </returns> |
| | 177 | | public static IndexDocumentsAction<T> MergeOrUpload<T>(T document) => |
| 6 | 178 | | new IndexDocumentsAction<T>(IndexActionType.MergeOrUpload, document); |
| | 179 | |
|
| | 180 | | /// <summary> |
| | 181 | | /// Create an <see cref="IndexDocumentsAction{T}"/> to delete. |
| | 182 | | /// </summary> |
| | 183 | | /// <typeparam name="T"> |
| | 184 | | /// The .NET type that maps to the index schema. Instances of this type |
| | 185 | | /// can be retrieved as documents from the index. |
| | 186 | | /// </typeparam> |
| | 187 | | /// <param name="document">The document to delete.</param> |
| | 188 | | /// <returns> |
| | 189 | | /// An <see cref="IndexDocumentsAction{T}"/> to delete. |
| | 190 | | /// </returns> |
| | 191 | | public static IndexDocumentsAction<T> Delete<T>(T document) => |
| 4 | 192 | | new IndexDocumentsAction<T>(IndexActionType.Delete, document); |
| | 193 | |
|
| | 194 | | /// <summary> |
| | 195 | | /// Create an <see cref="IndexDocumentsAction{SearchDocument}"/> to |
| | 196 | | /// delete. |
| | 197 | | /// </summary> |
| | 198 | | /// <param name="keyName"> |
| | 199 | | /// The name of the key field of the index. |
| | 200 | | /// </param> |
| | 201 | | /// <param name="keyValue">The key of the document to delete.</param> |
| | 202 | | /// <returns> |
| | 203 | | /// An <see cref="IndexDocumentsAction{SearchDocument}"/> to delete. |
| | 204 | | /// </returns> |
| | 205 | | public static IndexDocumentsAction<SearchDocument> Delete(string keyName, string keyValue) |
| | 206 | | { |
| 8 | 207 | | Argument.AssertNotNullOrEmpty(keyName, nameof(keyName)); |
| 8 | 208 | | Argument.AssertNotNull(keyValue, nameof(keyValue)); |
| | 209 | |
|
| 8 | 210 | | return new IndexDocumentsAction<SearchDocument>( |
| 8 | 211 | | IndexActionType.Delete, |
| 8 | 212 | | new SearchDocument { [keyName] = keyValue }); |
| | 213 | | } |
| | 214 | | } |
| | 215 | | } |