Skip to content
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

Add PreserveOriginal to NGramTokenFilter #4736

Merged
merged 1 commit into from
Jun 10, 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
22 changes: 20 additions & 2 deletions src/Nest/Analysis/TokenFilters/NgramTokenFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public interface INGramTokenFilter : ITokenFilter
[DataMember(Name ="min_gram")]
[JsonFormatter(typeof(NullableStringIntFormatter))]
int? MinGram { get; set; }

/// <summary>
/// Emits original token when set to `true`. Defaults to `false`.
/// <para />
/// Available in Elasticsearch 7.8.0+
/// </summary>
[DataMember(Name = "preserve_original")]
[JsonFormatter(typeof(NullableStringBooleanFormatter))]
bool? PreserveOriginal { get; set; }
}

/// <inheritdoc />
Expand All @@ -37,6 +46,9 @@ public NGramTokenFilter() : base("ngram") { }

/// <inheritdoc />
public int? MinGram { get; set; }

/// <inheritdoc />
public bool? PreserveOriginal { get; set; }
}

/// <inheritdoc />
Expand All @@ -48,10 +60,16 @@ public class NGramTokenFilterDescriptor

int? INGramTokenFilter.MinGram { get; set; }

/// <inheritdoc />
bool? INGramTokenFilter.PreserveOriginal { get; set; }

/// <inheritdoc cref="INGramTokenFilter.MinGram" />
public NGramTokenFilterDescriptor MinGram(int? minGram) => Assign(minGram, (a, v) => a.MinGram = v);

/// <inheritdoc />
/// <inheritdoc cref="INGramTokenFilter.MaxGram" />
public NGramTokenFilterDescriptor MaxGram(int? maxGram) => Assign(maxGram, (a, v) => a.MaxGram = v);

/// <inheritdoc cref="INGramTokenFilter.PreserveOriginal" />
public NGramTokenFilterDescriptor PreserveOriginal(bool? preserveOriginal = true) =>
Assign(preserveOriginal, (a, v) => a.PreserveOriginal = v);
}
}
12 changes: 12 additions & 0 deletions tests/Tests/Analysis/TokenFilters/TokenFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,18 @@ public class NGramTests : TokenFilterAssertionBase<NGramTests>
public override string Name => "ngram";
}

[SkipVersion("<7.8.0", "PreserveOriginal introduced in 7.8.0")]
public class NGramPreserveOriginalTests : TokenFilterAssertionBase<NGramPreserveOriginalTests>
{
public override FuncTokenFilters Fluent => (n, tf) => tf
.NGram(n, t => t.MinGram(3).MaxGram(4).PreserveOriginal());

public override ITokenFilter Initializer => new NGramTokenFilter { MinGram = 3, MaxGram = 4, PreserveOriginal = true };

public override object Json => new { type = "ngram", min_gram = 3, max_gram = 4, preserve_original = true };
public override string Name => "ngrampo";
}

public class PatternCaptureTests : TokenFilterAssertionBase<PatternCaptureTests>
{
public override FuncTokenFilters Fluent => (n, tf) => tf
Expand Down