Skip to content

Commit

Permalink
Add PreserveOriginal to EdgeNgramTokenFilter
Browse files Browse the repository at this point in the history
Relates: #4718
  • Loading branch information
russcam committed Jun 2, 2020
1 parent 720dd61 commit 5a69b69
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/Nest/Analysis/TokenFilters/EdgeNGram/EdgeNGramTokenFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public interface IEdgeNGramTokenFilter : ITokenFilter
/// </summary>
[DataMember(Name ="side")]
EdgeNGramSide? Side { get; set; }

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

/// <inheritdoc />
Expand All @@ -46,6 +53,9 @@ public EdgeNGramTokenFilter() : base("edge_ngram") { }

/// <inheritdoc />
public EdgeNGramSide? Side { get; set; }

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

/// <inheritdoc />
Expand All @@ -54,17 +64,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; }

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

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

/// <inheritdoc />
/// <inheritdoc cref="IEdgeNGramTokenFilter.Side" />
public EdgeNGramTokenFilterDescriptor Side(EdgeNGramSide? side) => Assign(side, (a, v) => a.Side = v);

/// <inheritdoc cref="IEdgeNGramTokenFilter.PreserveOriginal" />
public EdgeNGramTokenFilterDescriptor 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 @@ -98,6 +98,18 @@ public class EdgeNgramTests : TokenFilterAssertionBase<EdgeNgramTests>
public override string Name => "etf";
}

[SkipVersion("<7.8.0", "PreserveOriginal introduced in 7.8.0")]
public class EdgeNgramPreserveOriginalTests : TokenFilterAssertionBase<EdgeNgramPreserveOriginalTests>
{
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<ElisionTests>
{
public override FuncTokenFilters Fluent => (n, tf) => tf.Elision(n, t => t.Articles("a", "b", "c").ArticlesCase());
Expand Down

0 comments on commit 5a69b69

Please sign in to comment.