Skip to content

Commit 5f560a0

Browse files
authored
Support manual control of the bulk operation (#42)
1 parent ec97f60 commit 5f560a0

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/Elastic.Ingest.Elasticsearch/Indices/IndexChannelOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ public IndexChannelOptions(HttpTransport transport) : base(transport) { }
5151
/// <para>https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html#bulk-api-request-body</para>
5252
/// </summary>
5353
public Func<TEvent, string, bool>? BulkUpsertLookup { get; set; }
54+
55+
/// <summary>
56+
/// Control the operation header for each bulk operation.
57+
/// </summary>
58+
public OperationMode OperationMode { get; set; }
5459
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
namespace Elastic.Ingest.Elasticsearch.Indices;
5+
6+
/// <summary>
7+
/// Determines the operation header for each bulk operation.
8+
/// </summary>
9+
public enum OperationMode
10+
{
11+
/// <summary>
12+
/// The mode will be determined automatically based on default rules for the preferred mode.
13+
/// </summary>
14+
Auto = 0,
15+
16+
/// <summary>
17+
/// Each document will be sent with an 'index' bulk operation header.
18+
/// </summary>
19+
Index = 1,
20+
21+
/// <summary>
22+
/// Each document will be sent with a 'create' bulk operation header.
23+
/// </summary>
24+
Create = 2
25+
}

src/Elastic.Ingest.Elasticsearch/Serialization/BulkRequestDataFactory.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ public static BulkOperationHeader CreateBulkOperationHeaderForIndex<TEvent>(TEve
138138

139139
var id = options.BulkOperationIdLookup?.Invoke(@event);
140140

141+
if (options.OperationMode == OperationMode.Index)
142+
{
143+
return skipIndexName
144+
? !string.IsNullOrWhiteSpace(id) ? new IndexOperation { Id = id } : new IndexOperation()
145+
: !string.IsNullOrWhiteSpace(id) ? new IndexOperation { Index = index, Id = id } : new IndexOperation { Index = index };
146+
}
147+
148+
if (options.OperationMode == OperationMode.Create)
149+
{
150+
return skipIndexName
151+
? !string.IsNullOrWhiteSpace(id) ? new CreateOperation { Id = id } : new CreateOperation()
152+
: !string.IsNullOrWhiteSpace(id) ? new CreateOperation { Index = index, Id = id } : new CreateOperation { Index = index };
153+
}
154+
141155
if (!string.IsNullOrWhiteSpace(id) && id != null && (options.BulkUpsertLookup?.Invoke(@event, id) ?? false))
142156
return skipIndexName ? new UpdateOperation { Id = id } : new UpdateOperation { Id = id, Index = index };
143157

0 commit comments

Comments
 (0)