Skip to content

Commit 8af0883

Browse files
authored
Add MatchPattern to dynamic templates (#3374)
Closes #3373
1 parent 2ab4db2 commit 8af0883

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed
Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,144 @@
11
using System;
2+
using System.Runtime.Serialization;
23
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Converters;
35

46
namespace Nest
57
{
8+
/// <summary>
9+
/// A Dynamic template that defines custom mappings to be applied
10+
/// to dynamically added fields based on:
11+
/// <para />- the datatype detected by Elasticsearch, with <see cref="MatchMappingType"/>.
12+
/// <para />- the name of the field, with <see cref="Match"/> and <see cref="Unmatch"/> or <see cref="MatchPattern"/>.
13+
/// <para />- the full dotted path to the field, with <see cref="PathMatch"/> and <see cref="PathUnmatch"/>.
14+
/// </summary>
615
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
716
[JsonConverter(typeof(ReadAsTypeJsonConverter<DynamicTemplate>))]
817
public interface IDynamicTemplate
918
{
19+
/// <summary>
20+
/// A pattern to match on the field name
21+
/// </summary>
1022
[JsonProperty("match")]
1123
string Match { get; set; }
1224

25+
/// <summary>
26+
/// Adjusts the behavior of <see cref="Match"/> such that it supports full
27+
/// Java regular expression matching on the field name instead of simple wildcards
28+
/// </summary>
29+
[JsonProperty("match_pattern")]
30+
MatchType? MatchPattern { get; set; }
31+
32+
/// <summary>
33+
/// A pattern to exclude fields matched by <see cref="Match"/>
34+
/// </summary>
1335
[JsonProperty("unmatch")]
1436
string Unmatch { get; set; }
1537

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

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

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

60+
/// <summary>
61+
/// The mapping to apply to matching fields
62+
/// </summary>
2563
[JsonProperty("mapping")]
2664
IProperty Mapping { get; set; }
2765
}
2866

67+
/// <inheritdoc />
2968
public class DynamicTemplate : IDynamicTemplate
3069
{
70+
/// <inheritdoc />
3171
public string Match { get; set; }
3272

73+
/// <inheritdoc />
74+
public MatchType? MatchPattern { get; set; }
75+
76+
/// <inheritdoc />
3377
public string Unmatch { get; set; }
3478

79+
/// <inheritdoc />
3580
public string MatchMappingType { get; set; }
3681

82+
/// <inheritdoc />
3783
public string PathMatch { get; set; }
3884

85+
/// <inheritdoc />
3986
public string PathUnmatch { get; set; }
4087

88+
/// <inheritdoc />
4189
public IProperty Mapping { get; set; }
4290
}
4391

92+
/// <inheritdoc cref="IDynamicTemplate"/>
4493
public class DynamicTemplateDescriptor<T> : DescriptorBase<DynamicTemplateDescriptor<T>, IDynamicTemplate>, IDynamicTemplate
4594
where T : class
4695
{
4796
string IDynamicTemplate.Match { get; set; }
97+
MatchType? IDynamicTemplate.MatchPattern { get; set; }
4898
string IDynamicTemplate.Unmatch { get; set; }
4999
string IDynamicTemplate.MatchMappingType { get; set; }
50100
string IDynamicTemplate.PathMatch { get; set; }
51101
string IDynamicTemplate.PathUnmatch { get; set; }
52102
IProperty IDynamicTemplate.Mapping { get; set; }
53103

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

107+
/// <inheritdoc cref="IDynamicTemplate.MatchPattern"/>
108+
public DynamicTemplateDescriptor<T> MatchPattern(MatchType? matchPattern) => Assign(a => a.MatchPattern = matchPattern);
109+
110+
/// <inheritdoc cref="IDynamicTemplate.Unmatch"/>
56111
public DynamicTemplateDescriptor<T> Unmatch(string unMatch) => Assign(a => a.Unmatch = unMatch);
57112

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

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

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

122+
/// <inheritdoc cref="IDynamicTemplate.Mapping"/>
64123
public DynamicTemplateDescriptor<T> Mapping(Func<SingleMappingSelector<T>, IProperty> mappingSelector) => Assign(a => a.Mapping = mappingSelector?.Invoke(new SingleMappingSelector<T>()));
65124
}
66-
}
125+
126+
/// <summary>
127+
/// Dynamic match pattern type
128+
/// </summary>
129+
[JsonConverter(typeof(StringEnumConverter))]
130+
public enum MatchType
131+
{
132+
/// <summary>
133+
/// Simple matching with wildcards
134+
/// </summary>
135+
[EnumMember(Value = "simple")]
136+
Simple,
137+
138+
/// <summary>
139+
/// Regular expression matching
140+
/// </summary>
141+
[EnumMember(Value = "regex")]
142+
Regex
143+
}
144+
}

src/Tests/Tests/Mapping/Types/SingleMappingPropertyTestsBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ protected override LazyResponses ClientUsage() => Calls(
4343
@base = new
4444
{
4545
match = "*",
46+
match_pattern = "simple",
4647
match_mapping_type = "*",
4748
mapping = this.SingleMappingJson
4849
}
@@ -65,6 +66,7 @@ protected override LazyResponses ClientUsage() => Calls(
6566
.DynamicTemplates(t => t
6667
.DynamicTemplate("base", dt => dt
6768
.Match("*")
69+
.MatchPattern(MatchType.Simple)
6870
.MatchMappingType("*")
6971
.Mapping(FluentSingleMapping)
7072
)
@@ -93,6 +95,7 @@ protected override LazyResponses ClientUsage() => Calls(
9395
{ "base", new DynamicTemplate
9496
{
9597
Match = "*",
98+
MatchPattern = MatchType.Simple,
9699
MatchMappingType = "*",
97100
Mapping = InitializerSingleMapping
98101
}

src/Tests/Tests/Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<NoWarn>$(NoWarn);xUnit1013</NoWarn>
88
</PropertyGroup>
99
<ItemGroup>
10-
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
10+
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta1-build3642" />
1111
</ItemGroup>
1212
<ItemGroup>
1313
<ProjectReference Include="..\Tests.Core\Tests.Core.csproj" />

0 commit comments

Comments
 (0)