| | 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.Diagnostics; |
| | 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 IndexBatch |
| | 20 | | [CodeGenModel("IndexBatch")] |
| | 21 | | internal partial class IndexBatch { } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Contains a batch of document write actions to send to a search index |
| | 25 | | /// via <see cref="SearchClient.IndexDocuments"/>. |
| | 26 | | /// </summary> |
| | 27 | | public partial class IndexDocumentsBatch<T> |
| | 28 | | { |
| | 29 | | /// <summary> |
| | 30 | | /// The actions in the batch. |
| | 31 | | /// </summary> |
| 16877 | 32 | | public IList<IndexDocumentsAction<T>> Actions { get; } = new List<IndexDocumentsAction<T>>(); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Initializes a new instance of the IndexDocumentsBatch class. |
| | 36 | | /// </summary> |
| 38 | 37 | | public IndexDocumentsBatch() { } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Initializes a new instance of the IndexDocumentsBatch class. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="type"> |
| | 43 | | /// The operation to perform on a document in an indexing batch. |
| | 44 | | /// </param> |
| | 45 | | /// <param name="documents"> |
| | 46 | | /// The collection of documents to index. |
| | 47 | | /// </param> |
| 77 | 48 | | internal IndexDocumentsBatch(IndexActionType type, IEnumerable<T> documents) |
| | 49 | | { |
| | 50 | | Debug.Assert(Enum.IsDefined(typeof(IndexActionType), type)); |
| | 51 | | Debug.Assert(documents != null); |
| | 52 | |
|
| 77 | 53 | | if (documents != null) |
| | 54 | | { |
| 33416 | 55 | | foreach (T doc in documents) |
| | 56 | | { |
| 16631 | 57 | | Actions.Add(new IndexDocumentsAction<T>(type, doc)); |
| | 58 | | } |
| | 59 | | } |
| 77 | 60 | | } |
| | 61 | |
|
| | 62 | | #pragma warning disable CS1572 // Not all parameters will be used depending on feature flags |
| | 63 | | /// <summary> |
| | 64 | | /// Serialize the document batch. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="writer">The JSON writer.</param> |
| | 67 | | /// <param name="serializer"> |
| | 68 | | /// Optional serializer that can be used to customize the serialization |
| | 69 | | /// of strongly typed models. |
| | 70 | | /// </param> |
| | 71 | | /// <param name="options">JSON serializer options.</param> |
| | 72 | | /// <param name="async">Whether to execute sync or async.</param> |
| | 73 | | /// <param name="cancellationToken"> |
| | 74 | | /// Optional <see cref="CancellationToken"/> to propagate notifications |
| | 75 | | /// that the operation should be canceled. |
| | 76 | | /// </param> |
| | 77 | | /// <returns>A task representing the serialization.</returns> |
| | 78 | | internal async Task SerializeAsync( |
| | 79 | | Utf8JsonWriter writer, |
| | 80 | | #if EXPERIMENTAL_SERIALIZER |
| | 81 | | ObjectSerializer serializer, |
| | 82 | | #endif |
| | 83 | | JsonSerializerOptions options, |
| | 84 | | bool async, |
| | 85 | | CancellationToken cancellationToken) |
| | 86 | | #pragma warning restore CS1572 |
| | 87 | | { |
| | 88 | | Debug.Assert(writer != null); |
| 96 | 89 | | writer.WriteStartObject(); |
| 96 | 90 | | writer.WritePropertyName(Constants.ValueKeyJson); |
| 96 | 91 | | writer.WriteStartArray(); |
| 33562 | 92 | | foreach (IndexDocumentsAction<T> action in Actions) |
| | 93 | | { |
| 16685 | 94 | | await action.SerializeAsync( |
| 16685 | 95 | | writer, |
| 16685 | 96 | | #if EXPERIMENTAL_SERIALIZER |
| 16685 | 97 | | serializer, |
| 16685 | 98 | | #endif |
| 16685 | 99 | | options, |
| 16685 | 100 | | async, |
| 16685 | 101 | | cancellationToken) |
| 16685 | 102 | | .ConfigureAwait(false); |
| | 103 | | } |
| 96 | 104 | | writer.WriteEndArray(); |
| 96 | 105 | | writer.WriteEndObject(); |
| 96 | 106 | | } |
| | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Creates <see cref="IndexDocumentsBatch{T}"/> instances to update |
| | 111 | | /// search indexes via <see cref="SearchClient.IndexDocuments"/>. |
| | 112 | | /// </summary> |
| | 113 | | public static partial class IndexDocumentsBatch |
| | 114 | | { |
| | 115 | | /// <summary> |
| | 116 | | /// Create an <see cref="IndexDocumentsBatch{T}"/> to upload. |
| | 117 | | /// </summary> |
| | 118 | | /// <typeparam name="T"> |
| | 119 | | /// The .NET type that maps to the index schema. Instances of this type |
| | 120 | | /// can be retrieved as documents from the index. |
| | 121 | | /// </typeparam> |
| | 122 | | /// <param name="documents">The documents to upload.</param> |
| | 123 | | /// <returns> |
| | 124 | | /// An <see cref="IndexDocumentsBatch{T}"/> to upload. |
| | 125 | | /// </returns> |
| | 126 | | public static IndexDocumentsBatch<T> Upload<T>(IEnumerable<T> documents) => |
| | 127 | | new IndexDocumentsBatch<T>(IndexActionType.Upload, documents); |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Create an <see cref="IndexDocumentsBatch{T}"/> to merge. |
| | 131 | | /// </summary> |
| | 132 | | /// <typeparam name="T"> |
| | 133 | | /// The .NET type that maps to the index schema. Instances of this type |
| | 134 | | /// can be retrieved as documents from the index. |
| | 135 | | /// </typeparam> |
| | 136 | | /// <param name="documents">The documents to merge.</param> |
| | 137 | | /// <returns> |
| | 138 | | /// An <see cref="IndexDocumentsBatch{T}"/> to merge. |
| | 139 | | /// </returns> |
| | 140 | | public static IndexDocumentsBatch<T> Merge<T>(IEnumerable<T> documents) => |
| | 141 | | new IndexDocumentsBatch<T>(IndexActionType.Merge, documents); |
| | 142 | |
|
| | 143 | | /// <summary> |
| | 144 | | /// Create an <see cref="IndexDocumentsBatch{T}"/> to merge or upload. |
| | 145 | | /// </summary> |
| | 146 | | /// <typeparam name="T"> |
| | 147 | | /// The .NET type that maps to the index schema. Instances of this type |
| | 148 | | /// can be retrieved as documents from the index. |
| | 149 | | /// </typeparam> |
| | 150 | | /// <param name="documents">The documents to merge or upload.</param> |
| | 151 | | /// <returns> |
| | 152 | | /// An <see cref="IndexDocumentsBatch{T}"/> to merge or upload. |
| | 153 | | /// </returns> |
| | 154 | | public static IndexDocumentsBatch<T> MergeOrUpload<T>(IEnumerable<T> documents) => |
| | 155 | | new IndexDocumentsBatch<T>(IndexActionType.MergeOrUpload, documents); |
| | 156 | |
|
| | 157 | | /// <summary> |
| | 158 | | /// Create an <see cref="IndexDocumentsBatch{T}"/> to delete. |
| | 159 | | /// </summary> |
| | 160 | | /// <typeparam name="T"> |
| | 161 | | /// The .NET type that maps to the index schema. Instances of this type |
| | 162 | | /// can be retrieved as documents from the index. |
| | 163 | | /// </typeparam> |
| | 164 | | /// <param name="documents">The documents to delete.</param> |
| | 165 | | /// <returns> |
| | 166 | | /// An <see cref="IndexDocumentsBatch{T}"/> to delete. |
| | 167 | | /// </returns> |
| | 168 | | public static IndexDocumentsBatch<T> Delete<T>(IEnumerable<T> documents) => |
| | 169 | | new IndexDocumentsBatch<T>(IndexActionType.Delete, documents); |
| | 170 | |
|
| | 171 | | /// <summary> |
| | 172 | | ///Create an <see cref="IndexDocumentsBatch{SearchDocument}"/> to |
| | 173 | | /// delete. |
| | 174 | | /// </summary> |
| | 175 | | /// <param name="keyName"> |
| | 176 | | /// The name of the key field that uniquely identifies documents in |
| | 177 | | /// the index. |
| | 178 | | /// </param> |
| | 179 | | /// <param name="keyValues"> |
| | 180 | | /// The keys of the documents to delete. |
| | 181 | | /// </param> |
| | 182 | | /// <returns> |
| | 183 | | /// An <see cref="IndexDocumentsBatch{SearchDocument}"/> to delete. |
| | 184 | | /// </returns> |
| | 185 | | public static IndexDocumentsBatch<SearchDocument> Delete(string keyName, IEnumerable<string> keyValues) |
| | 186 | | { |
| | 187 | | IndexDocumentsBatch<SearchDocument> batch = new IndexDocumentsBatch<SearchDocument>(); |
| | 188 | | if (keyValues != null) |
| | 189 | | { |
| | 190 | | foreach (string value in keyValues) |
| | 191 | | { |
| | 192 | | batch.Actions.Add(IndexDocumentsAction.Delete(keyName, value)); |
| | 193 | | } |
| | 194 | | } |
| | 195 | | return batch; |
| | 196 | | } |
| | 197 | |
|
| | 198 | | /// <summary> |
| | 199 | | /// Creates a new <see cref="IndexDocumentsBatch{T}"/> for uploading |
| | 200 | | /// documents to the index. |
| | 201 | | /// </summary> |
| | 202 | | /// <typeparam name="T"> |
| | 203 | | /// The .NET type that maps to the index schema. Instances of this type |
| | 204 | | /// can be retrieved as documents from the index. |
| | 205 | | /// </typeparam> |
| | 206 | | /// <param name="actions">The document write actions.</param> |
| | 207 | | /// <returns> |
| | 208 | | /// An <see cref="IndexDocumentsBatch{T}"/> to update. |
| | 209 | | /// </returns> |
| | 210 | | public static IndexDocumentsBatch<T> Create<T>(params IndexDocumentsAction<T>[] actions) |
| | 211 | | { |
| | 212 | | IndexDocumentsBatch<T> batch = new IndexDocumentsBatch<T>(); |
| | 213 | | if (actions != null) |
| | 214 | | { |
| | 215 | | foreach (IndexDocumentsAction<T> action in actions) |
| | 216 | | { |
| | 217 | | batch.Actions.Add(action); |
| | 218 | | } |
| | 219 | | } |
| | 220 | | return batch; |
| | 221 | | } |
| | 222 | | } |
| | 223 | | } |