Skip to content

Commit 5899904

Browse files
committed
fix #388, IndexMany and DeleteMany now internally use Bulk, also added support for other missing bulk params when using indexmany or deletemany. Also fixed several integration test that were failing now that the query dsl is truly immutable
1 parent de6f8cb commit 5899904

File tree

12 files changed

+139
-88
lines changed

12 files changed

+139
-88
lines changed

src/Nest.Tests.Integration/Reproduce/Reproduce346Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public void NoSearchResults()
5454
)
5555
);
5656

57-
//Approval settings appears twice because we are spawing the nested queries of x
58-
Assert.AreEqual(2, Regex.Matches(response.ConnectionStatus.Request, @"approvalSettings\.approved").Count);
57+
//Approval settings should not appear twice just because we are spawing the nested queries of wrong lambda parameter (x)
58+
Assert.AreEqual(1, Regex.Matches(response.ConnectionStatus.Request, @"approvalSettings\.approved").Count, response.ConnectionStatus.Request);
5959

6060
//either use the lambda overload
6161
response = client.Count<MediaStreamEntry>(

src/Nest/DSL/BulkCreateDescriptor.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ public BulkCreateDescriptor<T> VersionType(string versionType)
9898
return this;
9999
}
100100

101+
public BulkCreateDescriptor<T> VersionType(VersionType versionType)
102+
{
103+
switch (versionType)
104+
{
105+
case Nest.VersionType.External:
106+
this._VersionType = "external";
107+
break;
108+
case Nest.VersionType.Internal:
109+
this._VersionType = "internal";
110+
break;
111+
}
112+
return this;
113+
}
114+
101115
public BulkCreateDescriptor<T> Routing(string routing)
102116
{
103117
this._Routing = routing;

src/Nest/DSL/BulkDeleteDescriptor.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ public BulkDeleteDescriptor<T> VersionType(string versionType)
9898
return this;
9999
}
100100

101+
public BulkDeleteDescriptor<T> VersionType(VersionType versionType)
102+
{
103+
switch (versionType)
104+
{
105+
case Nest.VersionType.External:
106+
this._VersionType = "external";
107+
break;
108+
case Nest.VersionType.Internal:
109+
this._VersionType = "internal";
110+
break;
111+
}
112+
return this;
113+
}
114+
101115
public BulkDeleteDescriptor<T> Routing(string routing)
102116
{
103117
this._Routing = routing;

src/Nest/DSL/BulkIndexDescriptor.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ internal override string GetIdForObject(ElasticInferrer inferrer)
3737
/// </summary>
3838
public BulkIndexDescriptor<T> Index(string index)
3939
{
40-
index.ThrowIfNullOrEmpty("indices");
4140
this._Index = index;
4241
return this;
4342
}
@@ -47,7 +46,6 @@ public BulkIndexDescriptor<T> Index(string index)
4746
/// </summary>
4847
public BulkIndexDescriptor<T> Type(string type)
4948
{
50-
type.ThrowIfNullOrEmpty("type");
5149
this._Type = type;
5250
return this;
5351
}
@@ -57,7 +55,6 @@ public BulkIndexDescriptor<T> Type(string type)
5755
/// </summary>
5856
public BulkIndexDescriptor<T> Type(Type type)
5957
{
60-
type.ThrowIfNull("type");
6158
this._Type = type;
6259
return this;
6360
}
@@ -101,6 +98,20 @@ public BulkIndexDescriptor<T> VersionType(string versionType)
10198
return this;
10299
}
103100

101+
public BulkIndexDescriptor<T> VersionType(VersionType versionType)
102+
{
103+
switch (versionType)
104+
{
105+
case Nest.VersionType.External:
106+
this._VersionType = "external";
107+
break;
108+
case Nest.VersionType.Internal:
109+
this._VersionType = "internal";
110+
break;
111+
}
112+
return this;
113+
}
114+
104115
public BulkIndexDescriptor<T> Routing(string routing)
105116
{
106117
this._Routing = routing;

src/Nest/DSL/BulkUpdateDescriptor.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,21 @@ public BulkUpdateDescriptor<T, K> VersionType(string versionType)
171171
return this;
172172
}
173173

