diff --git a/src/Nest/Analysis/TokenFilters/NgramTokenFilter.cs b/src/Nest/Analysis/TokenFilters/NgramTokenFilter.cs
index 036e2441fcd..f46d90b53d6 100644
--- a/src/Nest/Analysis/TokenFilters/NgramTokenFilter.cs
+++ b/src/Nest/Analysis/TokenFilters/NgramTokenFilter.cs
@@ -25,6 +25,15 @@ public interface INGramTokenFilter : ITokenFilter
[DataMember(Name ="min_gram")]
[JsonFormatter(typeof(NullableStringIntFormatter))]
int? MinGram { 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; }
}
///
@@ -37,6 +46,9 @@ public NGramTokenFilter() : base("ngram") { }
///
public int? MinGram { get; set; }
+
+ ///
+ public bool? PreserveOriginal { get; set; }
}
///
@@ -48,10 +60,16 @@ public class NGramTokenFilterDescriptor
int? INGramTokenFilter.MinGram { get; set; }
- ///
+ bool? INGramTokenFilter.PreserveOriginal { get; set; }
+
+ ///
public NGramTokenFilterDescriptor MinGram(int? minGram) => Assign(minGram, (a, v) => a.MinGram = v);
- ///
+ ///
public NGramTokenFilterDescriptor MaxGram(int? maxGram) => Assign(maxGram, (a, v) => a.MaxGram = v);
+
+ ///
+ public NGramTokenFilterDescriptor 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 66437924394..74276bd3c31 100644
--- a/tests/Tests/Analysis/TokenFilters/TokenFilterTests.cs
+++ b/tests/Tests/Analysis/TokenFilters/TokenFilterTests.cs
@@ -489,6 +489,18 @@ public class NGramTests : TokenFilterAssertionBase
public override string Name => "ngram";
}
+ [SkipVersion("<7.8.0", "PreserveOriginal introduced in 7.8.0")]
+ public class NGramPreserveOriginalTests : TokenFilterAssertionBase
+ {
+ 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
{
public override FuncTokenFilters Fluent => (n, tf) => tf