Skip to content

Commit

Permalink
Add keywords_pattern to Keyword Marker Token Filter. (#3558)
Browse files Browse the repository at this point in the history
A regular expression pattern to match against words in the text.

(cherry picked from commit 74bcbc7)
  • Loading branch information
Stuart Cam authored and russcam committed Apr 2, 2019
1 parent 59fcc78 commit 2c2a929
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
32 changes: 23 additions & 9 deletions src/Nest/Analysis/TokenFilters/KeywordMarkerTokenFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,30 @@ public interface IKeywordMarkerTokenFilter : ITokenFilter
/// <summary>
/// Set to true to lower case all words first. Defaults to false.
/// </summary>
[DataMember(Name ="ignore_case")]
[DataMember(Name = "ignore_case")]
[JsonFormatter(typeof(NullableStringBooleanFormatter))]
bool? IgnoreCase { get; set; }

/// <summary>
/// A list of words to use.
/// </summary>
[DataMember(Name ="keywords")]
[DataMember(Name = "keywords")]
IEnumerable<string> Keywords { get; set; }

/// <summary>
/// A path (either relative to config location, or absolute) to a list of words.
/// </summary>
[DataMember(Name ="keywords_path")]
[DataMember(Name = "keywords_path")]
string KeywordsPath { get; set; }

/// <summary>
/// A regular expression pattern to match against words in the text.
/// </summary>
[DataMember(Name = "keywords_pattern")]
string KeywordsPattern { get; set; }
}

/// <inheritdoc />
/// <inheritdoc cref="IKeywordMarkerTokenFilter" />
public class KeywordMarkerTokenFilter : TokenFilterBase, IKeywordMarkerTokenFilter
{
public KeywordMarkerTokenFilter() : base("keyword_marker") { }
Expand All @@ -42,9 +48,12 @@ public KeywordMarkerTokenFilter() : base("keyword_marker") { }

/// <inheritdoc />
public string KeywordsPath { get; set; }

/// <inheritdoc />
public string KeywordsPattern { get; set; }
}

/// <inheritdoc />
/// <inheritdoc cref="IKeywordMarkerTokenFilter" />
public class KeywordMarkerTokenFilterDescriptor
: TokenFilterDescriptorBase<KeywordMarkerTokenFilterDescriptor, IKeywordMarkerTokenFilter>, IKeywordMarkerTokenFilter
{
Expand All @@ -54,16 +63,21 @@ public class KeywordMarkerTokenFilterDescriptor
IEnumerable<string> IKeywordMarkerTokenFilter.Keywords { get; set; }
string IKeywordMarkerTokenFilter.KeywordsPath { get; set; }

/// <inheritdoc />
string IKeywordMarkerTokenFilter.KeywordsPattern { get; set; }

/// <inheritdoc cref="IKeywordMarkerTokenFilter.IgnoreCase" />
public KeywordMarkerTokenFilterDescriptor IgnoreCase(bool? ignoreCase = true) => Assign(a => a.IgnoreCase = ignoreCase);

/// <inheritdoc />
/// <inheritdoc cref="IKeywordMarkerTokenFilter.KeywordsPath" />
public KeywordMarkerTokenFilterDescriptor KeywordsPath(string path) => Assign(a => a.KeywordsPath = path);

/// <inheritdoc />
/// <inheritdoc cref="IKeywordMarkerTokenFilter.KeywordsPattern" />
public KeywordMarkerTokenFilterDescriptor KeywordsPattern(string pattern) => Assign(a => a.KeywordsPattern = pattern);

/// <inheritdoc cref="IKeywordMarkerTokenFilter.Keywords" />
public KeywordMarkerTokenFilterDescriptor Keywords(IEnumerable<string> keywords) => Assign(a => a.Keywords = keywords);

/// <inheritdoc />
/// <inheritdoc cref="IKeywordMarkerTokenFilter.Keywords" />
public KeywordMarkerTokenFilterDescriptor Keywords(params string[] keywords) => Assign(a => a.Keywords = keywords);
}
}
8 changes: 6 additions & 2 deletions src/Tests/Tests/Analysis/TokenFilters/TokenFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,19 @@ public class MarkerTests : TokenFilterAssertionBase<MarkerTests>
.KeywordMarker("marker", t => t
.IgnoreCase()
.Keywords("a", "b")
.KeywordsPattern(".*")
.KeywordsPath("path")
);

public override ITokenFilter Initializer => new KeywordMarkerTokenFilter { IgnoreCase = true, Keywords = new[] { "a", "b" } };
public override ITokenFilter Initializer => new KeywordMarkerTokenFilter { IgnoreCase = true, Keywords = new[] { "a", "b" }, KeywordsPath = "path", KeywordsPattern = ".*" };

public override object Json => new
{
type = "keyword_marker",
keywords = new[] { "a", "b" },
ignore_case = true
ignore_case = true,
keywords_path = "path",
keywords_pattern = ".*"
};

public override string Name => "marker";
Expand Down

0 comments on commit 2c2a929

Please sign in to comment.