Skip to content

Commit 7b5eb01

Browse files
committed
Refactor property and attribute mapping tests
Our property mapping tests weren't sufficient as they only tested serilization of fluent syntax and attribute types. This commit adds new tests that test both the fluent and initializer types, and run them as integration tests as well so we can validate our properties against ES. Also tackled #2322 as part of this, as I did a full pass over all of our datatype mappings. Code review changes and rename of base test classes Change DynamicMapping to Union<bool, DynamicMapping>
1 parent d77d88d commit 7b5eb01

File tree

62 files changed

+1227
-944
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1227
-944
lines changed

src/Nest/Indices/MappingManagement/PutMapping/PutMappingRequest.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal PutMappingRequest() { }
2525
/// <inheritdoc/>
2626
public IDynamicTemplateContainer DynamicTemplates { get; set; }
2727
/// <inheritdoc/>
28-
public DynamicMapping? Dynamic { get; set; }
28+
public Union<bool, DynamicMapping> Dynamic { get; set; }
2929
/// <inheritdoc/>
3030
public string Analyzer { get; set; }
3131
/// <inheritdoc/>
@@ -63,7 +63,7 @@ public PutMappingRequest() : this(typeof(T), typeof(T)) { }
6363
/// <inheritdoc/>
6464
public IDynamicTemplateContainer DynamicTemplates { get; set; }
6565
/// <inheritdoc/>
66-
public DynamicMapping? Dynamic { get; set; }
66+
public Union<bool, DynamicMapping> Dynamic { get; set; }
6767
/// <inheritdoc/>
6868
public string Analyzer { get; set; }
6969
/// <inheritdoc/>
@@ -102,7 +102,7 @@ public PutMappingDescriptor(IndexName index, TypeName type) : base(r=>r.Required
102102
string ITypeMapping.Analyzer { get; set; }
103103
string ITypeMapping.SearchAnalyzer { get; set; }
104104
IDynamicTemplateContainer ITypeMapping.DynamicTemplates { get; set; }
105-
DynamicMapping? ITypeMapping.Dynamic { get; set; }
105+
Union<bool, DynamicMapping> ITypeMapping.Dynamic { get; set; }
106106
IFieldNamesField ITypeMapping.FieldNamesField { get; set; }
107107
IIndexField ITypeMapping.IndexField { get; set; }
108108
FluentDictionary<string, object> ITypeMapping.Meta { get; set; }
@@ -126,10 +126,7 @@ public PutMappingDescriptor<T> AutoMap(IPropertyVisitor visitor = null, int maxR
126126
public PutMappingDescriptor<T> AutoMap(int maxRecursion) => AutoMap(null, maxRecursion);
127127

128128
/// <inheritdoc/>
129-
public PutMappingDescriptor<T> Dynamic(DynamicMapping dynamic) => Assign(a => a.Dynamic = dynamic);
130-
131-
/// <inheritdoc/>
132-
public PutMappingDescriptor<T> Dynamic(bool dynamic = true) => this.Dynamic(dynamic ? DynamicMapping.Allow : DynamicMapping.Ignore);
129+
public PutMappingDescriptor<T> Dynamic(Union<bool, DynamicMapping> dynamic) => Assign(a => a.Dynamic = dynamic);
133130

134131
/// <inheritdoc/>
135132
public PutMappingDescriptor<T> Parent(TypeName parentType) => Assign(a => a.ParentField = new ParentField { Type = parentType });

src/Nest/Mapping/DynamicMapping.cs

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,17 @@
11
using System;
22
using System.Runtime.Serialization;
33
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Converters;
45

56
namespace Nest
67
{
7-
/// <summary>
8-
/// Controls how elasticsearch handles dynamic mapping changes when a new document present new fields
9-
/// </summary>
10-
[JsonConverter(typeof(DynamicMappingJsonConverter))]
8+
[JsonConverter(typeof(StringEnumConverter))]
119
public enum DynamicMapping
1210
{
13-
/// <summary>
14-
/// Default value, allows unmapped fields to be cause a mapping update
15-
/// </summary>
16-
[EnumMember(Value = "allow")]
17-
Allow,
18-
/// <summary>
19-
/// New unmapped fields will be silently ignored
20-
/// </summary>
21-
[EnumMember(Value = "ignore")]
22-
Ignore,
2311
/// <summary>
2412
/// If new unmapped fields are passed, the whole document WON'T be added/updated
2513
/// </summary>
2614
[EnumMember(Value = "strict")]
2715
Strict
2816
}
29-
30-
internal class DynamicMappingJsonConverter : JsonConverter
31-
{
32-
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
33-
{
34-
var v = value as DynamicMapping?;
35-
if (!v.HasValue)
36-
{
37-
writer.WriteValue(true);
38-
return;
39-
}
40-
switch (v.Value)
41-
{
42-
case DynamicMapping.Strict:
43-
writer.WriteValue("strict");
44-
break;
45-
case DynamicMapping.Ignore:
46-
writer.WriteValue(false);
47-
break;
48-
default:
49-
writer.WriteValue(true);
50-
break;
51-
}
52-
}
53-
54-
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
55-
{
56-
var v = reader.Value;
57-
if (v == null)
58-
return null;
59-
60-
var sv = v.ToString().ToLowerInvariant();
61-
switch (sv)
62-
{
63-
case "false":
64-
return DynamicMapping.Ignore;
65-
case "strict":
66-
return DynamicMapping.Strict;
67-
default:
68-
return DynamicMapping.Allow;
69-
}
70-
}
71-
72-
public override bool CanConvert(Type objectType) => objectType == typeof(DynamicMapping?);
73-
}
7417
}

src/Nest/Mapping/TypeMapping.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public interface ITypeMapping
5454
IDynamicTemplateContainer DynamicTemplates { get; set; }
5555

5656
[JsonProperty("dynamic")]
57-
DynamicMapping? Dynamic { get; set; }
57+
Union<bool, DynamicMapping> Dynamic { get; set; }
5858

5959
[JsonProperty("properties", TypeNameHandling = TypeNameHandling.None)]
6060
IProperties Properties { get; set; }
@@ -69,7 +69,7 @@ public class TypeMapping : ITypeMapping
6969
/// <inheritdoc/>
7070
public bool? DateDetection { get; set; }
7171
/// <inheritdoc/>
72-
public DynamicMapping? Dynamic { get; set; }
72+
public Union<bool, DynamicMapping> Dynamic { get; set; }
7373
/// <inheritdoc/>
7474
public IEnumerable<string> DynamicDateFormats { get; set; }
7575
/// <inheritdoc/>
@@ -104,7 +104,7 @@ public class TypeMappingDescriptor<T> : DescriptorBase<TypeMappingDescriptor<T>,
104104
IAllField ITypeMapping.AllField { get; set; }
105105
string ITypeMapping.Analyzer { get; set; }
106106
bool? ITypeMapping.DateDetection { get; set; }
107-
DynamicMapping? ITypeMapping.Dynamic { get; set; }
107+
Union<bool, DynamicMapping> ITypeMapping.Dynamic { get; set; }
108108
IEnumerable<string> ITypeMapping.DynamicDateFormats { get; set; }
109109
IDynamicTemplateContainer ITypeMapping.DynamicTemplates { get; set; }
110110
IFieldNamesField ITypeMapping.FieldNamesField { get; set; }
@@ -131,10 +131,7 @@ public TypeMappingDescriptor<T> AutoMap(IPropertyVisitor visitor = null, int max
131131
public TypeMappingDescriptor<T> AutoMap(int maxRecursion) => AutoMap(null, maxRecursion);
132132

133133
/// <inheritdoc/>
134-
public TypeMappingDescriptor<T> Dynamic(DynamicMapping dynamic) => Assign(a => a.Dynamic = dynamic);
135-
136-
/// <inheritdoc/>
137-
public TypeMappingDescriptor<T> Dynamic(bool dynamic = true) => this.Dynamic(dynamic ? DynamicMapping.Allow : DynamicMapping.Ignore);
134+
public TypeMappingDescriptor<T> Dynamic(Union<bool, DynamicMapping> dynamic) => Assign(a => a.Dynamic = dynamic);
138135

139136
/// <inheritdoc/>
140137
public TypeMappingDescriptor<T> Parent(TypeName parentType) => Assign(a => a.ParentField = new ParentField { Type = parentType });

src/Nest/Mapping/Types/Complex/Nested/NestedProperty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ public NestedPropertyDescriptor<TParent, TChild> IncludeInParent(bool includeInP
3737
public NestedPropertyDescriptor<TParent, TChild> IncludeInRoot(bool includeInRoot = true) =>
3838
Assign(a => a.IncludeInRoot = includeInRoot);
3939
}
40-
}
40+
}

src/Nest/Mapping/Types/Complex/Object/ObjectAttribute.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ public class ObjectAttribute : ElasticsearchCorePropertyAttributeBase, IObjectPr
66
{
77
IObjectProperty Self => this;
88

9-
DynamicMapping? IObjectProperty.Dynamic { get; set; }
9+
Union<bool, DynamicMapping> IObjectProperty.Dynamic { get; set; }
1010
bool? IObjectProperty.Enabled { get; set; }
1111
bool? IObjectProperty.IncludeInAll { get; set; }
12-
string IObjectProperty.Path { get; set; }
1312
IProperties IObjectProperty.Properties { get; set; }
1413

15-
public DynamicMapping Dynamic { get { return Self.Dynamic.GetValueOrDefault(); } set { Self.Dynamic = value; } }
1614
public bool Enabled { get { return Self.Enabled.GetValueOrDefault(); } set { Self.Enabled = value; } }
1715
public bool IncludeInAll { get { return Self.IncludeInAll.GetValueOrDefault(); } set { Self.IncludeInAll = value; } }
18-
public string Path { get { return Self.Path; } set { Self.Path = value; } }
1916

2017
public ObjectAttribute() : base("object") { }
2118
protected ObjectAttribute(string typeName) : base(typeName) { }

src/Nest/Mapping/Types/Complex/Object/ObjectProperty.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ namespace Nest
88
public interface IObjectProperty : ICoreProperty
99
{
1010
[JsonProperty("dynamic")]
11-
DynamicMapping? Dynamic { get; set; }
11+
Union<bool, DynamicMapping> Dynamic { get; set; }
1212

1313
[JsonProperty("enabled")]
1414
bool? Enabled { get; set; }
1515

1616
[JsonProperty("include_in_all")]
1717
bool? IncludeInAll { get; set; }
1818

19-
[JsonProperty("path")]
20-
string Path { get; set; }
21-
2219
[JsonProperty("properties", TypeNameHandling = TypeNameHandling.None)]
2320
IProperties Properties { get; set; }
2421
}
@@ -29,10 +26,9 @@ public ObjectProperty() : base("object") { }
2926

3027
protected ObjectProperty(string type) : base(type) { }
3128

32-
public DynamicMapping? Dynamic { get; set; }
29+
public Union<bool, DynamicMapping> Dynamic { get; set; }
3330
public bool? Enabled { get; set; }
3431
public bool? IncludeInAll { get; set; }
35-
public string Path { get; set; }
3632
public IProperties Properties { get; set; }
3733
}
3834

@@ -52,10 +48,9 @@ public abstract class ObjectPropertyDescriptorBase<TDescriptor, TInterface, TPar
5248
{
5349
internal TypeName _TypeName { get; set; }
5450

55-
DynamicMapping? IObjectProperty.Dynamic { get; set; }
51+
Union<bool, DynamicMapping> IObjectProperty.Dynamic { get; set; }
5652
bool? IObjectProperty.Enabled { get; set; }
5753
bool? IObjectProperty.IncludeInAll { get; set; }
58-
string IObjectProperty.Path { get; set; }
5954
IProperties IObjectProperty.Properties { get; set; }
6055

6156
protected ObjectPropertyDescriptorBase() : this("object") { }
@@ -65,21 +60,15 @@ protected ObjectPropertyDescriptorBase(string type) : base(type)
6560
_TypeName = TypeName.Create<TChild>();
6661
}
6762

68-
public TDescriptor Dynamic(DynamicMapping dynamic) =>
63+
public TDescriptor Dynamic(Union<bool, DynamicMapping> dynamic) =>
6964
Assign(a => a.Dynamic = dynamic);
7065

71-
public TDescriptor Dynamic(bool dynamic = true) =>
72-
Dynamic(dynamic ? DynamicMapping.Allow : DynamicMapping.Ignore);
73-
7466
public TDescriptor Enabled(bool enabled = true) =>
7567
Assign(a => a.Enabled = enabled);
7668

7769
public TDescriptor IncludeInAll(bool includeInAll = true) =>
7870
Assign(a => a.IncludeInAll = includeInAll);
7971

80-
public TDescriptor Path(string path) =>
81-
Assign(a => a.Path = path);
82-
8372
public TDescriptor Properties(Func<PropertiesDescriptor<TChild>, IPromise<IProperties>> selector) =>
8473
Assign(a => a.Properties = selector?.Invoke(new PropertiesDescriptor<TChild>(a.Properties))?.Value);
8574

src/Nest/Mapping/Types/Core/Date/DateAttribute.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,16 @@ public class DateAttribute : ElasticsearchDocValuesPropertyAttributeBase, IDateP
1010
double? IDateProperty.Boost { get; set; }
1111
DateTime? IDateProperty.NullValue { get; set; }
1212
bool? IDateProperty.IncludeInAll { get; set; }
13-
int? IDateProperty.PrecisionStep { get; set; }
1413
bool? IDateProperty.IgnoreMalformed { get; set; }
1514
string IDateProperty.Format { get; set; }
16-
NumericResolutionUnit? IDateProperty.NumericResolution { get; set; }
1715
INumericFielddata IDateProperty.Fielddata { get; set; }
1816

1917
public bool Index { get { return Self.Index.GetValueOrDefault(); } set { Self.Index = value; } }
2018
public double Boost { get { return Self.Boost.GetValueOrDefault(); } set { Self.Boost = value; } }
2119
public DateTime NullValue { get { return Self.NullValue.GetValueOrDefault(); } set { Self.NullValue = value; } }
2220
public bool IncludeInAll { get { return Self.IncludeInAll.GetValueOrDefault(); } set { Self.IncludeInAll = value; } }
23-
public int PrecisionStep { get { return Self.PrecisionStep.GetValueOrDefault(); } set { Self.PrecisionStep = value; } }
2421
public bool IgnoreMalformed { get { return Self.IgnoreMalformed.GetValueOrDefault(); } set { Self.IgnoreMalformed = value; } }
2522
public string Format { get { return Self.Format; } set { Self.Format = value; } }
26-
public NumericResolutionUnit NumericResolution { get { return Self.NumericResolution.GetValueOrDefault(); } set { Self.NumericResolution = value; } }
2723

2824
public DateAttribute() : base("date") { }
2925
}

