-
Notifications
You must be signed in to change notification settings - Fork 1.2k
add support for the char_group tokenizer #3427
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6a0d86d
re-arch the way we do anlysis tests by making it easier for test clas…
Mpdreamz f0a0912
continueing to port analysis over to more isolated chunchs
Mpdreamz d536fd9
Ported normalizers over to new test format
Mpdreamz 537f9f9
clean up namespaces
Mpdreamz 5f6f5ef
All analysis unit/integ tests pass again
Mpdreamz 8a8e05d
abstracted analysis base classes even further to all share a base whi…
Mpdreamz 22502cf
add support for the char_group tokenizer
Mpdreamz e21d93c
Update XML comment
russcam 34d1490
Merge branch '6.4' into feature/char-group-tokenizer
russcam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
|
||
namespace Nest | ||
{ | ||
/// <summary> | ||
/// A tokenizer that breaks text into terms whenever it encounters a character which is in a defined set. It is mostly useful | ||
/// for cases where a simple custom tokenization is desired, and the overhead of use of <see cref="PatternTokenizer"/> is not acceptable. | ||
/// </summary> | ||
public interface ICharGroupTokenizer : ITokenizer | ||
{ | ||
/// <summary> | ||
/// A list containing a list of characters to tokenize the string on. Whenever a character from this list is encountered, a | ||
/// new token is started. This accepts either single characters like eg. -, or character groups: whitespace, letter, digit, | ||
/// punctuation, symbol. | ||
/// </summary> | ||
[JsonProperty("tokenize_on_chars")] | ||
IEnumerable<string> TokenizeOnCharacters { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. specialized type that takes a union of enum and |
||
} | ||
|
||
/// <inheritdoc cref="ICharGroupTokenizer"/> | ||
public class CharGroupTokenizer : TokenizerBase, ICharGroupTokenizer | ||
{ | ||
internal const string TokenizerType = "char_group"; | ||
|
||
public CharGroupTokenizer() => this.Type = TokenizerType; | ||
|
||
/// <inheritdoc cref="ICharGroupTokenizer.TokenizeOnCharacters"/> | ||
public IEnumerable<string> TokenizeOnCharacters { get; set; } | ||
} | ||
|
||
/// <inheritdoc cref="ICharGroupTokenizer"/> | ||
public class CharGroupTokenizerDescriptor | ||
: TokenizerDescriptorBase<CharGroupTokenizerDescriptor, ICharGroupTokenizer>, ICharGroupTokenizer | ||
{ | ||
protected override string Type => CharGroupTokenizer.TokenizerType; | ||
|
||
IEnumerable<string> ICharGroupTokenizer.TokenizeOnCharacters { get; set; } | ||
|
||
/// <inheritdoc cref="ICharGroupTokenizer.TokenizeOnCharacters"/> | ||
public CharGroupTokenizerDescriptor TokenizeOnCharacters(params string[] characters) => | ||
Assign(a => a.TokenizeOnCharacters = characters); | ||
|
||
/// <inheritdoc cref="ICharGroupTokenizer.TokenizeOnCharacters"/> | ||
public CharGroupTokenizerDescriptor TokenizeOnCharacters(IEnumerable<string> characters) => | ||
Assign(a => a.TokenizeOnCharacters = characters); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
src/Tests/Tests.Core/ManagedElasticsearch/Clusters/ReadOnlyCluster.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 8 additions & 3 deletions
11
src/Tests/Tests.Core/ManagedElasticsearch/Clusters/WritableCluster.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Elastic.Xunit; | ||
using Elastic.Xunit.XunitPlumbing; | ||
using FluentAssertions; | ||
using Nest; | ||
using Tests.Core.Client; | ||
using Tests.Core.ManagedElasticsearch.Clusters; | ||
using Tests.Core.Serialization; | ||
using Tests.Framework.Integration; | ||
|
||
namespace Tests.Analysis | ||
{ | ||
public interface IAnalysisAssertion | ||
{ | ||
string Name { get; } | ||
object Json { get; } | ||
} | ||
public interface IAnalysisAssertion<out TComponent, out TContainer, in TDescriptor> : IAnalysisAssertion | ||
where TContainer : class | ||
{ | ||
TComponent Initializer { get; } | ||
Func<string, TDescriptor, IPromise<TContainer>> Fluent { get; } | ||
} | ||
|
||
[IntegrationTestCluster(typeof(WritableCluster))] | ||
public abstract class AnalysisComponentTestBase<TAssertion, TComponent, TContainer, TDescriptor> | ||
: IAnalysisAssertion<TComponent, TContainer, TDescriptor> | ||
where TAssertion : AnalysisComponentTestBase<TAssertion, TComponent, TContainer, TDescriptor>, new() | ||
where TContainer : class | ||
{ | ||
private static readonly SingleEndpointUsage<ICreateIndexResponse> Usage = new SingleEndpointUsage<ICreateIndexResponse> | ||
( | ||
fluent: (s, c) => c.CreateIndex(s, AssertionSetup.FluentCall), | ||
fluentAsync: (s, c) => c.CreateIndexAsync(s, AssertionSetup.FluentCall), | ||
request: (s, c) => c.CreateIndex(AssertionSetup.InitializerCall(s)), | ||
requestAsync: (s, c) => c.CreateIndexAsync(AssertionSetup.InitializerCall(s)), | ||
valuePrefix: $"test-{typeof(TAssertion).Name.ToLowerInvariant()}" | ||
) | ||
{ | ||
OnAfterCall = c=> c.DeleteIndex(Usage.CallUniqueValues.Value) | ||
}; | ||
protected static TAssertion AssertionSetup { get; } = new TAssertion(); | ||
|
||
protected AnalysisComponentTestBase() | ||
{ | ||
this.Client = (ElasticXunitRunner.CurrentCluster as ReadOnlyCluster)?.Client ?? TestClient.DefaultInMemoryClient; | ||
Usage.KickOffOnce(this.Client, oneRandomCall: true); | ||
} | ||
|
||
private IElasticClient Client { get; } | ||
|
||
public abstract string Name { get; } | ||
public abstract TComponent Initializer { get; } | ||
public abstract Func<string, TDescriptor, IPromise<TContainer>> Fluent { get; } | ||
public abstract object Json { get; } | ||
|
||
private Func<CreateIndexDescriptor, ICreateIndexRequest> FluentCall => i =>i.Settings(s => s.Analysis(this.FluentAnalysis)); | ||
protected abstract IAnalysis FluentAnalysis(AnalysisDescriptor an); | ||
|
||
private CreateIndexRequest InitializerCall(string index) => new CreateIndexRequest(index) | ||
{ | ||
Settings = new IndexSettings { Analysis = this.InitializerAnalysis() } | ||
}; | ||
protected abstract Nest.Analysis InitializerAnalysis(); | ||
|
||
[U] public virtual async Task TestPutSettingsRequest() => await Usage.AssertOnAllResponses(r => | ||
{ | ||
var json = new { settings = new { analysis = this.AnalysisJson } }; | ||
SerializationTestHelper.Expect(json).FromRequest(r); | ||
}); | ||
|
||
protected abstract object AnalysisJson { get; } | ||
|
||
[I] public virtual async Task TestPutSettingsResponse() => await Usage.AssertOnAllResponses(r => | ||
{ | ||
r.ApiCall.HttpStatusCode.Should().Be(200); | ||
}); | ||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Elastic.Xunit.XunitPlumbing; | ||
using FluentAssertions; | ||
using Nest; | ||
using Tests.Analysis.Analyzers; | ||
using Tests.Analysis.CharFilters; | ||
using Tests.Analysis.Normalizers; | ||
using Tests.Analysis.TokenFilters; | ||
using Tests.Analysis.Tokenizers; | ||
using Tests.Core.Client; | ||
|
||
namespace Tests.Analysis | ||
{ | ||
public class AnalysisUsageTestsTests | ||
{ | ||
[U] public static void CollectionsShouldNotBeEmpty() | ||
{ | ||
var analyzers = AnalysisUsageTests.AnalyzersInitializer.Analysis.Analyzers; | ||
var charFilters = AnalysisUsageTests.CharFiltersInitializer.Analysis.CharFilters; | ||
var tokenizers = AnalysisUsageTests.TokenizersInitializer.Analysis.Tokenizers; | ||
var tokenFilters = AnalysisUsageTests.TokenFiltersInitializer.Analysis.TokenFilters; | ||
|
||
analyzers.Should().NotBeNull().And.NotBeEmpty(); | ||
charFilters.Should().NotBeNull().And.NotBeEmpty(); | ||
tokenizers.Should().NotBeNull().And.NotBeEmpty(); | ||
tokenFilters.Should().NotBeNull().And.NotBeEmpty(); | ||
} | ||
} | ||
|
||
public static class AnalysisUsageTests | ||
{ | ||
|
||
public static IndexSettings NormalizersFluent => Fluent<NormalizersDescriptor, INormalizerAssertion, INormalizers>(i => i.Fluent, (a, v) => a.Normalizers = v.Value); | ||
|
||
public static IndexSettings AnalyzersFluent => Fluent<AnalyzersDescriptor, IAnalyzerAssertion, IAnalyzers>(i => i.Fluent, (a, v) => a.Analyzers = v.Value); | ||
|
||
public static IndexSettings TokenizersFluent => Fluent<TokenizersDescriptor, ITokenizerAssertion, ITokenizers>(i => i.Fluent, (a, v) => a.Tokenizers = v.Value); | ||
|
||
public static IndexSettings TokenFiltersFluent => Fluent<TokenFiltersDescriptor, ITokenFilterAssertion, ITokenFilters>(i => i.Fluent, (a, v) => a.TokenFilters = v.Value); | ||
|
||
public static IndexSettings CharFiltersFluent => Fluent<CharFiltersDescriptor, ICharFilterAssertion, ICharFilters>(i => i.Fluent, (a, v) => a.CharFilters = v.Value); | ||
|
||
public static IndexSettings NormalizersInitializer => Init<Nest.Normalizers, INormalizerAssertion, INormalizer>(i => i.Initializer, (a, v) => a.Normalizers = v); | ||
|
||
public static IndexSettings AnalyzersInitializer => Init<Nest.Analyzers, IAnalyzerAssertion, IAnalyzer>(i => i.Initializer, (a, v) => a.Analyzers = v); | ||
|
||
public static IndexSettings TokenizersInitializer => Init<Nest.Tokenizers, ITokenizerAssertion, ITokenizer>(i => i.Initializer, (a, v) => a.Tokenizers = v); | ||
|
||
public static IndexSettings TokenFiltersInitializer => Init<Nest.TokenFilters, ITokenFilterAssertion, ITokenFilter>(i => i.Initializer, (a, v) => a.TokenFilters = v); | ||
|
||
public static IndexSettings CharFiltersInitializer => Init<Nest.CharFilters, ICharFilterAssertion, ICharFilter>(i => i.Initializer, (a, v) => a.CharFilters = v); | ||
|
||
private static IndexSettings Fluent<TContainer, TAssertion, TValue>(Func<TAssertion, Func<string, TContainer, IPromise<TValue>>> fluent, Action<Nest.Analysis, IPromise<TValue>> set) | ||
where TAssertion : IAnalysisAssertion | ||
where TContainer : IPromise<TValue>, new() | ||
where TValue : class => Wrap(an => set(an, Apply<TContainer, TAssertion>((t, a) => fluent(a)(a.Name, t)))); | ||
|
||
private static IndexSettings Init<TContainer, TAssertion, TInitializer>(Func<TAssertion, TInitializer> value, Action<Nest.Analysis, TContainer> set) | ||
where TAssertion : IAnalysisAssertion | ||
where TContainer : IDictionary<string, TInitializer>, new() => Wrap(an => set(an, Apply<TContainer, TAssertion>((t, a) => t[a.Name] = value(a)))); | ||
|
||
private static TContainer Apply<TContainer, TAssertion>(Action<TContainer, TAssertion> act) | ||
where TAssertion : IAnalysisAssertion | ||
where TContainer : new() => All<TAssertion>().Aggregate(new TContainer() , (t,a) => { act(t,a); return t; }, t=>t); | ||
|
||
private static IndexSettings Wrap(Action<Nest.Analysis> set) | ||
{ | ||
var a = new Nest.Analysis(); | ||
var s =new IndexSettings { Analysis = a }; | ||
set(a); | ||
return s; | ||
} | ||
|
||
private static List<TAssertion> All<TAssertion>() | ||
where TAssertion : IAnalysisAssertion | ||
{ | ||
var assertions = typeof(TokenizerTests).GetNestedTypes() | ||
.Union(typeof(TokenFilterTests).GetNestedTypes()) | ||
.Union(typeof(NormalizerTests).GetNestedTypes()) | ||
.Union(typeof(AnalyzerTests).GetNestedTypes()) | ||
.Union(typeof(CharFilterTests).GetNestedTypes()) | ||
.ToList(); | ||
|
||
var nestedTypes = assertions | ||
.Where(t => typeof(TAssertion).IsAssignableFrom(t) && t.IsClass) | ||
.ToList(); | ||
|
||
var types = nestedTypes | ||
.Select(t => new | ||
{ | ||
t, | ||
a = t.GetCustomAttributes(typeof(SkipVersionAttribute)).FirstOrDefault() as SkipVersionAttribute | ||
}) | ||
.Where(@t1 => @t1.a == null || [email protected](r => r.IsSatisfied(TestClient.Configuration.ElasticsearchVersion))) | ||
.Select(@t1 => (TAssertion) Activator.CreateInstance(@t1.t)); | ||
return types.ToList(); | ||
} | ||
|
||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the documentation should be