Skip to content

Commit 39a822c

Browse files
ejsmithgmarz
authored andcommitted
Adding support for local metadata on properties. (#2320)
* Adding support for local metadata on properties. * Fix whitespace * Move LocalMetadata into the IProperty interface Closes #2320
1 parent 1c49649 commit 39a822c

File tree

9 files changed

+159
-74
lines changed

9 files changed

+159
-74
lines changed

src/Nest/Mapping/AttributeBased/ElasticsearchPropertyAttributeBase.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Reflection;
45
using Elasticsearch.Net;
@@ -31,6 +32,8 @@ protected ElasticsearchPropertyAttributeBase(Type type)
3132
public static ElasticsearchPropertyAttributeBase From(MemberInfo memberInfo)
3233
{
3334
return memberInfo.GetCustomAttribute<ElasticsearchPropertyAttributeBase>(true);
34-
}
35+
}
36+
37+
IDictionary<string, object> IPropertyWithLocalMetadata.LocalMetadata { get; set; }
3538
}
3639
}

src/Nest/Mapping/TypeMapping.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ public TypeMappingDescriptor<T> AutoMap(IPropertyVisitor visitor = null, int max
157157
/// <inheritdoc/>
158158
public TypeMappingDescriptor<T> SizeField(Func<SizeFieldDescriptor, ISizeField> sizeFieldSelector) => Assign(a => a.SizeField = sizeFieldSelector?.Invoke(new SizeFieldDescriptor()));
159159

160-
/// <inheritdoc/>
161-
public TypeMappingDescriptor<T> SourceField(Func<SourceFieldDescriptor, ISourceField> sourceFieldSelector) => Assign(a => a.SourceField = sourceFieldSelector?.Invoke(new SourceFieldDescriptor()));
160+
/// <inheritdoc/>
161+
public TypeMappingDescriptor<T> SourceField(Func<SourceFieldDescriptor, ISourceField> sourceFieldSelector) => Assign(a => a.SourceField = sourceFieldSelector?.Invoke(new SourceFieldDescriptor()));
162162

163-
/// <inheritdoc/>
164-
public TypeMappingDescriptor<T> DisableSizeField(bool disabled = true) => Assign(a => a.SizeField = new SizeField { Enabled = !disabled });
163+
/// <inheritdoc/>
164+
public TypeMappingDescriptor<T> DisableSizeField(bool disabled = true) => Assign(a => a.SizeField = new SizeField { Enabled = !disabled });
165165

166166
/// <inheritdoc/>
167167
public TypeMappingDescriptor<T> DisableIndexField(bool disabled = true) => Assign(a => a.IndexField = new IndexField { Enabled = !disabled });

src/Nest/Mapping/Types/PropertyBase.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System.Collections.Generic;
2+
using System.Reflection;
23
using Newtonsoft.Json;
34

45
namespace Nest
@@ -11,6 +12,12 @@ public interface IProperty : IFieldMapping
1112

1213
[JsonProperty("type")]
1314
TypeName Type { get; set; }
15+
16+
/// <summary>
17+
/// Local property metadata that will NOT be stored in Elasticsearch with the mappings
18+
/// </summary>
19+
[JsonIgnore]
20+
IDictionary<string, object> LocalMetadata { get; set; }
1421
}
1522

1623
public interface IPropertyWithClrOrigin
@@ -28,5 +35,10 @@ protected PropertyBase(TypeName typeName)
2835
public PropertyName Name { get; set; }
2936
public virtual TypeName Type { get; set; }
3037
PropertyInfo IPropertyWithClrOrigin.ClrOrigin { get; set; }
38+
39+
/// <summary>
40+
/// Local property metadata that will NOT be stored in Elasticsearch with the mappings
41+
/// </summary>
42+
public IDictionary<string, object> LocalMetadata { get; set; }
3143
}
3244
}

src/Nest/Mapping/Types/PropertyDescriptorBase.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq.Expressions;
34

45
namespace Nest
@@ -17,5 +18,10 @@ public abstract class PropertyDescriptorBase<TDescriptor, TInterface, T>
1718
public TDescriptor Name(PropertyName name) => Assign(a => a.Name = name);
1819

1920
public TDescriptor Name(Expression<Func<T, object>> objectPath) => Assign(a => a.Name = objectPath);
21+
22+
/// <summary>
23+
/// Local property metadata that will NOT be stored in Elasticsearch with the mappings
24+
/// </summary>
25+
public IDictionary<string, object> LocalMetadata { get; set; }
2026
}
2127
}