174+
175+
public BulkUpdateDescriptor<T, K> VersionType(VersionType versionType)
176+
{
177+
switch (versionType)
178+
{
179+
case Nest.VersionType.External:
180+
this._VersionType = "external";
181+
break;
182+
case Nest.VersionType.Internal:
183+
this._VersionType = "internal";
184+
break;
185+
}
186+
return this;
187+
}
188+
174189
public BulkUpdateDescriptor<T, K> Routing(string routing)
175190
{
176191
this._Routing = routing;

src/Nest/DSL/SearchDescriptor.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -938,11 +938,12 @@ public SearchDescriptor<T> Query(BaseQuery query)
938938
public SearchDescriptor<T> QueryString(string userInput)
939939
{
940940
var q = new QueryDescriptor<T>();
941+
BaseQuery bq;
941942
if (userInput.IsNullOrEmpty())
942-
q.MatchAll();
943+
bq = q.MatchAll();
943944
else
944-
q.QueryString(qs => qs.Query(userInput));
945-
this._Query = q;
945+
bq = q.QueryString(qs => qs.Query(userInput));
946+
this._Query = bq;
946947
return this;
947948
}
948949

src/Nest/Domain/Parameters/BulkParameters.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public class BulkParameters<T> where T : class
1919
public string Parent { get; set; }
2020

2121
public T Document { get; private set; }
22+
public string Ttl { get; set; }
23+
public long Timestamp { get; set; }
24+
public string Percolate { get; set; }
25+
2226
public BulkParameters(T document)
2327
{
2428
this.Document = document;

src/Nest/ElasticClient-Bulk.cs

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ private void GenerateBulkPathAndJson(BulkDescriptor bulkDescriptor, out string j
8686
path += queryString.ToQueryString();
8787

8888
}
89+
8990
public IBulkResponse Bulk(BulkDescriptor bulkDescriptor)
9091
{
9192
string json, path;
@@ -193,72 +194,64 @@ private string GenerateBulkCommand<T>(IEnumerable<BulkParameters<T>> objects, st
193194
}
194195

195196

197+
198+
//used by IndexMany and DeleteMany
196199
private string GenerateBulkCommand<T>(IEnumerable<T> @objects, string index, string typeName, string command) where T : class
197200
{
198201
objects.ThrowIfEmpty("objects");
199202

200-
var sb = new StringBuilder();
201-
var action = "{{ \"{0}\" : {{ \"_index\" : \"{1}\", \"_type\" : \"{2}\"".F(command, index, typeName);
202-
203-
foreach (var @object in objects)
203+
var b = new BulkDescriptor();
204+
b.FixedPath(index, typeName);
205+
foreach (var @object in @objects)
204206
{
205-
var objectAction = action;
206-
207-
var id = this.Infer.Id(@object);
208-
if (!id.IsNullOrEmpty())
209-
objectAction += ", \"_id\" : \"{0}\" ".F(id);
210-
211-
objectAction += "} }\n";
212-
213-
sb.Append(objectAction);
207+
var o = @object;
214208
if (command == "index")
215-
{
216-
string jsonCommand = this.Serializer.Serialize(@object, Formatting.None);
217-
sb.Append(jsonCommand + "\n");
218-
}
209+
b.Index<T>(bb => bb.Object(o));
210+
else if (command == "delete")
211+
b.Delete<T>(bb => bb.Object(o));
219212
}
220-
var json = sb.ToString();
213+
214+
string json, path;
215+
this.GenerateBulkPathAndJson(b, out json, out path);
221216
return json;
217+
}
222218

223219

224-
225-
}
220+
//used by IndexMany and DeleteMany
226221
private string GenerateBulkCommand<T>(IEnumerable<BulkParameters<T>> @objects, string index, string typeName, string command) where T : class
227222
{
228223
objects.ThrowIfEmpty("objects");
229224

230-
var sb = new StringBuilder();
231-
var action = "{{ \"{0}\" : {{ \"_index\" : \"{1}\", \"_type\" : \"{2}\"".F(command, index, typeName);
232225

233-
foreach (var @object in objects)
226+
var b = new BulkDescriptor();
227+
b.FixedPath(index, typeName);
228+
foreach (var @object in @objects)
234229
{
235-
if (@object.Document == null)
236-
continue;
237-
238-
var objectAction = action;
239-
if (!@object.Id.IsNullOrEmpty())
240-
objectAction += ", \"_id\" : \"{0}\" ".F(@object.Id);
241-
else
242-
objectAction += ", \"_id\" : \"{0}\" ".F(this.Infer.Id(@object.Document));
243-
244-
if (!@object.Version.IsNullOrEmpty())
245-
objectAction += ", \"version\" : \"{0}\" ".F(@object.Version);
246-
if (!@object.Parent.IsNullOrEmpty())
247-
objectAction += ", \"parent\" : \"{0}\" ".F(@object.Parent);
248-
if (@object.VersionType != VersionType.Internal)
249-
objectAction += ", \"version_type\" : \"{0}\" ".F(@object.VersionType.ToString().ToLower());
250-
if (!@object.Routing.IsNullOrEmpty())
251-
objectAction += ", \"routing\" : \"{0}\" ".F(@object.Routing);
252-
objectAction += "} }\n";
253-
254-
sb.Append(objectAction);
230+
var o = @object;
255231
if (command == "index")
256-
{
257-
string jsonCommand = this.Serializer.Serialize(@object.Document, Formatting.None);
258-
sb.Append(jsonCommand + "\n");
259-
}
232+
b.Index<T>(bb => bb
233+
.Object(o.Document)
234+
.Id(o.Id)
235+
.Parent(o.Parent)
236+
.Percolate(o.Percolate)
237+
.Routing(o.Routing)
238+
.Timestamp(o.Timestamp)
239+
.Ttl(o.Ttl)
240+
.Version(o.Version)
241+
.VersionType(o.VersionType));
242+
else if (command == "delete")
243+
b.Delete<T>(bb => bb
244+
.Object(o.Document)
245+
.Parent(o.Parent)
246+
.Routing(o.Routing)
247+
.Timestamp(o.Timestamp)
248+
.Ttl(o.Ttl)
249+
.Version(o.Version)
250+
.VersionType(o.VersionType));
260251
}
261-
var json = sb.ToString();
252+
253+
string json, path;
254+
this.GenerateBulkPathAndJson(b, out json, out path);
262255
return json;
263256
}
264257

src/Nest/ElasticClient-Count.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public ICountResponse CountAll<T>(Func<QueryDescriptor<T>, BaseQuery> querySelec
3232
{
3333
querySelector.ThrowIfNull("querySelector");
3434
var descriptor = new QueryDescriptor<T>();
35-
querySelector(descriptor);
36-
var query = this.Serialize(descriptor);
35+
var bq = querySelector(descriptor);
36+
var query = this.Serialize(bq);
3737
return this._Count("_count", query);
3838
}
3939

@@ -92,8 +92,8 @@ public ICountResponse Count<T>(Func<QueryDescriptor<T>, BaseQuery> querySelector
9292
var typeName = this.Infer.TypeName<T>();
9393
string path = this.PathResolver.CreateIndexTypePath(index, typeName, "_count");
9494
var descriptor = new QueryDescriptor<T>();
95-
querySelector(descriptor);
96-
var query = this.Serialize(descriptor);
95+
var bq = querySelector(descriptor);
96+
var query = this.Serialize(bq);
9797
return _Count(path, query);
9898
}
9999

@@ -105,8 +105,8 @@ public ICountResponse Count<T>(IEnumerable<string> indices, Func<QueryDescriptor
105105
indices.ThrowIfEmpty("indices");
106106
string path = this.PathResolver.CreateIndexPath(indices, "_count");
107107
var descriptor = new QueryDescriptor<T>();
108-
querySelector(descriptor);
109-
var query = this.Serialize(descriptor);
108+
var bq = querySelector(descriptor);
109+
var query = this.Serialize(bq);
110110
return _Count(path, query);
111111
}
112112

@@ -119,8 +119,8 @@ public ICountResponse Count<T>(IEnumerable<string> indices, IEnumerable<string>
119119
indices.ThrowIfEmpty("types");
120120
string path = this.PathResolver.CreateIndexTypePath(indices, types, "_count");
121121
var descriptor = new QueryDescriptor<T>();
122-
querySelector(descriptor);
123-
var query = this.Serialize(descriptor);
122+
var bq = querySelector(descriptor);
123+
var query = this.Serialize(bq);
124124
return _Count(path, query);
125125
}
126126

src/Nest/ElasticClient-DeleteByQuery.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public partial class ElasticClient
1212
/// <param name="query">RoutingQueryPathDescriptor also allows you to control which indices and types are affected</param>
1313
/// <param name="parameters">Control routing/consistency and replication</param>
1414
/// <returns>IDeleteResponse, check .IsValid to validate success</returns>
15-
public IDeleteResponse DeleteByQuery<T>(Action<RoutingQueryPathDescriptor<T>> query, DeleteByQueryParameters parameters = null) where T : class
15+
public IDeleteResponse DeleteByQuery<T>(Func<RoutingQueryPathDescriptor<T>, BaseQuery> query, DeleteByQueryParameters parameters = null) where T : class
1616
{
1717
var descriptor = new RoutingQueryPathDescriptor<T>();
18-
query(descriptor);
19-
var stringQuery = this.Serialize(descriptor);
18+
var bq = query(descriptor);
19+
var stringQuery = this.Serialize(bq);
2020
var path = this.PathResolver.GetPathForTyped(descriptor, "_query");
2121
if (parameters != null)
2222
path = this.PathResolver.AppendDeleteByQueryParametersToPath(path, parameters);
@@ -29,11 +29,11 @@ public IDeleteResponse DeleteByQuery<T>(Action<RoutingQueryPathDescriptor<T>> qu
2929
/// <param name="query">RoutingQueryPathDescriptor also allows you to control which indices and types are affected</param>
3030
/// <param name="parameters">Control routing/consistency and replication</param>
3131
/// <returns>IDeleteResponse, check .IsValid to validate success</returns>
32-
public IDeleteResponse DeleteByQuery(Action<RoutingQueryPathDescriptor> query, DeleteByQueryParameters parameters = null)
32+
public IDeleteResponse DeleteByQuery(Func<RoutingQueryPathDescriptor, BaseQuery> query, DeleteByQueryParameters parameters = null)
3333
{
3434
var descriptor = new RoutingQueryPathDescriptor();
35-
query(descriptor);
36-
var stringQuery = this.Serialize(descriptor);
35+
var bq = query(descriptor);
36+
var stringQuery = this.Serialize(bq);
3737
var path = this.PathResolver.GetDeleteByQueryPathForDynamic(descriptor, "_query");
3838
if (parameters != null)
3939
path = this.PathResolver.AppendDeleteByQueryParametersToPath(path, parameters);
@@ -46,11 +46,11 @@ public IDeleteResponse DeleteByQuery(Action<RoutingQueryPathDescriptor> query, D
4646
/// <param name="query">RoutingQueryPathDescriptor also allows you to control which indices and types are affected</param>
4747
/// <param name="parameters">Control routing/consistency and replication</param>
4848
/// <returns>IDeleteResponse, check .IsValid to validate success</returns>
49-
public Task<IDeleteResponse> DeleteByQueryAsync<T>(Action<RoutingQueryPathDescriptor<T>> query, DeleteByQueryParameters parameters = null) where T : class
49+
public Task<IDeleteResponse> DeleteByQueryAsync<T>(Func<RoutingQueryPathDescriptor<T>, BaseQuery> query, DeleteByQueryParameters parameters = null) where T : class
5050
{
5151
var descriptor = new RoutingQueryPathDescriptor<T>();
52-
query(descriptor);
53-
var stringQuery = this.Serialize(descriptor);
52+
var bq = query(descriptor);
53+
var stringQuery = this.Serialize(bq);
5454
var path = this.PathResolver.GetPathForTyped(descriptor, "_query");
5555
if (parameters != null)
5656
path = this.PathResolver.AppendDeleteByQueryParametersToPath(path, parameters);
@@ -63,11 +63,11 @@ public Task<IDeleteResponse> DeleteByQueryAsync<T>(Action<RoutingQueryPathDescri
6363
/// <param name="query">RoutingQueryPathDescriptor also allows you to control which indices and types are affected</param>
6464
/// <param name="parameters">Control routing/consistency and replication</param>
6565
/// <returns>IDeleteResponse, check .IsValid to validate success</returns>
66-
public Task<IDeleteResponse> DeleteByQueryAsync(Action<RoutingQueryPathDescriptor> query, DeleteByQueryParameters parameters = null)
66+
public Task<IDeleteResponse> DeleteByQueryAsync(Func<RoutingQueryPathDescriptor, BaseQuery> query, DeleteByQueryParameters parameters = null)
6767
{
6868
var descriptor = new RoutingQueryPathDescriptor();
69-
query(descriptor);
70-
var stringQuery = this.Serialize(descriptor);
69+
var bq = query(descriptor);
70+
var stringQuery = this.Serialize(bq);
7171
var path = this.PathResolver.GetDeleteByQueryPathForDynamic(descriptor, "_query");
7272
if (parameters != null)
7373
path = this.PathResolver.AppendDeleteByQueryParametersToPath(path, parameters);

0 commit comments

Comments
 (0)