Skip to content

Add MatchPattern to dynamic templates #3374

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 1 commit into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
80 changes: 79 additions & 1 deletion src/Nest/Mapping/DynamicTemplate/DynamicTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,144 @@
using System;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Nest
{
/// <summary>
/// A Dynamic template that defines custom mappings to be applied
/// to dynamically added fields based on:
/// <para />- the datatype detected by Elasticsearch, with <see cref="MatchMappingType"/>.
/// <para />- the name of the field, with <see cref="Match"/> and <see cref="Unmatch"/> or <see cref="MatchPattern"/>.
/// <para />- the full dotted path to the field, with <see cref="PathMatch"/> and <see cref="PathUnmatch"/>.
/// </summary>
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[JsonConverter(typeof(ReadAsTypeJsonConverter<DynamicTemplate>))]
public interface IDynamicTemplate
{
/// <summary>
/// A pattern to match on the field name
/// </summary>
[JsonProperty("match")]
string Match { get; set; }

/// <summary>
/// Adjusts the behavior of <see cref="Match"/> such that it supports full
/// Java regular expression matching on the field name instead of simple wildcards
/// </summary>
[JsonProperty("match_pattern")]
MatchType? MatchPattern { get; set; }

/// <summary>
/// A pattern to exclude fields matched by <see cref="Match"/>
/// </summary>
[JsonProperty("unmatch")]
string Unmatch { get; set; }

/// <summary>
/// Matches on the datatype detected by dynamic field mapping,
/// in other words, the datatype that Elasticsearch thinks the field should have.
/// Only the following datatypes can be automatically detected: boolean, date, double,
/// long, object, string. It also accepts * to match all datatypes.
/// </summary>
[JsonProperty("match_mapping_type")]
string MatchMappingType { get; set; }

/// <summary>
/// A pattern to match on the field name, which may be the full dotted path
/// to the field name
/// </summary>
[JsonProperty("path_match")]
string PathMatch { get; set; }

/// <summary>
/// A pattern to exclude fields matched by <see cref="PathMatch"/>
/// </summary>
[JsonProperty("path_unmatch")]
string PathUnmatch { get; set; }

/// <summary>
/// The mapping to apply to matching fields
/// </summary>
[JsonProperty("mapping")]
IProperty Mapping { get; set; }
}

/// <inheritdoc />
public class DynamicTemplate : IDynamicTemplate
{
/// <inheritdoc />
public string Match { get; set; }

/// <inheritdoc />
public MatchType? MatchPattern { get; set; }

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

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

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

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

/// <inheritdoc />
public IProperty Mapping { get; set; }
}

/// <inheritdoc cref="IDynamicTemplate"/>
public class DynamicTemplateDescriptor<T> : DescriptorBase<DynamicTemplateDescriptor<T>, IDynamicTemplate>, IDynamicTemplate
where T : class
{
string IDynamicTemplate.Match { get; set; }
MatchType? IDynamicTemplate.MatchPattern { get; set; }
string IDynamicTemplate.Unmatch { get; set; }
string IDynamicTemplate.MatchMappingType { get; set; }
string IDynamicTemplate.PathMatch { get; set; }
string IDynamicTemplate.PathUnmatch { get; set; }
IProperty IDynamicTemplate.Mapping { get; set; }

/// <inheritdoc cref="IDynamicTemplate.Match"/>
public DynamicTemplateDescriptor<T> Match(string match) => Assign(a => a.Match = match);

/// <inheritdoc cref="IDynamicTemplate.MatchPattern"/>
public DynamicTemplateDescriptor<T> MatchPattern(MatchType? matchPattern) => Assign(a => a.MatchPattern = matchPattern);

/// <inheritdoc cref="IDynamicTemplate.Unmatch"/>
public DynamicTemplateDescriptor<T> Unmatch(string unMatch) => Assign(a => a.Unmatch = unMatch);

/// <inheritdoc cref="IDynamicTemplate.MatchMappingType"/>
public DynamicTemplateDescriptor<T> MatchMappingType(string matchMappingType) => Assign(a => a.MatchMappingType = matchMappingType);

/// <inheritdoc cref="IDynamicTemplate.PathMatch"/>
public DynamicTemplateDescriptor<T> PathMatch(string pathMatch) => Assign(a => a.PathMatch = pathMatch);

/// <inheritdoc cref="IDynamicTemplate.PathUnmatch"/>
public DynamicTemplateDescriptor<T> PathUnmatch(string pathUnmatch) => Assign(a => a.PathUnmatch = pathUnmatch);

/// <inheritdoc cref="IDynamicTemplate.Mapping"/>
public DynamicTemplateDescriptor<T> Mapping(Func<SingleMappingSelector<T>, IProperty> mappingSelector) => Assign(a => a.Mapping = mappingSelector?.Invoke(new SingleMappingSelector<T>()));
}
}

/// <summary>
/// Dynamic match pattern type
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum MatchType
{
/// <summary>
/// Simple matching with wildcards
/// </summary>
[EnumMember(Value = "simple")]
Simple,

/// <summary>
/// Regular expression matching
/// </summary>
[EnumMember(Value = "regex")]
Regex
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected override LazyResponses ClientUsage() => Calls(
@base = new
{
match = "*",
match_pattern = "simple",
match_mapping_type = "*",
mapping = this.SingleMappingJson
}
Expand All @@ -65,6 +66,7 @@ protected override LazyResponses ClientUsage() => Calls(
.DynamicTemplates(t => t
.DynamicTemplate("base", dt => dt
.Match("*")
.MatchPattern(MatchType.Simple)
.MatchMappingType("*")
.Mapping(FluentSingleMapping)
)
Expand Down Expand Up @@ -93,6 +95,7 @@ protected override LazyResponses ClientUsage() => Calls(
{ "base", new DynamicTemplate
{
Match = "*",
MatchPattern = MatchType.Simple,
MatchMappingType = "*",
Mapping = InitializerSingleMapping
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<NoWarn>$(NoWarn);xUnit1013</NoWarn>
</PropertyGroup>
<ItemGroup>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta1-build3642" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dotnet xunit 2.3.1 does not play properly with running tests from the command line - it cannot find the dotnet SDK. Reverted change back

</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Tests.Core\Tests.Core.csproj" />
Expand Down