src/Nest/Mapping/Visitor/IMappingVisitor.cs

+34-34
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,64 @@ namespace Nest
55
public interface IMappingVisitor
66
{
77
int Depth { get; set; }
8-
void Visit(TypeMapping mapping);
8+
void Visit(ITypeMapping mapping);
99
#pragma warning disable 618
10-
void Visit(StringProperty property);
10+
void Visit(IStringProperty property);
1111
#pragma warning restore 618
12-
void Visit(TextProperty property);
13-
void Visit(KeywordProperty property);
14-
void Visit(DateProperty property);
15-
void Visit(BooleanProperty property);
16-
void Visit(BinaryProperty property);
17-
void Visit(ObjectProperty property);
18-
void Visit(NestedProperty property);
19-
void Visit(IpProperty property);
20-
void Visit(GeoPointProperty property);
21-
void Visit(GeoShapeProperty property);
22-
void Visit(AttachmentProperty property);
23-
void Visit(NumberProperty property);
24-
void Visit(CompletionProperty property);
25-
void Visit(Murmur3HashProperty property);
26-
void Visit(TokenCountProperty property);
12+
void Visit(ITextProperty property);
13+
void Visit(IKeywordProperty property);
14+
void Visit(IDateProperty property);
15+
void Visit(IBooleanProperty property);
16+
void Visit(IBinaryProperty property);
17+
void Visit(IObjectProperty property);
18+
void Visit(INestedProperty property);
19+
void Visit(IIpProperty property);
20+
void Visit(IGeoPointProperty property);
21+
void Visit(IGeoShapeProperty property);
22+
void Visit(IAttachmentProperty property);
23+
void Visit(INumberProperty property);
24+
void Visit(ICompletionProperty property);
25+
void Visit(IMurmur3HashProperty property);
26+
void Visit(ITokenCountProperty property);
2727
}
2828

2929
public class NoopMappingVisitor : IMappingVisitor
3030
{
3131
public virtual int Depth { get; set; }
3232

33-
public virtual void Visit(TypeMapping mapping) { }
33+
public virtual void Visit(ITypeMapping mapping) { }
3434

3535
#pragma warning disable 618
36-
public virtual void Visit(StringProperty property ) { }
36+
public virtual void Visit(IStringProperty property ) { }
3737
#pragma warning restore 618
38-
public virtual void Visit(TextProperty property) { }
38+
public virtual void Visit(ITextProperty property) { }
3939

40-
public virtual void Visit(KeywordProperty property) { }
40+
public virtual void Visit(IKeywordProperty property) { }
4141

42-
public virtual void Visit(DateProperty property) { }
42+
public virtual void Visit(IDateProperty property) { }
4343

44-
public virtual void Visit(BooleanProperty property) { }
44+
public virtual void Visit(IBooleanProperty property) { }
4545

46-
public virtual void Visit(BinaryProperty property) { }
46+
public virtual void Visit(IBinaryProperty property) { }
4747

48-
public virtual void Visit(NumberProperty property) { }
48+
public virtual void Visit(INumberProperty property) { }
4949

50-
public virtual void Visit(ObjectProperty property) { }
50+
public virtual void Visit(IObjectProperty property) { }
5151

52-
public virtual void Visit(NestedProperty property) { }
52+
public virtual void Visit(INestedProperty property) { }
5353

54-
public virtual void Visit(IpProperty property) { }
54+
public virtual void Visit(IIpProperty property) { }
5555

56-
public virtual void Visit(GeoPointProperty property) { }
56+
public virtual void Visit(IGeoPointProperty property) { }
5757

58-
public virtual void Visit(GeoShapeProperty property) { }
58+
public virtual void Visit(IGeoShapeProperty property) { }
5959

60-
public virtual void Visit(AttachmentProperty property) { }
60+
public virtual void Visit(IAttachmentProperty property) { }
6161

62-
public virtual void Visit(CompletionProperty property) { }
62+
public virtual void Visit(ICompletionProperty property) { }
6363

64-
public virtual void Visit(Murmur3HashProperty property) { }
64+
public virtual void Visit(IMurmur3HashProperty property) { }
6565

66-
public virtual void Visit(TokenCountProperty property) { }
66+
public virtual void Visit(ITokenCountProperty property) { }
6767
}
6868
}

src/Nest/Mapping/Visitor/MappingWalker.cs

+17-17
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void Accept(IGetMappingResponse response)
2323
}
2424
}
2525

