-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathBulkUpdateDescriptor.cs
278 lines (231 loc) · 8.1 KB
/
BulkUpdateDescriptor.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using System.Collections.Generic;
using System.Globalization;
using Elasticsearch.Net;
namespace Nest
{
public interface IBulkUpdateOperation<TDocument, TPartialDocument> : IBulkOperation
where TDocument : class
where TPartialDocument : class
{
TDocument InferFrom { get; set; }
TDocument Upsert { get; set; }
TPartialDocument Doc { get; set; }
bool? DocAsUpsert { get; set; }
string Lang { get; set; }
string Script { get; set; }
Dictionary<string, object> Params { get; set; }
}
public class BulkUpdateOperation<TDocument, TPartialDocument> : BulkOperationBase, IBulkUpdateOperation<TDocument, TPartialDocument>
where TDocument : class
where TPartialDocument : class
{
public BulkUpdateOperation(string id) { this.Id = id; }
public BulkUpdateOperation(long id) : this(id.ToString(CultureInfo.InvariantCulture)) {}
/// <summary>
/// Create a new bulk operation
/// </summary>
/// <param name="idFrom">Use this document to infer the id from</param>
/// <param name="useIdFromAsUpsert">Use the document to infer on as the upsert document in this update operation</param>
public BulkUpdateOperation(TDocument idFrom, bool useIdFromAsUpsert = false)
{
this.InferFrom = idFrom;
if (useIdFromAsUpsert)
this.Upsert = idFrom;
}
/// <summary>
/// Create a new Bulk Operation
/// </summary>
/// <param name="idFrom">Use this document to infer the id from</param>
/// <param name="update">The partial update document (doc) to send as update</param>
/// <param name="useIdFromAsUpsert">Use the document to infer on as the upsert document in this update operation</param>
public BulkUpdateOperation(TDocument idFrom, TPartialDocument update, bool useIdFromAsUpsert = false)
{
this.InferFrom = idFrom;
if (useIdFromAsUpsert)
this.Upsert = idFrom;
this.Doc = update;
}
public override string Operation { get { return "update"; } }
public override Type ClrType { get { return typeof(TDocument); } }
public override string GetIdForOperation(ElasticInferrer inferrer)
{
return this.Id ?? inferrer.Id(this.InferFrom);
}
public override object GetBody()
{
return new BulkUpdateBody<TDocument, TPartialDocument>
{
_PartialUpdate = this.Doc,
_Script = this.Script,
_Lang = this.Lang,
_Params = this.Params,
_Upsert = this.Upsert,
_DocAsUpsert = this.DocAsUpsert
};
}
public TDocument InferFrom { get; set; }
public TDocument Upsert { get; set; }
public TPartialDocument Doc { get; set; }
public bool? DocAsUpsert { get; set; }
public string Lang { get; set; }
public string Script { get; set; }
public Dictionary<string, object> Params { get; set; }
}
public class BulkUpdateDescriptor<TDocument, TPartialDocument> : BulkOperationDescriptorBase, IBulkUpdateOperation<TDocument, TPartialDocument>
where TDocument : class
where TPartialDocument : class
{
private IBulkUpdateOperation<TDocument, TPartialDocument> Self { get { return this; } }
protected override string BulkOperationType { get { return "update"; } }
protected override Type BulkOperationClrType { get { return typeof(TDocument); } }
TDocument IBulkUpdateOperation<TDocument, TPartialDocument>.InferFrom { get; set; }
TDocument IBulkUpdateOperation<TDocument, TPartialDocument>.Upsert { get; set; }
TPartialDocument IBulkUpdateOperation<TDocument, TPartialDocument>.Doc { get; set; }
bool? IBulkUpdateOperation<TDocument, TPartialDocument>.DocAsUpsert { get; set; }
string IBulkUpdateOperation<TDocument, TPartialDocument>.Lang { get; set; }
string IBulkUpdateOperation<TDocument, TPartialDocument>.Script { get; set; }
Dictionary<string, object> IBulkUpdateOperation<TDocument, TPartialDocument>.Params { get; set; }
protected override object GetBulkOperationBody()
{
return new BulkUpdateBody<TDocument, TPartialDocument>
{
_PartialUpdate = Self.Doc,
_Script = Self.Script,
_Lang = Self.Lang,
_Params = Self.Params,
_Upsert = Self.Upsert,
_DocAsUpsert = Self.DocAsUpsert
};
}
protected override string GetIdForOperation(ElasticInferrer inferrer)
{
return Self.Id ?? inferrer.Id(Self.InferFrom) ?? inferrer.Id(Self.Upsert);
}
/// <summary>
/// Manually set the index, default to the default index or the fixed index set on the bulk operation
/// </summary>
public BulkUpdateDescriptor<TDocument, TPartialDocument> Index(string index)
{
index.ThrowIfNullOrEmpty("indices");
Self.Index = index;
return this;
}
/// <summary>
/// Manualy set the type to get the object from, default to whatever
/// T will be inferred to if not passed or the fixed type set on the parent bulk operation
/// </summary>
public BulkUpdateDescriptor<TDocument, TPartialDocument> Type(string type)
{
type.ThrowIfNullOrEmpty("type");
Self.Type = type;
return this;
}
/// <summary>
/// Manually set the type of which a typename will be inferred
/// </summary>
public BulkUpdateDescriptor<TDocument, TPartialDocument> Type(Type type)
{
type.ThrowIfNull("type");
Self.Type = type;
return this;
}
/// <summary>
/// Manually set the id for the newly created object
/// </summary>
public BulkUpdateDescriptor<TDocument, TPartialDocument> Id(long id)
{
return this.Id(id.ToString(CultureInfo.InvariantCulture));
}
/// <summary>
/// Manually set the id for the newly created object
/// </summary>
public BulkUpdateDescriptor<TDocument, TPartialDocument> Id(string id)
{
Self.Id = id;
return this;
}
/// <summary>
/// The object to update, if id is not manually set it will be inferred from the object.
/// Used ONLY to infer the ID see Document() to apply a partial object merge.
/// </summary>
public BulkUpdateDescriptor<TDocument, TPartialDocument> IdFrom(TDocument @object, bool useAsUpsert = false)
{
Self.InferFrom = @object;
if (useAsUpsert) return this.Upsert(@object);
return this;
}
/// <summary>
/// A document to upsert when the specified document to be updated is not found
/// </summary>
public BulkUpdateDescriptor<TDocument, TPartialDocument> Upsert(TDocument @object)
{
Self.Upsert = @object;
return this;
}
/// <summary>
/// The partial update document to be merged on to the existing object.
/// </summary>
public BulkUpdateDescriptor<TDocument, TPartialDocument> Doc(TPartialDocument @object)
{
Self.Doc = @object;
return this;
}
public BulkUpdateDescriptor<TDocument, TPartialDocument> DocAsUpsert(bool partialDocumentAsUpsert = true)
{
Self.DocAsUpsert = partialDocumentAsUpsert;
return this;
}
public BulkUpdateDescriptor<TDocument, TPartialDocument> Lang(string lang)
{
Self.Lang = lang;
return this;
}
public BulkUpdateDescriptor<TDocument, TPartialDocument> Script(string script)
{
script.ThrowIfNull("script");
Self.Script = script;
return this;
}
public BulkUpdateDescriptor<TDocument, TPartialDocument> Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramDictionary)
{
paramDictionary.ThrowIfNull("paramDictionary");
Self.Params = paramDictionary(new FluentDictionary<string, object>());
return this;
}
public BulkUpdateDescriptor<TDocument, TPartialDocument> Version(string version)
{
Self.Version = version;
return this;
}
public BulkUpdateDescriptor<TDocument, TPartialDocument> VersionType(VersionType versionType)
{
Self.VersionType = versionType;
return this;
}
public BulkUpdateDescriptor<TDocument, TPartialDocument> Routing(string routing)
{
Self.Routing = routing;
return this;
}
public BulkUpdateDescriptor<TDocument, TPartialDocument> Parent(string parent) {
Self.Parent = parent;
return this;
}
public BulkUpdateDescriptor<TDocument, TPartialDocument> Timestamp(long timestamp)
{
Self.Timestamp = timestamp;
return this;
}
public BulkUpdateDescriptor<TDocument, TPartialDocument> Ttl(string ttl)
{
Self.Ttl = ttl;
return this;
}
public BulkUpdateDescriptor<TDocument, TPartialDocument> RetriesOnConflict(int retriesOnConflict)
{
Self.RetriesOnConflict = retriesOnConflict;
return this;
}
}
}