Skip to content
This repository was archived by the owner on Jan 17, 2026. It is now read-only.

Commit a0bb712

Browse files
snakefootmarkmcdowell
authored andcommitted
feat: Added option OpCodeCreate to support Elastic data streams
1 parent 6ecdf32 commit a0bb712

1 file changed

Lines changed: 37 additions & 9 deletions

File tree

src/NLog.Targets.ElasticSearch/ElasticSearchTarget.cs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ public string CloudId
150150
/// </summary>
151151
public Layout DocumentType { get; set; } = "_doc";
152152

153+
/// <summary>
154+
/// Gets or sets to only create index for the document if it does not already exist (put if absent). Required when request targets a data stream.
155+
/// </summary>
156+
/// <remarks>
157+
/// Elastic ver. 7.9 is required for using data streams.
158+
/// </remarks>
159+
public bool OpCodeCreate { get; set; }
160+
153161
/// <summary>
154162
/// Gets or sets the pipeline transformation
155163
/// </summary>
@@ -301,7 +309,7 @@ protected override void InitializeTarget()
301309
_documentInfoJsonLayout = new JsonLayout()
302310
{
303311
Attributes = {
304-
new JsonAttribute("index", new JsonLayout()
312+
new JsonAttribute(OpCodeCreate ? "create" : "index", new JsonLayout()
305313
{
306314
Attributes = {
307315
new JsonAttribute("_index", Index) { EscapeForwardSlash = false },
@@ -398,7 +406,7 @@ private PostData FormPayload(ICollection<AsyncLogEventInfo> logEvents)
398406
var type = RenderLogEvent(DocumentType, logEvent);
399407
var pipeLine = RenderLogEvent(Pipeline, logEvent);
400408

401-
var documentInfo = GenerateDocumentInfo(index, type, pipeLine);
409+
var documentInfo = GenerateDocumentInfo(OpCodeCreate, index, type, pipeLine);
402410
var document = GenerateDocumentProperties(logEvent);
403411

404412
payload.Add(documentInfo);
@@ -462,21 +470,41 @@ private Dictionary<string, object> GenerateDocumentProperties(LogEventInfo logEv
462470
return document;
463471
}
464472

465-
private static object GenerateDocumentInfo(string index, string type, string pipeLine)
473+
private static object GenerateDocumentInfo(bool opCodeCreate, string index, string mappingType, string pipeLine)
466474
{
467475
if (string.IsNullOrEmpty(pipeLine))
468476
{
469-
if (string.IsNullOrEmpty(type))
470-
return new { index = new { _index = index } };
477+
if (string.IsNullOrEmpty(mappingType))
478+
{
479+
if (opCodeCreate)
480+
return new { create = new { _index = index } };
481+
else
482+
return new { index = new { _index = index } };
483+
}
471484
else
472-
return new { index = new { _index = index, _type = type } };
485+
{
486+
if (opCodeCreate)
487+
return new { create = new { _index = index, _type = mappingType } };
488+
else
489+
return new { index = new { _index = index, _type = mappingType } };
490+
}
473491
}
474492
else
475493
{
476-
if (string.IsNullOrEmpty(type))
477-
return new { index = new { _index = index, pipeline = pipeLine } };
494+
if (string.IsNullOrEmpty(mappingType))
495+
{
496+
if (opCodeCreate)
497+
return new { create = new { _index = index, pipeline = pipeLine } };
498+
else
499+
return new { index = new { _index = index, pipeline = pipeLine } };
500+
}
478501
else
479-
return new { index = new { _index = index, _type = type, pipeline = pipeLine } };
502+
{
503+
if (opCodeCreate)
504+
return new { create = new { _index = index, _type = mappingType, pipeline = pipeLine } };
505+
else
506+
return new { index = new { _index = index, _type = mappingType, pipeline = pipeLine } };
507+
}
480508
}
481509
}
482510

0 commit comments

Comments
 (0)