-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
IndexDocumentsAction.cs
207 lines (189 loc) · 7.97 KB
/
IndexDocumentsAction.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
204
205
206
207
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
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 IndexAction
[CodeGenModel("IndexAction")]
internal partial class IndexAction { }
/// <summary>
/// Represents an index action that operates on a document.
/// </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 class IndexDocumentsAction<T>
{
/// <summary>
/// The operation to perform on a document in an indexing batch.
/// </summary>
public IndexActionType ActionType { get; private set; }
/// <summary>
/// The document to index.
/// </summary>
public T Document { get; private set; }
/// <summary>
/// Private constructor to prevent direct instantiation.
/// </summary>
private IndexDocumentsAction() { }
/// <summary>
/// Initializes a new instance of the IndexDocumentsAction class.
/// </summary>
/// <param name="type">
/// The operation to perform on the document.
/// </param>
/// <param name="doc">The document to index.</param>
public IndexDocumentsAction(IndexActionType type, T doc)
{
Debug.Assert(Enum.IsDefined(typeof(IndexActionType), type));
ActionType = type;
Document = doc;
}
#pragma warning disable CS1572 // Not all parameters will be used depending on feature flags
#pragma warning disable CA1801 // Not all parameters are used depending on feature flags
#pragma warning disable CS1998 // Won't await depending on feature flags
/// <summary>
/// Serialize the document write action.
/// </summary>
/// <param name="writer">The JSON writer.</param>
/// <param name="serializer">
/// Optional serializer that can be used to customize the serialization
/// of strongly typed models.
/// </param>
/// <param name="options">JSON serializer options.</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>A task representing the serialization.</returns>
internal async Task SerializeAsync(
Utf8JsonWriter writer,
ObjectSerializer serializer,
JsonSerializerOptions options,
bool async,
CancellationToken cancellationToken)
#pragma warning restore CS1998
#pragma warning restore CA1801
#pragma warning restore CS1572
{
Debug.Assert(writer != null);
writer.WriteStartObject();
writer.WriteString(Constants.SearchActionKeyJson, ActionType.ToSerialString());
// HACK: Serialize the user's model, parse it, and then write each
// of its properties as if they were our own.
byte[] json;
if (serializer != null)
{
using MemoryStream stream = new MemoryStream();
if (async)
{
await serializer.SerializeAsync(stream, Document, typeof(T), cancellationToken).ConfigureAwait(false);
}
else
{
serializer.Serialize(stream, Document, typeof(T), cancellationToken);
}
json = stream.ToArray();
}
else
{
json = JsonSerializer.SerializeToUtf8Bytes<T>(Document, options);
}
using JsonDocument nested = JsonDocument.Parse(json);
foreach (JsonProperty property in nested.RootElement.EnumerateObject())
{
property.WriteTo(writer);
}
writer.WriteEndObject();
}
}
/// <summary>
/// Creates <see cref="IndexDocumentsAction{T}"/> instances to add to an
/// <see cref="IndexDocumentsBatch{T}"/> and submit via
/// <see cref="SearchClient.IndexDocuments"/>.
/// </summary>
public static class IndexDocumentsAction
{
/// <summary>
/// Create an <see cref="IndexDocumentsAction{T}"/> to upload.
/// </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="document">The document to upload.</param>
/// <returns>
/// An <see cref="IndexDocumentsAction{T}"/> to upload.
/// </returns>
public static IndexDocumentsAction<T> Upload<T>(T document) =>
new IndexDocumentsAction<T>(IndexActionType.Upload, document);
/// <summary>
/// Create an <see cref="IndexDocumentsAction{T}"/> to merge.
/// </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="document">The document to merge.</param>
/// <returns>
/// An <see cref="IndexDocumentsAction{T}"/> to merge.
/// </returns>
public static IndexDocumentsAction<T> Merge<T>(T document) =>
new IndexDocumentsAction<T>(IndexActionType.Merge, document);
/// <summary>
/// Create an <see cref="IndexDocumentsAction{T}"/> to merge or upload.
/// </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="document">The document to merge or upload.</param>
/// <returns>
/// An <see cref="IndexDocumentsAction{T}"/> to merge or upload.
/// </returns>
public static IndexDocumentsAction<T> MergeOrUpload<T>(T document) =>
new IndexDocumentsAction<T>(IndexActionType.MergeOrUpload, document);
/// <summary>
/// Create an <see cref="IndexDocumentsAction{T}"/> to delete.
/// </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="document">The document to delete.</param>
/// <returns>
/// An <see cref="IndexDocumentsAction{T}"/> to delete.
/// </returns>
public static IndexDocumentsAction<T> Delete<T>(T document) =>
new IndexDocumentsAction<T>(IndexActionType.Delete, document);
/// <summary>
/// Create an <see cref="IndexDocumentsAction{SearchDocument}"/> to
/// delete.
/// </summary>
/// <param name="keyName">
/// The name of the key field of the index.
/// </param>
/// <param name="keyValue">The key of the document to delete.</param>
/// <returns>
/// An <see cref="IndexDocumentsAction{SearchDocument}"/> to delete.
/// </returns>
public static IndexDocumentsAction<SearchDocument> Delete(string keyName, string keyValue)
{
Argument.AssertNotNullOrEmpty(keyName, nameof(keyName));
Argument.AssertNotNull(keyValue, nameof(keyValue));
return new IndexDocumentsAction<SearchDocument>(
IndexActionType.Delete,
new SearchDocument { [keyName] = keyValue });
}
}
}