Skip to content

Commit e77a507

Browse files
committed
fix #2389 add support for .UpdateMany() inside .Bulk() (#2433)
Conflicts: src/Nest/Document/Multiple/Bulk/BulkRequest.cs src/Tests/Tests.csproj
1 parent 6e07203 commit e77a507

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

src/.vs/Elasticsearch.sqlite

1.07 MB
Binary file not shown.

src/Nest/Document/Multiple/Bulk/BulkRequest.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public BulkDescriptor DeleteMany<T>(IEnumerable<T> @objects, Func<BulkDeleteDesc
6363
/// </summary>
6464
/// <param name="ids">Enumerable of string ids to delete</param>
6565
/// <param name="bulkDeleteSelector">A func called on each ids to describe the individual delete operation</param>
66-
public BulkDescriptor DeleteMany<T>(IEnumerable<string> ids, Func<BulkDeleteDescriptor<T>, string, IBulkDeleteOperation<T>> bulkDeleteSelector = null) where T : class=>
66+
public BulkDescriptor DeleteMany<T>(IEnumerable<string> ids, Func<BulkDeleteDescriptor<T>, string, IBulkDeleteOperation<T>> bulkDeleteSelector = null) where T : class =>
6767
Assign(a => ids.ForEach(o => AddOperation(bulkDeleteSelector.InvokeOrDefault(new BulkDeleteDescriptor<T>().Id(o), o))));
6868

6969
/// <summary>
@@ -74,7 +74,25 @@ public BulkDescriptor DeleteMany<T>(IEnumerable<string> ids, Func<BulkDeleteDesc
7474
public BulkDescriptor DeleteMany<T>(IEnumerable<long> ids, Func<BulkDeleteDescriptor<T>, long, IBulkDeleteOperation<T>> bulkDeleteSelector = null) where T : class =>
7575
Assign(a => ids.ForEach(o => AddOperation(bulkDeleteSelector.InvokeOrDefault(new BulkDeleteDescriptor<T>().Id(o), o))));
7676

77-
public BulkDescriptor Update<T>(Func<BulkUpdateDescriptor<T, T>, IBulkUpdateOperation<T, T>> bulkUpdateSelector) where T : class =>
77+
/// <summary>
78+
/// Updatemany, convenience method to pass many objects at once to do multiple updates.
79+
/// </summary>
80+
/// <param name="objects">the objects to update</param>
81+
/// <param name="bulkUpdateSelector">An func called on each object to describe the individual update operation</param>
82+
public BulkDescriptor UpdateMany<T>(IEnumerable<T> @objects, Func<BulkUpdateDescriptor<T, T>, T, IBulkUpdateOperation<T, T>> bulkUpdateSelector) where T : class =>
83+
Assign(a => @objects.ForEach(o => AddOperation(bulkUpdateSelector.InvokeOrDefault(new BulkUpdateDescriptor<T, T>().IdFrom(o), o))));
84+
85+
/// <summary>
86+
/// Updatemany, convenience method to pass many objects at once to do multiple updates.
87+
/// </summary>
88+
/// <param name="objects">the objects to update</param>
89+
/// <param name="bulkUpdateSelector">An func called on each object to describe the individual update operation</param>
90+
public BulkDescriptor UpdateMany<T, TPartialDocument>(IEnumerable<T> @objects, Func<BulkUpdateDescriptor<T, TPartialDocument>, T, IBulkUpdateOperation<T, TPartialDocument>> bulkUpdateSelector)
91+
where T : class
92+
where TPartialDocument : class =>
93+
Assign(a => @objects.ForEach(o => AddOperation(bulkUpdateSelector.InvokeOrDefault(new BulkUpdateDescriptor<T, TPartialDocument>().IdFrom(o), o))));
94+
95+
public BulkDescriptor Update<T>(Func<BulkUpdateDescriptor<T, T>, IBulkUpdateOperation<T, T>> bulkUpdateSelector) where T : class =>
7896
this.Update<T, T>(bulkUpdateSelector);
7997

8098
public BulkDescriptor Update<T, TPartialDocument>(Func<BulkUpdateDescriptor<T, TPartialDocument>, IBulkUpdateOperation<T, TPartialDocument>> bulkUpdateSelector)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Elasticsearch.Net;
5+
using FluentAssertions;
6+
using Nest;
7+
using Tests.Framework;
8+
using Tests.Framework.Integration;
9+
using Tests.Framework.MockData;
10+
using Xunit;
11+
12+
namespace Tests.Document.Multiple.Bulk
13+
{
14+
public class BulkUpdateManyTests : ApiTestBase<ReadOnlyCluster, IBulkResponse, IBulkRequest, BulkDescriptor, BulkRequest>
15+
{
16+
private List<Project> Updates = Project.Projects.Take(10).ToList();
17+
18+
public BulkUpdateManyTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
19+
protected override LazyResponses ClientUsage() => Calls(
20+
fluent: (client, f) => client.Bulk(f),
21+
fluentAsync: (client, f) => client.BulkAsync(f),
22+
request: (client, r) => client.Bulk(r),
23+
requestAsync: (client, r) => client.BulkAsync(r)
24+
);
25+
26+
protected override HttpMethod HttpMethod => HttpMethod.POST;
27+
protected override string UrlPath => $"/{CallIsolatedValue}/_bulk";
28+
29+
protected override bool SupportsDeserialization => false;
30+
31+
protected override object ExpectJson => Updates.SelectMany<Project, object>(ProjectToBulkJson);
32+
33+
private IEnumerable<object> ProjectToBulkJson(Project p)
34+
{
35+
yield return new Dictionary<string, object> { { "update", new { _type = "project", _id = p.Name } } };
36+
yield return new { script = new { inline = "_source.counter++" } };
37+
}
38+
39+
protected override Func<BulkDescriptor, IBulkRequest> Fluent => d => d
40+
.Index(CallIsolatedValue)
41+
.UpdateMany(Updates, (b, u) => b.Script("_source.counter++"));
42+
43+
44+
protected override BulkRequest Initializer => new BulkRequest(CallIsolatedValue)
45+
{
46+
Operations = Updates
47+
.Select(u=> new BulkUpdateOperation<Project, Project>(u) { Script = "_source.counter++" })
48+
.ToList<IBulkOperation>()
49+
};
50+
}
51+
}

src/Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
<Compile Include="ClientConcepts\ConnectionPooling\Dispose\ResponseBuilderDisposeTests.cs" />
240240
<Compile Include="ClientConcepts\Connection\CustomConnections.cs" />
241241
<Compile Include="Document\Multiple\BulkAll\BulkAllApiTests.cs" />
242+
<Compile Include="Document\Multiple\Bulk\BulkUpdateManyTests.cs" />
242243
<Compile Include="Document\Multiple\ReindexRethrottle\ReindexRethrottleApiTests.cs" />
243244
<Compile Include="Document\Multiple\ReindexRethrottle\ReindexRethrottleUrlTests.cs" />
244245
<Compile Include="Framework\Extensions\ShouldExtensions.cs" />

src/Tests/tests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# mode either u (unit test), i (integration test) or m (mixed mode)
2-
mode: m
2+
mode: u
33
# the elasticsearch version that should be started
44
elasticsearch_version: 2.4.1
55
# cluster filter allows you to only run the integration tests of a particular cluster (cluster suffix not needed)
66
cluster_filter:
77
# whether we want to forcefully reseed on the node, if you are starting the tests with a node already running
88
force_reseed: true
99
# do not spawn nodes as part of the test setup but rely on a manually started es node being up
10-
do_not_spawn: false
10+
do_not_spawn: false

0 commit comments

Comments
 (0)