Skip to content

Commit d2c5002

Browse files
committed
Merge pull request #795 from elasticsearch/fix/streamline-updates
Try to streamline the update api's
2 parents 84781b5 + b5cae7c commit d2c5002

19 files changed

+405
-133
lines changed

Diff for: src/Nest/DSL/Bulk/BulkUpdateDescriptor.cs

+80-52
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
namespace Nest
77
{
8-
public interface IBulkUpdateOperation<TDocument, TPartialUpdate> : IBulkOperation
8+
public interface IBulkUpdateOperation<TDocument, TPartialDocument> : IBulkOperation
99
where TDocument : class
10-
where TPartialUpdate : class
10+
where TPartialDocument : class
1111
{
12-
TDocument Document { get; set; }
12+
TDocument InferFrom { get; set; }
13+
1314
TDocument Upsert { get; set; }
1415

15-
TPartialUpdate PartialUpdate { get; set; }
16+
TPartialDocument Doc { get; set; }
1617

1718
bool? DocAsUpsert { get; set; }
1819

@@ -23,18 +24,39 @@ public interface IBulkUpdateOperation<TDocument, TPartialUpdate> : IBulkOperatio
2324
Dictionary<string, object> Params { get; set; }
2425
}
2526

26-
public class BulkUpdateOperation<TDocument, TPartialUpdate>
27-
: BulkOperationBase, IBulkUpdateOperation<TDocument, TPartialUpdate>
27+
public class BulkUpdateOperation<TDocument, TPartialDocument> : BulkOperationBase, IBulkUpdateOperation<TDocument, TPartialDocument>
2828
where TDocument : class
29-
where TPartialUpdate : class
29+
where TPartialDocument : class
3030
{
31+
3132

33+
public BulkUpdateOperation(string id) { this.Id = id; }
34+
public BulkUpdateOperation(long id) : this(id.ToString(CultureInfo.InvariantCulture)) {}
3235

33-
public BulkUpdateOperation() {}
34-
public BulkUpdateOperation(TDocument document, TPartialUpdate update) : this()
36+
/// <summary>
37+
/// Create a new bulk operation
38+
/// </summary>
39+
/// <param name="idFrom">Use this document to infer the id from</param>
40+
/// <param name="useIdFromAsUpsert">Use the document to infer on as the upsert document in this update operation</param>
41+
public BulkUpdateOperation(TDocument idFrom, bool useIdFromAsUpsert = false)
3542
{
36-
this.PartialUpdate = update;
37-
this.Document = document;
43+
this.InferFrom = idFrom;
44+
if (useIdFromAsUpsert)
45+
this.Upsert = idFrom;
46+
}
47+
48+
/// <summary>
49+
/// Create a new Bulk Operation
50+
/// </summary>
51+
/// <param name="idFrom">Use this document to infer the id from</param>
52+
/// <param name="update">The partial update document (doc) to send as update</param>
53+
/// <param name="useIdFromAsUpsert">Use the document to infer on as the upsert document in this update operation</param>
54+
public BulkUpdateOperation(TDocument idFrom, TPartialDocument update, bool useIdFromAsUpsert = false)
55+
{
56+
this.InferFrom = idFrom;
57+
if (useIdFromAsUpsert)
58+
this.Upsert = idFrom;
59+
this.Doc = update;
3860
}
3961

4062

@@ -44,14 +66,14 @@ public BulkUpdateOperation(TDocument document, TPartialUpdate update) : this()
4466

4567
public override string GetIdForOperation(ElasticInferrer inferrer)
4668
{
47-
return this.Id ?? inferrer.Id(this.Document);
69+
return this.Id ?? inferrer.Id(this.InferFrom);
4870
}
4971

5072
public override object GetBody()
5173
{
52-
return new BulkUpdateBody<TDocument, TPartialUpdate>
74+
return new BulkUpdateBody<TDocument, TPartialDocument>
5375
{
54-
_PartialUpdate = this.PartialUpdate,
76+
_PartialUpdate = this.Doc,
5577
_Script = this.Script,
5678
_Lang = this.Lang,
5779
_Params = this.Params,
@@ -60,38 +82,43 @@ public override object GetBody()
6082
};
6183
}
6284

63-
public TDocument Document { get; set; }
85+
public TDocument InferFrom { get; set; }
6486
public TDocument Upsert { get; set; }
65-
public TPartialUpdate PartialUpdate { get; set; }
87+
public TPartialDocument Doc { get; set; }
6688
public bool? DocAsUpsert { get; set; }
6789
public string Lang { get; set; }
6890
public string Script { get; set; }
6991
public Dictionary<string, object> Params { get; set; }
7092
}
7193

72-
public class BulkUpdateDescriptor<TDocument, TPartialUpdate>
73-
: BulkOperationDescriptorBase, IBulkUpdateOperation<TDocument, TPartialUpdate>
94+
public class BulkUpdateDescriptor<TDocument, TPartialDocument> : BulkOperationDescriptorBase, IBulkUpdateOperation<TDocument, TPartialDocument>
7495
where TDocument : class
75-
where TPartialUpdate : class
96+
where TPartialDocument : class
7697
{
77-
private IBulkUpdateOperation<TDocument, TPartialUpdate> Self { get { return this; } }
98+
private IBulkUpdateOperation<TDocument, TPartialDocument> Self { get { return this; } }
7899

79100
protected override string BulkOperationType { get { return "update"; } }
80101
protected override Type BulkOperationClrType { get { return typeof(TDocument); } }
81102

82-
TDocument IBulkUpdateOperation<TDocument, TPartialUpdate>.Document { get; set; }
83-
TDocument IBulkUpdateOperation<TDocument, TPartialUpdate>.Upsert { get; set; }
84-
TPartialUpdate IBulkUpdateOperation<TDocument, TPartialUpdate>.PartialUpdate { get; set; }
85-
bool? IBulkUpdateOperation<TDocument, TPartialUpdate>.DocAsUpsert { get; set; }
86-
string IBulkUpdateOperation<TDocument, TPartialUpdate>.Lang { get; set; }
87-
string IBulkUpdateOperation<TDocument, TPartialUpdate>.Script { get; set; }
88-
Dictionary<string, object> IBulkUpdateOperation<TDocument, TPartialUpdate>.Params { get; set; }
103+
TDocument IBulkUpdateOperation<TDocument, TPartialDocument>.InferFrom { get; set; }
104+
105+
TDocument IBulkUpdateOperation<TDocument, TPartialDocument>.Upsert { get; set; }
106+
107+
TPartialDocument IBulkUpdateOperation<TDocument, TPartialDocument>.Doc { get; set; }
108+
109+
bool? IBulkUpdateOperation<TDocument, TPartialDocument>.DocAsUpsert { get; set; }
110+
111+
string IBulkUpdateOperation<TDocument, TPartialDocument>.Lang { get; set; }
112+
113+
string IBulkUpdateOperation<TDocument, TPartialDocument>.Script { get; set; }
114+
115+
Dictionary<string, object> IBulkUpdateOperation<TDocument, TPartialDocument>.Params { get; set; }
89116

90117
protected override object GetBulkOperationBody()
91118
{
92-
return new BulkUpdateBody<TDocument, TPartialUpdate>
119+
return new BulkUpdateBody<TDocument, TPartialDocument>
93120
{
94-
_PartialUpdate = Self.PartialUpdate,
121+
_PartialUpdate = Self.Doc,
95122
_Script = Self.Script,
96123
_Lang = Self.Lang,
97124
_Params = Self.Params,
@@ -102,13 +129,13 @@ protected override object GetBulkOperationBody()
102129

103130
protected override string GetIdForOperation(ElasticInferrer inferrer)
104131
{
105-
return Self.Id ?? inferrer.Id(Self.Document);
132+
return Self.Id ?? inferrer.Id(Self.InferFrom) ?? inferrer.Id(Self.Upsert);
106133
}
107134

108135
/// <summary>
109136
/// Manually set the index, default to the default index or the fixed index set on the bulk operation
110137
/// </summary>
111-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Index(string index)
138+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Index(string index)
112139
{
113140
index.ThrowIfNullOrEmpty("indices");
114141
Self.Index = index;
@@ -118,7 +145,7 @@ public BulkUpdateDescriptor<TDocument, TPartialUpdate> Index(string index)
118145
/// Manualy set the type to get the object from, default to whatever
119146
/// T will be inferred to if not passed or the fixed type set on the parent bulk operation
120147
/// </summary>
121-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Type(string type)
148+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Type(string type)
122149
{
123150
type.ThrowIfNullOrEmpty("type");
124151
Self.Type = type;
@@ -128,7 +155,7 @@ public BulkUpdateDescriptor<TDocument, TPartialUpdate> Type(string type)
128155
/// <summary>
129156
/// Manually set the type of which a typename will be inferred
130157
/// </summary>
131-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Type(Type type)
158+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Type(Type type)
132159
{
133160
type.ThrowIfNull("type");
134161
Self.Type = type;
@@ -138,15 +165,15 @@ public BulkUpdateDescriptor<TDocument, TPartialUpdate> Type(Type type)
138165
/// <summary>
139166
/// Manually set the id for the newly created object
140167
/// </summary>
141-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Id(long id)
168+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Id(long id)
142169
{
143170
return this.Id(id.ToString(CultureInfo.InvariantCulture));
144171
}
145172

146173
/// <summary>
147174
/// Manually set the id for the newly created object
148175
/// </summary>
149-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Id(string id)
176+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Id(string id)
150177
{
151178
Self.Id = id;
152179
return this;
@@ -156,91 +183,92 @@ public BulkUpdateDescriptor<TDocument, TPartialUpdate> Id(string id)
156183
/// The object to update, if id is not manually set it will be inferred from the object.
157184
/// Used ONLY to infer the ID see Document() to apply a partial object merge.
158185
/// </summary>
159-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Document(TDocument @object)
186+
public BulkUpdateDescriptor<TDocument, TPartialDocument> IdFrom(TDocument @object, bool useAsUpsert = false)
160187
{
161-
Self.Document = @object;
188+
Self.InferFrom = @object;
189+
if (useAsUpsert) return this.Upsert(@object);
162190
return this;
163191
}
164192
/// <summary>
165193
/// A document to upsert when the specified document to be updated is not found
166194
/// </summary>
167-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Upsert(TDocument @object)
195+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Upsert(TDocument @object)
168196
{
169197
Self.Upsert = @object;
170198
return this;
171199
}
172200
/// <summary>
173201
/// The partial update document to be merged on to the existing object.
174202
/// </summary>
175-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> PartialUpdate(TPartialUpdate @object)
203+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Doc(TPartialDocument @object)
176204
{
177-
Self.PartialUpdate = @object;
205+
Self.Doc = @object;
178206
return this;
179207
}
180208

181-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> DocAsUpsert(bool docAsUpsert = true)
209+
public BulkUpdateDescriptor<TDocument, TPartialDocument> DocAsUpsert(bool partialDocumentAsUpsert = true)
182210
{
183-
Self.DocAsUpsert = docAsUpsert;
211+
Self.DocAsUpsert = partialDocumentAsUpsert;
184212
return this;
185213
}
186214

187-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Lang(string lang)
215+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Lang(string lang)
188216
{
189217
Self.Lang = lang;
190218
return this;
191219
}
192220

193-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Script(string script)
221+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Script(string script)
194222
{
195223
script.ThrowIfNull("script");
196224
Self.Script = script;
197225
return this;
198226
}
199227

200-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramDictionary)
228+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramDictionary)
201229
{
202230
paramDictionary.ThrowIfNull("paramDictionary");
203231
Self.Params = paramDictionary(new FluentDictionary<string, object>());
204232
return this;
205233
}
206234

207-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Version(string version)
235+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Version(string version)
208236
{
209237
Self.Version = version;
210238
return this;
211239
}
212240

213241

214-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> VersionType(VersionType versionType)
242+
public BulkUpdateDescriptor<TDocument, TPartialDocument> VersionType(VersionType versionType)
215243
{
216244
Self.VersionType = versionType;
217245
return this;
218246
}
219247

220-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Routing(string routing)
248+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Routing(string routing)
221249
{
222250
Self.Routing = routing;
223251
return this;
224252
}
225253

226-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Parent(string parent) {
254+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Parent(string parent) {
227255
Self.Parent = parent;
228256
return this;
229257
}
230258

231-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Timestamp(long timestamp)
259+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Timestamp(long timestamp)
232260
{
233261
Self.Timestamp = timestamp;
234262
return this;
235263
}
236264

237-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> Ttl(string ttl)
265+
public BulkUpdateDescriptor<TDocument, TPartialDocument> Ttl(string ttl)
238266
{
239267
Self.Ttl = ttl;
240268
return this;
241269
}
242270

243-
public BulkUpdateDescriptor<TDocument, TPartialUpdate> RetriesOnConflict(int retriesOnConflict)
271+
public BulkUpdateDescriptor<TDocument, TPartialDocument> RetriesOnConflict(int retriesOnConflict)
244272
{
245273
Self.RetriesOnConflict = retriesOnConflict;
246274
return this;

Diff for: src/Nest/DSL/Paths/DocumentOptionalPathDescriptor.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public abstract class DocumentOptionalPathBase<TParameters, T> : BasePathRequest
8787

8888
public DocumentOptionalPathBase(string id) { this.Id = id; }
8989
public DocumentOptionalPathBase(long id) : this(id.ToString(CultureInfo.InvariantCulture)) {}
90-
public DocumentOptionalPathBase(T document) { this.IdFrom = document; }
90+
public DocumentOptionalPathBase(T idFrom) { this.IdFrom = idFrom; }
9191

9292
protected override void SetRouteParameters(IConnectionSettingsValues settings, ElasticsearchPathInfo<TParameters> pathInfo)
9393
{
@@ -152,14 +152,14 @@ public TDescriptor Type<TAlternative>() where TAlternative : class
152152
}
153153
public TDescriptor Id(long id)
154154
{
155-
return this.Id(id.ToString());
155+
return this.Id(id.ToString(CultureInfo.InvariantCulture));
156156
}
157157
public TDescriptor Id(string id)
158158
{
159159
Self.Id = id;
160160
return (TDescriptor)this;
161161
}
162-
public TDescriptor Object(T @object)
162+
public TDescriptor IdFrom(T @object)
163163
{
164164
Self.IdFrom = @object;
165165
return (TDescriptor)this;

0 commit comments

Comments
 (0)