Skip to content

Commit 1d996bf

Browse files
committed
Merge pull request #885 from Tasteful/issue/873
Use MultiFieldMapper as base for the core-mappers
2 parents cc67ac4 + e171d13 commit 1d996bf

16 files changed

+210
-135
lines changed

docs/contents/nest/indices/put-mapping.markdown

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,41 +61,37 @@ You can persist this mapping by simpling calling
6161

6262
You can also create mappings on the fly:
6363

64-
var typeMapping = new TypeMapping(Guid.NewGuid().ToString("n"));
65-
var property = new TypeMappingProperty
64+
var indexDefinition = new RootObjectMapping
6665
{
67-
Type = "multi_field"
66+
Properties = new Dictionary<PropertyNameMarker, IElasticType>(),
67+
Name = indexName
6868
};
6969

70-
var primaryField = new TypeMappingProperty
70+
var property = new StringMapping
7171
{
72-
Type = "string",
7372
Index = "not_analyzed"
7473
};
7574

76-
var analyzedField = new TypeMappingProperty
75+
var analyzedField = new StringMapping
7776
{
78-
Type = "string",
7977
Index = "analyzed"
8078
};
8179

82-
property.Fields = new Dictionary<string, TypeMappingProperty>();
83-
property.Fields.Add("name", primaryField);
8480
property.Fields.Add("name_analyzed", analyzedField);
81+
indexDefinition.Properties.Add("name", property);
82+
this.ConnectedClient.Map<object>(x => x.InitializeUsing(indexDefinition));
8583

86-
typeMapping.Properties.Add("name", property);
87-
this.ConnectedClient.Map(typeMapping);
8884

8985
## Multifield Mapping
9086
To create multifield type you can use following example.
9187

