Skip to content

[Backport master] Add empty_value parameter to CSV processor #4623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2020
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
15 changes: 13 additions & 2 deletions src/Nest/Ingest/Processors/CsvProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using Elasticsearch.Net;
using Elasticsearch.Net.Utf8Json;


namespace Nest
{
/// <summary>
Expand Down Expand Up @@ -52,6 +50,13 @@ public interface ICsvProcessor : IProcessor
/// </summary>
[DataMember(Name = "trim")]
bool? Trim { get; set; }

/// <summary>
/// Value used to fill empty fields, empty fields will be skipped if this is not provided.
/// Empty field is one with no value (2 consecutive separators) or empty quotes (`""`)
/// </summary>
[DataMember(Name = "empty_value")]
object EmptyValue { get; set; }
}

/// <inheritdoc cref="ICsvProcessor"/>
Expand All @@ -70,6 +75,8 @@ public class CsvProcessor : ProcessorBase, ICsvProcessor
/// <inheritdoc />
public bool? Trim { get; set; }
/// <inheritdoc />
public object EmptyValue { get; set; }
/// <inheritdoc />
protected override string Name => "csv";
}

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

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

/// <inheritdoc cref="ICsvProcessor.Separator" />
public CsvProcessorDescriptor<T> Separator(string separator) => Assign(separator, (a, v) => a.Separator = v);

/// <inheritdoc cref="ICsvProcessor.EmptyValue" />
public CsvProcessorDescriptor<T> EmptyValue(object value) => Assign(value, (a, v) => a.EmptyValue = v);
}
}
6 changes: 6 additions & 0 deletions tests/Tests/Ingest/ProcessorAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,24 @@ public class Csv : ProcessorAssertion
.Csv<Project>(c => c
.Field(p => p.Name)
.TargetFields(new[] { "targetField1", "targetField2" })
.EmptyValue("empty")
.Trim()
);

public override IProcessor Initializer => new CsvProcessor
{
Field = "name",
TargetFields = new[] { "targetField1", "targetField2" },
EmptyValue = "empty",
Trim = true
};

public override object Json => new
{
field = "name",
target_fields = new[] { "targetField1", "targetField2" },
empty_value = "empty",
trim = true
};

public override string Key => "csv";
Expand Down