Skip to content

Commit 16fe8a0

Browse files
Add empty_value parameter to CSV processor (#4597) (#4623)
Add empty_value parameter to CSV processor Co-authored-by: Stuart Cam <stuart.cam@elastic.co>
1 parent a948436 commit 16fe8a0

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/Nest/Ingest/Processors/CsvProcessor.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using System;
22
using System.Linq.Expressions;
33
using System.Runtime.Serialization;
4-
using Elasticsearch.Net;
54
using Elasticsearch.Net.Utf8Json;
65

7-
86
namespace Nest
97
{
108
/// <summary>
@@ -52,6 +50,13 @@ public interface ICsvProcessor : IProcessor
5250
/// </summary>
5351
[DataMember(Name = "trim")]
5452
bool? Trim { get; set; }
53+
54+
/// <summary>
55+
/// Value used to fill empty fields, empty fields will be skipped if this is not provided.
56+
/// Empty field is one with no value (2 consecutive separators) or empty quotes (`""`)
57+
/// </summary>
58+
[DataMember(Name = "empty_value")]
59+
object EmptyValue { get; set; }
5560
}
5661

5762
/// <inheritdoc cref="ICsvProcessor"/>
@@ -70,6 +75,8 @@ public class CsvProcessor : ProcessorBase, ICsvProcessor
7075
/// <inheritdoc />
7176
public bool? Trim { get; set; }
7277
/// <inheritdoc />
78+
public object EmptyValue { get; set; }
79+
/// <inheritdoc />
7380
protected override string Name => "csv";
7481
}
7582

@@ -84,6 +91,7 @@ public class CsvProcessorDescriptor<T> : ProcessorDescriptorBase<CsvProcessorDes
8491
string ICsvProcessor.Quote { get; set; }
8592
string ICsvProcessor.Separator { get; set; }
8693
bool? ICsvProcessor.Trim { get; set; }
94+
object ICsvProcessor.EmptyValue { get; set; }
8795

8896
/// <inheritdoc cref="ICsvProcessor.Field" />
8997
public CsvProcessorDescriptor<T> Field(Field field) => Assign(field, (a, v) => a.Field = v);
@@ -110,5 +118,8 @@ public CsvProcessorDescriptor<T> TargetFields(Func<FieldsDescriptor<T>, IPromise
110118

111119
/// <inheritdoc cref="ICsvProcessor.Separator" />
112120
public CsvProcessorDescriptor<T> Separator(string separator) => Assign(separator, (a, v) => a.Separator = v);
121+
122+
/// <inheritdoc cref="ICsvProcessor.EmptyValue" />
123+
public CsvProcessorDescriptor<T> EmptyValue(object value) => Assign(value, (a, v) => a.EmptyValue = v);
113124
}
114125
}

tests/Tests/Ingest/ProcessorAssertions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,24 @@ public class Csv : ProcessorAssertion
7777
.Csv<Project>(c => c
7878
.Field(p => p.Name)
7979
.TargetFields(new[] { "targetField1", "targetField2" })
80+
.EmptyValue("empty")
81+
.Trim()
8082
);
8183

8284
public override IProcessor Initializer => new CsvProcessor
8385
{
8486
Field = "name",
8587
TargetFields = new[] { "targetField1", "targetField2" },
88+
EmptyValue = "empty",
89+
Trim = true
8690
};
8791

8892
public override object Json => new
8993
{
9094
field = "name",
9195
target_fields = new[] { "targetField1", "targetField2" },
96+
empty_value = "empty",
97+
trim = true
9298
};
9399

94100
public override string Key => "csv";

0 commit comments

Comments
 (0)