src/Nest/Mapping/Types/Core/Date/DateProperty.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@ public interface IDateProperty : IDocValuesProperty
1818
[JsonProperty("include_in_all")]
1919
bool? IncludeInAll { get; set; }
2020

21-
[JsonProperty("precision_step")]
22-
int? PrecisionStep { get; set; }
23-
2421
[JsonProperty("ignore_malformed")]
2522
bool? IgnoreMalformed { get; set; }
2623

2724
[JsonProperty("format")]
2825
string Format { get; set; }
2926

30-
[JsonProperty("numeric_resolution")]
31-
NumericResolutionUnit? NumericResolution { get; set; }
32-
3327
[JsonProperty("fielddata")]
3428
INumericFielddata Fielddata { get; set; }
3529
}
@@ -45,7 +39,6 @@ public DateProperty() : base("date") { }
4539
public int? PrecisionStep { get; set; }
4640
public bool? IgnoreMalformed { get; set; }
4741
public string Format { get; set; }
48-
public NumericResolutionUnit? NumericResolution { get; set; }
4942
public INumericFielddata Fielddata { get; set; }
5043
}
5144

@@ -57,10 +50,8 @@ public class DatePropertyDescriptor<T>
5750
double? IDateProperty.Boost { get; set; }
5851
DateTime? IDateProperty.NullValue { get; set; }
5952
bool? IDateProperty.IncludeInAll { get; set; }
60-
int? IDateProperty.PrecisionStep { get; set; }
6153
bool? IDateProperty.IgnoreMalformed { get; set; }
6254
string IDateProperty.Format { get; set; }
63-
NumericResolutionUnit? IDateProperty.NumericResolution { get; set; }
6455
INumericFielddata IDateProperty.Fielddata { get; set; }
6556

