Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions src/Nest/Document/Multiple/Bulk/BulkRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public partial class BulkDescriptor
private BulkDescriptor AddOperation(IBulkOperation operation) => Assign(a => a.Operations.AddIfNotNull(operation));

public BulkDescriptor Create<T>(Func<BulkCreateDescriptor<T>, IBulkCreateOperation<T>> bulkCreateSelector) where T : class =>
Assign(a => AddOperation(bulkCreateSelector?.Invoke(new BulkCreateDescriptor<T>())));

Assign(a => AddOperation(bulkCreateSelector?.Invoke(new BulkCreateDescriptor<T>())));
/// <summary>
/// CreateMany, convenience method to create many documents at once.
/// </summary>
Expand All @@ -34,8 +34,8 @@ public BulkDescriptor CreateMany<T>(IEnumerable<T> @objects, Func<BulkCreateDesc
Assign(a => @objects.ForEach(o => AddOperation(bulkCreateSelector.InvokeOrDefault(new BulkCreateDescriptor<T>().Document(o), o))));

public BulkDescriptor Index<T>(Func<BulkIndexDescriptor<T>, IBulkIndexOperation<T>> bulkIndexSelector) where T : class =>
Assign(a => AddOperation(bulkIndexSelector?.Invoke(new BulkIndexDescriptor<T>())));

Assign(a => AddOperation(bulkIndexSelector?.Invoke(new BulkIndexDescriptor<T>())));
/// <summary>
/// IndexMany, convenience method to pass many objects at once.
/// </summary>
Expand All @@ -48,32 +48,50 @@ public BulkDescriptor Delete<T>(T obj, Func<BulkDeleteDescriptor<T>, IBulkDelete
Assign(a => AddOperation(bulkDeleteSelector.InvokeOrDefault(new BulkDeleteDescriptor<T>().Document(obj))));

public BulkDescriptor Delete<T>(Func<BulkDeleteDescriptor<T>, IBulkDeleteOperation<T>> bulkDeleteSelector) where T : class =>
Assign(a => AddOperation(bulkDeleteSelector?.Invoke(new BulkDeleteDescriptor<T>())));

Assign(a => AddOperation(bulkDeleteSelector?.Invoke(new BulkDeleteDescriptor<T>())));
/// <summary>
/// DeleteMany, convenience method to delete many objects at once.
/// </summary>
/// <param name="objects">the objects to delete</param>
/// <param name="bulkDeleteSelector">A func called on each object to describe the individual delete operation</param>
public BulkDescriptor DeleteMany<T>(IEnumerable<T> @objects, Func<BulkDeleteDescriptor<T>, T, IBulkDeleteOperation<T>> bulkDeleteSelector = null) where T : class =>
Assign(a => @objects.ForEach(o => AddOperation(bulkDeleteSelector.InvokeOrDefault(new BulkDeleteDescriptor<T>().Document(o), o))));

Assign(a => @objects.ForEach(o => AddOperation(bulkDeleteSelector.InvokeOrDefault(new BulkDeleteDescriptor<T>().Document(o), o))));
/// <summary>
/// DeleteMany, convenience method to delete many objects at once.
/// </summary>
/// <param name="ids">Enumerable of string ids to delete</param>
/// <param name="bulkDeleteSelector">A func called on each ids to describe the individual delete operation</param>
public BulkDescriptor DeleteMany<T>(IEnumerable<string> ids, Func<BulkDeleteDescriptor<T>, string, IBulkDeleteOperation<T>> bulkDeleteSelector = null) where T : class=>
Assign(a => ids.ForEach(o => AddOperation(bulkDeleteSelector.InvokeOrDefault(new BulkDeleteDescriptor<T>().Id(o), o))));

public BulkDescriptor DeleteMany<T>(IEnumerable<string> ids, Func<BulkDeleteDescriptor<T>, string, IBulkDeleteOperation<T>> bulkDeleteSelector = null) where T : class =>
Assign(a => ids.ForEach(o => AddOperation(bulkDeleteSelector.InvokeOrDefault(new BulkDeleteDescriptor<T>().Id(o), o))));
/// <summary>
/// DeleteMany, convenience method to delete many objects at once.
/// </summary>
/// <param name="ids">Enumerable of int ids to delete</param>
/// <param name="bulkDeleteSelector">A func called on each ids to describe the individual delete operation</param>
public BulkDescriptor DeleteMany<T>(IEnumerable<long> ids, Func<BulkDeleteDescriptor<T>, long, IBulkDeleteOperation<T>> bulkDeleteSelector = null) where T : class =>
Assign(a => ids.ForEach(o => AddOperation(bulkDeleteSelector.InvokeOrDefault(new BulkDeleteDescriptor<T>().Id(o), o))));

Assign(a => ids.ForEach(o => AddOperation(bulkDeleteSelector.InvokeOrDefault(new BulkDeleteDescriptor<T>().Id(o), o))));

/// <summary>
/// Updatemany, convenience method to pass many objects at once to do multiple updates.
/// </summary>
/// <param name="objects">the objects to update</param>
/// <param name="bulkUpdateSelector">An func called on each object to describe the individual update operation</param>
public BulkDescriptor UpdateMany<T>(IEnumerable<T> @objects, Func<BulkUpdateDescriptor<T, T>, T, IBulkUpdateOperation<T, T>> bulkUpdateSelector) where T : class =>
Assign(a => @objects.ForEach(o => AddOperation(bulkUpdateSelector.InvokeOrDefault(new BulkUpdateDescriptor<T, T>().IdFrom(o), o))));

/// <summary>
/// Updatemany, convenience method to pass many objects at once to do multiple updates.
/// </summary>
/// <param name="objects">the objects to update</param>
/// <param name="bulkUpdateSelector">An func called on each object to describe the individual update operation</param>
public BulkDescriptor UpdateMany<T, TPartialDocument>(IEnumerable<T> @objects, Func<BulkUpdateDescriptor<T, TPartialDocument>, T, IBulkUpdateOperation<T, TPartialDocument>> bulkUpdateSelector)
where T : class
where TPartialDocument : class =>
Assign(a => @objects.ForEach(o => AddOperation(bulkUpdateSelector.InvokeOrDefault(new BulkUpdateDescriptor<T, TPartialDocument>().IdFrom(o), o))));

public BulkDescriptor Update<T>(Func<BulkUpdateDescriptor<T, T>, IBulkUpdateOperation<T, T>> bulkUpdateSelector) where T : class =>
this.Update<T, T>(bulkUpdateSelector);

Expand Down
51 changes: 51 additions & 0 deletions src/Tests/Document/Multiple/Bulk/BulkUpdateManyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;
using Tests.Framework;
using Tests.Framework.Integration;
using Tests.Framework.MockData;
using Xunit;

namespace Tests.Document.Multiple.Bulk
{
public class BulkUpdateManyTests : ApiTestBase<ReadOnlyCluster, IBulkResponse, IBulkRequest, BulkDescriptor, BulkRequest>
{
private List<Project> Updates = Project.Projects.Take(10).ToList();

public BulkUpdateManyTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
protected override LazyResponses ClientUsage() => Calls(
fluent: (client, f) => client.Bulk(f),
fluentAsync: (client, f) => client.BulkAsync(f),
request: (client, r) => client.Bulk(r),
requestAsync: (client, r) => client.BulkAsync(r)
);

protected override HttpMethod HttpMethod => HttpMethod.POST;
protected override string UrlPath => $"/{CallIsolatedValue}/_bulk";

protected override bool SupportsDeserialization => false;

protected override object ExpectJson => Updates.SelectMany<Project, object>(ProjectToBulkJson);

private IEnumerable<object> ProjectToBulkJson(Project p)
{
yield return new Dictionary<string, object> { { "update", new { _type = "project", _id = p.Name } } };
yield return new { script = new { inline = "_source.counter++" } };
}

protected override Func<BulkDescriptor, IBulkRequest> Fluent => d => d
.Index(CallIsolatedValue)
.UpdateMany(Updates, (b, u) => b.Script(s => s.Inline("_source.counter++")));


protected override BulkRequest Initializer => new BulkRequest(CallIsolatedValue)
{
Operations = Updates
.Select(u=> new BulkUpdateOperation<Project, Project>(u) { Script = new InlineScript("_source.counter++") })
.ToList<IBulkOperation>()
};
}
}
1 change: 1 addition & 0 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
<Compile Include="Cluster\TaskManagement\GetTask\GetTaskApiTests.cs" />
<Compile Include="Cluster\TaskManagement\GetTask\GetTaskUrlTests.cs" />
<Compile Include="CodeStandards\Responses.doc.cs" />
<Compile Include="Document\Multiple\Bulk\BulkUpdateManyTests.cs" />
<Compile Include="Document\Multiple\Bulk\BulkInvalidVersionApiTests.cs" />
<Compile Include="Document\Multiple\Bulk\BulkResponseParsingTests.cs" />
<Compile Include="Document\Multiple\MultiGet\GetManyApiTests.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# mode either u (unit test), i (integration test) or m (mixed mode)
mode: m
mode: u
# the elasticsearch version that should be started
# Can be a snapshot version of sonatype or "latest" to get the latest snapshot of sonatype
elasticsearch_version: 5.0.1
Expand Down