9288
var result = this._client.Map<ElasticsearchProject>(m => m
9389
.Properties(props => props
94-
.MultiField(s => s
90+
.String(s => s
9591
.Name(p => p.Name)
9692
.Path(MultiFieldMappingPath.Full)
93+
.Index(FieldIndexOption.not_analyzed)
9794
.Fields(pprops => pprops
98-
.String(ps => ps.Name(p => p.Name).Index(FieldIndexOption.not_analyzed))
9995
.String(ps => ps.Name(p => p.Name.Suffix("searchable")).Index(FieldIndexOption.analyzed))
10096
)
10197
))

src/Nest/Domain/Mapping/Descriptors/AttachmentMappingDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Nest
55
{
6-
public class AttachmentMappingDescriptor<T>
6+
public class AttachmentMappingDescriptor<T> where T : class
77
{
88
internal AttachmentMapping _Mapping = new AttachmentMapping();
99

src/Nest/Domain/Mapping/Descriptors/BooleanMappingDescriptor.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Nest
66
{
7-
public class BooleanMappingDescriptor<T>
7+
public class BooleanMappingDescriptor<T> where T : class
88
{
99
internal BooleanMapping _Mapping = new BooleanMapping();
1010

@@ -63,5 +63,26 @@ public BooleanMappingDescriptor<T> CopyTo(params Expression<Func<T, object>>[] o
6363
this._Mapping.CopyTo = objectPaths.Select(e => (PropertyPathMarker)e);
6464
return this;
6565
}
66+
67+
public BooleanMappingDescriptor<T> Path(MultiFieldMappingPath path)
68+
{
69+
this._Mapping.Path = path.Value;
70+
return this;
71+
}
72+
73+
public BooleanMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
74+
{
75+
fieldSelector.ThrowIfNull("fieldSelector");
76+
var properties = fieldSelector(new CorePropertiesDescriptor<T>());
77+
foreach (var p in properties.Properties)
78+
{
79+
var value = p.Value as IElasticCoreType;
80+
if (value == null)
81+
continue;
82+
83+
_Mapping.Fields[p.Key] = value;
84+
}
85+
return this;
86+
}
6687
}
6788
}

src/Nest/Domain/Mapping/Descriptors/DateMappingDescriptor.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Nest
55
{
6-
public class DateMappingDescriptor<T>
6+
public class DateMappingDescriptor<T> where T : class
77
{
88
internal DateMapping _Mapping = new DateMapping();
99

@@ -68,5 +68,25 @@ public DateMappingDescriptor<T> IgnoreMalformed(bool ignoreMalformed = true)
6868
return this;
6969
}
7070

71+
public DateMappingDescriptor<T> Path(MultiFieldMappingPath path)
72+
{
73+
this._Mapping.Path = path.Value;
74+
return this;
75+
}
76+
77+
public DateMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
78+
{
79+
fieldSelector.ThrowIfNull("fieldSelector");
80+
var properties = fieldSelector(new CorePropertiesDescriptor<T>());
81+
foreach (var p in properties.Properties)
82+
{
83+
var value = p.Value as IElasticCoreType;
84+
if (value == null)
85+
continue;
86+
87+
_Mapping.Fields[p.Key] = value;
88+
}
89+
return this;
90+
}
7191
}
7292
}

src/Nest/Domain/Mapping/Descriptors/GenericMappingDescriptor.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Nest
55
{
6-
public class GenericMappingDescriptor<T>
6+
public class GenericMappingDescriptor<T> where T : class
77
{
88
internal GenericMapping _Mapping = new GenericMapping();
99

@@ -75,5 +75,26 @@ public GenericMappingDescriptor<T> IncludeInAll(bool includeInAll = true)
7575
this._Mapping.IncludeInAll = includeInAll;
7676
return this;
7777
}
78+
79+
public GenericMappingDescriptor<T> Path(MultiFieldMappingPath path)
80+
{
81+
this._Mapping.Path = path.Value;
82+
return this;
83+
}
84+
85+
public GenericMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
86+
{
87+
fieldSelector.ThrowIfNull("fieldSelector");
88+
var properties = fieldSelector(new CorePropertiesDescriptor<T>());
89+
foreach (var p in properties.Properties)
90+
{
91+
var value = p.Value as IElasticCoreType;
92+
if (value == null)
93+
continue;
94+
95+
_Mapping.Fields[p.Key] = value;
96+
}
97+
return this;
98+
}
7899
}
79100
}

src/Nest/Domain/Mapping/Descriptors/NumberMappingDescriptor.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using System.Linq.Expressions;
33

44
namespace Nest
5-
{
6-
public class NumberMappingDescriptor<T>
5+
{
6+
public class NumberMappingDescriptor<T> where T : class
77
{
88
internal NumberMapping _Mapping = new NumberMapping();
99

@@ -79,7 +79,27 @@ public NumberMappingDescriptor<T> Coerce(bool coerce = true)
7979
{
8080
this._Mapping.Coerce = coerce;
8181
return this;
82+
}
83+
84+
public NumberMappingDescriptor<T> Path(MultiFieldMappingPath path)
85+
{
86+
this._Mapping.Path = path.Value;
87+
return this;
88+
}
89+
90+
public NumberMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
91+
{
92+
fieldSelector.ThrowIfNull("fieldSelector");
93+
var properties = fieldSelector(new CorePropertiesDescriptor<T>());
94+
foreach (var p in properties.Properties)
95+
{
96+
var value = p.Value as IElasticCoreType;
97+
if (value == null)
98+
continue;
99+
100+
_Mapping.Fields[p.Key] = value;
101+
}
102+
return this;
82103
}
83-
84104
}
85105
}

src/Nest/Domain/Mapping/Descriptors/StringMappingDescriptor.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
using System.Linq.Expressions;
44

55
namespace Nest
6-
{
7-
public class StringMappingDescriptor<T>
6+
{
7+
public class StringMappingDescriptor<T> where T : class
88
{
99
internal StringMapping _Mapping = new StringMapping();
1010

@@ -118,6 +118,27 @@ public StringMappingDescriptor<T> CopyTo(params Expression<Func<T, object>>[] ob
118118
{
119119
this._Mapping.CopyTo = objectPaths.Select(e => (PropertyPathMarker)e);
120120
return this;
121+
}
122+
123+
public StringMappingDescriptor<T> Path(MultiFieldMappingPath path)
124+
{
125+
this._Mapping.Path = path.Value;
126+
return this;
127+
}
128+
129+
public StringMappingDescriptor<T> Fields(Func<CorePropertiesDescriptor<T>, CorePropertiesDescriptor<T>> fieldSelector)
130+
{
131+
fieldSelector.ThrowIfNull("fieldSelector");
132+
var properties = fieldSelector(new CorePropertiesDescriptor<T>());
133+
foreach (var p in properties.Properties)
134+
{
135+
var value = p.Value as IElasticCoreType;
136+
if (value == null)
137+
continue;
138+
139+
_Mapping.Fields[p.Key] = value;
140+
}
141+
return this;
121142
}
122143
}
123144
}

src/Nest/Domain/Mapping/Types/BooleanMapping.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@
66
namespace Nest
77
{
88
[JsonObject(MemberSerialization.OptIn)]
9-
public class BooleanMapping : IElasticType, IElasticCoreType
9+
public class BooleanMapping : MultiFieldMapping, IElasticType, IElasticCoreType
1010
{
11-
12-
public PropertyNameMarker Name { get; set; }
13-
14-
[JsonProperty("type")]
15-
public virtual TypeNameMarker Type { get { return new TypeNameMarker { Name = "boolean" }; } }
16-
17-
[JsonProperty("similarity")]
18-
public string Similarity { get; set; }
11+
public BooleanMapping():base("boolean")
12+
{
13+
}
1914

2015
/// <summary>
2116
/// The name of the field that will be stored in the index. Defaults to the property/field name.
@@ -35,9 +30,6 @@ public class BooleanMapping : IElasticType, IElasticCoreType
3530
[JsonProperty("null_value")]
3631
public bool? NullValue { get; set; }
3732

38-
[JsonProperty("include_in_all")]
39-
public bool? IncludeInAll { get; set; }
40-
4133
[JsonProperty("copy_to")]
4234
public IEnumerable<PropertyPathMarker> CopyTo { get; set; }
4335
}

src/Nest/Domain/Mapping/Types/DateMapping.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66
namespace Nest
77
{
88
[JsonObject(MemberSerialization.OptIn)]
9-
public class DateMapping : IElasticType, IElasticCoreType
9+
public class DateMapping : MultiFieldMapping, IElasticType, IElasticCoreType
1010
{
11-
public PropertyNameMarker Name { get; set; }
12-
13-
[JsonProperty("type")]
14-
public virtual TypeNameMarker Type { get { return new TypeNameMarker { Name = "date" }; } }
15-
16-
[JsonProperty("similarity")]
17-
public string Similarity { get; set; }
11+
public DateMapping():base("date")
12+
{
13+
}
1814

1915
/// <summary>
2016
/// The name of the field that will be stored in the index. Defaults to the property/field name.
@@ -40,9 +36,6 @@ public class DateMapping : IElasticType, IElasticCoreType
4036
[JsonProperty("null_value")]
4137
public DateTime? NullValue { get; set; }
4238

43-
[JsonProperty("include_in_all")]
44-
public bool? IncludeInAll { get; set; }
45-
4639
[JsonProperty("ignore_malformed")]
4740
public bool? IgnoreMalformed { get; set; }
4841

src/Nest/Domain/Mapping/Types/GenericMapping.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@ namespace Nest
1010
/// in order to specify "{dynamic_template}" the type, or if you have some plugin that exposes a new type.
1111
/// </summary>
1212
[JsonObject(MemberSerialization.OptIn)]
13-
public class GenericMapping : IElasticType, IElasticCoreType
13+
public class GenericMapping : MultiFieldMapping, IElasticType, IElasticCoreType
1414
{
15-
16-
public PropertyNameMarker Name { get; set; }
17-
18-
[JsonProperty("type")]
19-
public TypeNameMarker Type { get; set; }
20-
21-
[JsonProperty("similarity")]
22-
public string Similarity { get; set; }
15+
public GenericMapping():base(null)
16+
{
17+
}
2318

2419
/// <summary>
2520
/// The name of the field that will be stored in the index. Defaults to the property/field name.
@@ -39,9 +34,6 @@ public class GenericMapping : IElasticType, IElasticCoreType
3934
[JsonProperty("boost")]
4035
public double? Boost { get; set; }
4136

42-
[JsonProperty("include_in_all")]
43-
public bool? IncludeInAll { get; set; }
44-
4537
[JsonProperty("term_vector")]
4638
public string TermVector { get; set; }
4739

src/Nest/Domain/Mapping/Types/MultiFieldMapping.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,27 @@ namespace Nest
88
[JsonObject(MemberSerialization.OptIn)]
99
public class MultiFieldMapping : IElasticType
1010
{
11+
private readonly TypeNameMarker _defaultType;
12+
13+
public MultiFieldMapping()
14+
: this("multi_field")
15+
{
16+
this.Fields = new Dictionary<PropertyNameMarker, IElasticCoreType>();
17+
}
18+
19+
protected MultiFieldMapping(TypeNameMarker defaultType)
20+
{
21+
_defaultType = defaultType;
22+
}
23+
1124
public PropertyNameMarker Name { get; set; }
1225

1326
private TypeNameMarker _typeOverride;
1427

1528
[JsonProperty("type")]
1629
public virtual TypeNameMarker Type
1730
{
18-
get { return _typeOverride ?? new TypeNameMarker { Name = "multi_field" }; }
31+
get { return _typeOverride ?? _defaultType; }
1932
set { _typeOverride = value; }
2033
}
2134

@@ -28,14 +41,8 @@ public virtual TypeNameMarker Type
2841
[JsonProperty("include_in_all")]
2942
public bool? IncludeInAll { get; set; }
3043

31-
[JsonProperty("fields"), JsonConverter(typeof(ElasticCoreTypeConverter))]
44+
[JsonProperty("fields", DefaultValueHandling = DefaultValueHandling.Ignore), JsonConverter(typeof(ElasticCoreTypeConverter))]
3245
public IDictionary<PropertyNameMarker, IElasticCoreType> Fields { get; set; }
33-
34-
35-
public MultiFieldMapping()
36-
{
37-
this.Fields = new Dictionary<PropertyNameMarker, IElasticCoreType>();
38-
}
3946
}
4047

4148
public class MultiFieldMappingPath

0 commit comments

Comments
 (0)