6657
public DatePropertyDescriptor() : base("date") { }
@@ -69,10 +60,8 @@ public DatePropertyDescriptor() : base("date") { }
6960
public DatePropertyDescriptor<T> Boost(double boost) => Assign(a => a.Boost = boost);
7061
public DatePropertyDescriptor<T> NullValue(DateTime nullValue) => Assign(a => a.NullValue = nullValue);
7162
public DatePropertyDescriptor<T> IncludeInAll(bool includeInAll = true) => Assign(a => a.IncludeInAll = includeInAll);
72-
public DatePropertyDescriptor<T> PrecisionStep(int precisionStep) => Assign(a => a.PrecisionStep = precisionStep);
7363
public DatePropertyDescriptor<T> IgnoreMalformed(bool ignoreMalformed = true) => Assign(a => a.IgnoreMalformed = ignoreMalformed);
7464
public DatePropertyDescriptor<T> Format(string format) => Assign(a => a.Format = format);
75-
public DatePropertyDescriptor<T> NumericResolution(NumericResolutionUnit unit) => Assign(a => a.NumericResolution = unit);
7665
public DatePropertyDescriptor<T> Fielddata(Func<NumericFielddataDescriptor, INumericFielddata> selector) =>
7766
Assign(a => a.Fielddata = selector(new NumericFielddataDescriptor()));
7867
}

