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 EdgeNgramTokenFilter #4727

Merged
merged 2 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 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>.
russcam marked this conversation as resolved.
Show resolved Hide resolved
/// </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