26-
public void Accept(TypeMapping mapping)
26+
public void Accept(ITypeMapping mapping)
2727
{
2828
if (mapping == null) return;
2929
this._visitor.Visit(mapping);
@@ -41,20 +41,20 @@ public void Accept(IProperties properties)
4141
switch (type.Name)
4242
{
4343
case "text":
44-
var t = field as TextProperty;
44+
var t = field as ITextProperty;
4545
if (t == null) continue;
4646
this._visitor.Visit(t);
4747
this.Accept(t.Fields);
4848
break;
4949
case "keyword":
50-
var k = field as KeywordProperty;
50+
var k = field as IKeywordProperty;
5151
if (k == null) continue;
5252
this._visitor.Visit(k);
5353
this.Accept(k.Fields);
5454
break;
5555
case "string":
5656
#pragma warning disable 618
57-
var s = field as StringProperty;
57+
var s = field as IStringProperty;
5858
#pragma warning restore 618
5959
if (s == null) continue;
6060
this._visitor.Visit(s);
@@ -66,83 +66,83 @@ public void Accept(IProperties properties)
6666
case "short":
6767
case "integer":
6868
case "long":
69-
var nu = field as NumberProperty;
69+
var nu = field as INumberProperty;
7070
if (nu == null) continue;
7171
this._visitor.Visit(nu);
7272
this.Accept(nu.Fields);
7373
break;
7474
case "date":
75-
var d = field as DateProperty;
75+
var d = field as IDateProperty;
7676
if (d == null) continue;
7777
this._visitor.Visit(d);
7878
this.Accept(d.Fields);
7979
break;
8080
case "boolean":
81-
var bo = field as BooleanProperty;
81+
var bo = field as IBooleanProperty;
8282
if (bo == null) continue;
8383
this._visitor.Visit(bo);
8484
this.Accept(bo.Fields);
8585
break;
8686
case "binary":
87-
var bi = field as BinaryProperty;
87+
var bi = field as IBinaryProperty;
8888
if (bi == null) continue;
8989
this._visitor.Visit(bi);
9090
this.Accept(bi.Fields);
9191
break;
9292
case "object":
93-
var o = field as ObjectProperty;
93+
var o = field as IObjectProperty;
9494
if (o == null) continue;
9595
this._visitor.Visit(o);
9696
this._visitor.Depth += 1;
9797
this.Accept(o.Properties);
9898
this._visitor.Depth -= 1;
9999
break;
100100
case "nested":
101-
var n = field as NestedProperty;
101+
var n = field as INestedProperty;
102102
if (n == null) continue;
103103
this._visitor.Visit(n);
104104
this._visitor.Depth += 1;
105105
this.Accept(n.Properties);
106106
this._visitor.Depth -= 1;
107107
break;
108108
case "ip":
109-
var i = field as IpProperty;
109+
var i = field as IIpProperty;
110110
if (i == null) continue;
111111
this._visitor.Visit(i);
112112
this.Accept(i.Fields);
113113
break;
114114
case "geo_point":
115-
var gp = field as GeoPointProperty;
115+
var gp = field as IGeoPointProperty;
116116
if (gp == null) continue;
117117
this._visitor.Visit(gp);
118118
this.Accept(gp.Fields);
119119
break;
120120
case "geo_shape":
121-
var gs = field as GeoShapeProperty;
121+
var gs = field as IGeoShapeProperty;
122122
if (gs == null) continue;
123123
this._visitor.Visit(gs);
124124
this.Accept(gs.Fields);
125125
break;
126126
case "attachment":
127-
var a = field as AttachmentProperty;
127+
var a = field as IAttachmentProperty;
128128
if (a == null) continue;
129129
this._visitor.Visit(a);
130130
this.Accept(a.Fields);
131131
break;
132132
case "completion":
133-
var c = field as CompletionProperty;
133+
var c = field as ICompletionProperty;
134134
if (c == null) continue;
135135
this._visitor.Visit(c);
136136
this.Accept(c.Fields);
137137
break;
138138
case "murmur3":
139-
var mm = field as Murmur3HashProperty;
139+
var mm = field as IMurmur3HashProperty;
140140
if (mm == null) continue;
141141
this._visitor.Visit(mm);
142142
this.Accept(mm.Fields);
143143
break;
144144
case "token_count":
145-
var tc = field as TokenCountProperty;
145+
var tc = field as ITokenCountProperty;
146146
if (tc == null) continue;
147147
this._visitor.Visit(tc);
148148
this.Accept(tc.Fields);

0 commit comments

Comments
 (0)