src/Nest/Mapping/Types/Core/Date/NumericResolutionUnit.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Nest/Mapping/Types/Core/Keyword/KeywordAttribute.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class KeywordAttribute : ElasticsearchDocValuesPropertyAttributeBase, IKe
1818
IndexOptions? IKeywordProperty.IndexOptions { get; set; }
1919
bool? IKeywordProperty.Norms { get; set; }
2020
string IKeywordProperty.NullValue { get; set; }
21-
string IKeywordProperty.SearchAnalyzer { get; set; }
2221

2322
public double Boost { get { return Self.Boost.GetValueOrDefault(); } set { Self.Boost = value; } }
2423
public bool EagerGlobalOrdinals { get { return Self.EagerGlobalOrdinals.GetValueOrDefault(); } set { Self.EagerGlobalOrdinals = value; } }
@@ -27,7 +26,6 @@ public class KeywordAttribute : ElasticsearchDocValuesPropertyAttributeBase, IKe
2726
public bool Index { get { return Self.Index.GetValueOrDefault(); } set { Self.Index = value; } }
2827
public IndexOptions IndexOptions { get { return Self.IndexOptions.GetValueOrDefault(); } set { Self.IndexOptions = value; } }
2928
public string NullValue { get { return Self.NullValue; } set { Self.NullValue = value; } }
30-
public string SearchAnalyzer { get { return Self.SearchAnalyzer; } set { Self.SearchAnalyzer = value; } }
3129
public bool Norms { get { return Self.Norms.GetValueOrDefault(true); } set { Self.Norms = value; } }
3230

3331
public KeywordAttribute() : base("keyword") { }

0 commit comments

Comments
 (0)