Skip to content

Commit

Permalink
Add PreserveOriginal to NGramTokenFilter (#4736) (#4755)
Browse files Browse the repository at this point in the history
Relates: #4718, elastic/elasticsearch#55432
Added in Elasticsearch 7.8.0

Co-authored-by: Russ Cam <[email protected]>
  • Loading branch information
github-actions[bot] and russcam authored Jun 10, 2020
1 parent aa2fab2 commit 690fa58
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
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 @@ -489,6 +489,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

0 comments on commit 690fa58

Please sign in to comment.