diff --git a/src/Nest/Analysis/TokenFilters/EdgeNGram/EdgeNGramTokenFilter.cs b/src/Nest/Analysis/TokenFilters/EdgeNGram/EdgeNGramTokenFilter.cs index 4c609f98c48..e4cc53cc30c 100644 --- a/src/Nest/Analysis/TokenFilters/EdgeNGram/EdgeNGramTokenFilter.cs +++ b/src/Nest/Analysis/TokenFilters/EdgeNGram/EdgeNGramTokenFilter.cs @@ -31,6 +31,15 @@ public interface IEdgeNGramTokenFilter : ITokenFilter /// [DataMember(Name ="side")] EdgeNGramSide? Side { get; set; } + + /// + /// Emits original token when set to true. Defaults to false. + /// + /// Available in Elasticsearch 7.8.0+ + /// + [DataMember(Name = "preserve_original")] + [JsonFormatter(typeof(NullableStringBooleanFormatter))] + bool? PreserveOriginal { get; set; } } /// @@ -46,6 +55,9 @@ public EdgeNGramTokenFilter() : base("edge_ngram") { } /// public EdgeNGramSide? Side { get; set; } + + /// + public bool? PreserveOriginal { get; set; } } /// @@ -54,17 +66,21 @@ public class EdgeNGramTokenFilterDescriptor { protected override string Type => "edge_ngram"; int? IEdgeNGramTokenFilter.MaxGram { get; set; } - int? IEdgeNGramTokenFilter.MinGram { get; set; } EdgeNGramSide? IEdgeNGramTokenFilter.Side { get; set; } + bool? IEdgeNGramTokenFilter.PreserveOriginal { get; set; } - /// + /// public EdgeNGramTokenFilterDescriptor MinGram(int? minGram) => Assign(minGram, (a, v) => a.MinGram = v); - /// + /// public EdgeNGramTokenFilterDescriptor MaxGram(int? maxGram) => Assign(maxGram, (a, v) => a.MaxGram = v); - /// + /// public EdgeNGramTokenFilterDescriptor Side(EdgeNGramSide? side) => Assign(side, (a, v) => a.Side = v); + + /// + public EdgeNGramTokenFilterDescriptor PreserveOriginal(bool? preserveOriginal = true) => + Assign(preserveOriginal, (a, v) => a.PreserveOriginal = v); } } diff --git a/tests/Tests/Analysis/TokenFilters/TokenFilterTests.cs b/tests/Tests/Analysis/TokenFilters/TokenFilterTests.cs index 8886d1d055d..66437924394 100644 --- a/tests/Tests/Analysis/TokenFilters/TokenFilterTests.cs +++ b/tests/Tests/Analysis/TokenFilters/TokenFilterTests.cs @@ -98,6 +98,18 @@ public class EdgeNgramTests : TokenFilterAssertionBase public override string Name => "etf"; } + [SkipVersion("<7.8.0", "PreserveOriginal introduced in 7.8.0")] + public class EdgeNgramPreserveOriginalTests : TokenFilterAssertionBase + { + public override FuncTokenFilters Fluent => (n, tf) => tf + .EdgeNGram(n, t => t.MaxGram(2).MinGram(1).PreserveOriginal()); + + public override ITokenFilter Initializer => new EdgeNGramTokenFilter { MaxGram = 2, MinGram = 1, PreserveOriginal = true }; + + public override object Json => new { type = "edge_ngram", min_gram = 1, max_gram = 2, preserve_original = true }; + public override string Name => "etfpo"; + } + public class ElisionTests : TokenFilterAssertionBase { public override FuncTokenFilters Fluent => (n, tf) => tf.Elision(n, t => t.Articles("a", "b", "c").ArticlesCase());