7
7
using static Tests . Framework . RoundTripper ;
8
8
9
9
namespace Tests . ClientConcepts . HighLevel . Mapping
10
- {
10
+ {
11
11
/** # Auto mapping properties
12
12
*
13
13
* When creating a mapping (either when creating an index or via the put mapping API),
@@ -16,14 +16,14 @@ namespace Tests.ClientConcepts.HighLevel.Mapping
16
16
* you're using attributes to map your properties, then calling AutoMap() is required
17
17
* in order for your attributes to be applied. We'll look at examples of both.
18
18
*
19
- **/
19
+ **/
20
20
public class AutoMap
21
- {
21
+ {
22
22
/**
23
23
* For these examples, we'll define two POCOS. A Company, which has a name
24
24
* and a collection of Employees. And Employee, which has various properties of
25
25
* different types, and itself has a collection of Employees.
26
- */
26
+ */
27
27
public class Company
28
28
{
29
29
public string Name { get ; set ; }
@@ -38,16 +38,16 @@ public class Employee
38
38
public DateTime Birthday { get ; set ; }
39
39
public bool IsManager { get ; set ; }
40
40
public List < Employee > Employees { get ; set ; }
41
- public TimeSpan Hours { get ; set ; }
41
+ public TimeSpan Hours { get ; set ; }
42
42
}
43
43
44
44
[ U ]
45
45
public void MappingManually ( )
46
- {
46
+ {
47
47
/** ## Manual mapping
48
48
* To create a mapping for our Company type, we can use the fluent API
49
49
* and map each property explicitly
50
- */
50
+ */
51
51
var descriptor = new CreateIndexDescriptor ( "myindex" )
52
52
. Mappings ( ms => ms
53
53
. Map < Company > ( m => m
@@ -72,13 +72,13 @@ public void MappingManually()
72
72
)
73
73
)
74
74
)
75
- ) ;
76
-
75
+ ) ;
76
+
77
77
/**
78
78
* Which is all fine and dandy, and useful for some use cases. However in most cases
79
79
* this is becomes too cumbersome of an approach, and you simply just want to map *all*
80
80
* the properties of your POCO in a single go.
81
- */
81
+ */
82
82
var expected = new
83
83
{
84
84
mappings = new
@@ -115,22 +115,22 @@ public void MappingManually()
115
115
}
116
116
} ;
117
117
118
- Expect ( expected ) . WhenSerializing ( ( ICreateIndexRequest ) descriptor ) ;
118
+ Expect ( expected ) . WhenSerializing ( ( ICreateIndexRequest ) descriptor ) ;
119
119
}
120
120
121
121
[ U ]
122
122
public void UsingAutoMap ( )
123
- {
123
+ {
124
124
/** ## Simple Automapping
125
125
* This is exactly where `AutoMap()` becomes useful. Instead of manually mapping each property,
126
126
* explicitly, we can instead call `.AutoMap()` for each of our mappings and let NEST do all the work
127
- */
127
+ */
128
128
var descriptor = new CreateIndexDescriptor ( "myindex" )
129
129
. Mappings ( ms => ms
130
130
. Map < Company > ( m => m . AutoMap ( ) )
131
131
. Map < Employee > ( m => m . AutoMap ( ) )
132
- ) ;
133
-
132
+ ) ;
133
+
134
134
/**
135
135
* Observe that NEST has inferred the Elasticsearch types based on the CLR type of our POCO properties.
136
136
* In this example,
@@ -140,7 +140,7 @@ public void UsingAutoMap()
140
140
* - Salary as an integer
141
141
* - Employees as an object
142
142
* and the remaining string properties as strings.
143
- */
143
+ */
144
144
var expected = new
145
145
{
146
146
mappings = new
@@ -168,7 +168,7 @@ public void UsingAutoMap()
168
168
} ,
169
169
hours = new
170
170
{
171
- type = "long"
171
+ type = "long"
172
172
} ,
173
173
isManager = new
174
174
{
@@ -229,30 +229,31 @@ public void UsingAutoMap()
229
229
}
230
230
} ;
231
231
232
- Expect ( expected ) . WhenSerializing ( ( ICreateIndexRequest ) descriptor ) ;
233
- }
234
-
232
+ Expect ( expected ) . WhenSerializing ( ( ICreateIndexRequest ) descriptor ) ;
233
+ }
234
+
235
235
/** ## Automapping with overrides
236
236
* In most cases, you'll want to map more than just the vanilla datatypes and also provide
237
237
* various options on your properties (analyzer, doc_values, etc...). In that case, it's
238
238
* possible to use AutoMap() in conjuction with explicitly mapped properties.
239
- */
240
- [ U ] public void OverridingAutoMappedProperties ( )
241
- {
239
+ */
240
+ [ U ]
241
+ public void OverridingAutoMappedProperties ( )
242
+ {
242
243
/**
243
244
* Here we are using AutoMap() to automatically map our company type, but then we're
244
245
* overriding our employee property and making it a `nested` type, since by default,
245
246
* AutoMap() will infer objects as `object`.
246
- */
247
+ */
247
248
var descriptor = new CreateIndexDescriptor ( "myindex" )
248
249
. Mappings ( ms => ms
249
250
. Map < Company > ( m => m
250
251
. AutoMap ( )
251
252
. Properties ( ps => ps
252
253
. Nested < Employee > ( n => n
253
254
. Name ( c => c . Employees )
254
- . Properties ( eps => eps
255
- // snip
255
+ . Properties ( eps => eps
256
+ // snip
256
257
)
257
258
)
258
259
)
@@ -274,21 +275,21 @@ [U] public void OverridingAutoMappedProperties()
274
275
employees = new
275
276
{
276
277
type = "nested" ,
277
- properties = new { }
278
+ properties = new { }
278
279
}
279
280
}
280
281
}
281
282
}
282
283
} ;
283
284
284
- Expect ( expected ) . WhenSerializing ( ( ICreateIndexRequest ) descriptor ) ;
285
- }
286
-
285
+ Expect ( expected ) . WhenSerializing ( ( ICreateIndexRequest ) descriptor ) ;
286
+ }
287
+
287
288
/** ## Automap with attributes
288
289
* It is also possible to define your mappings using attributes on your POCOS. When you
289
290
* use attributes, you MUST use AutoMap() in order for the attributes to be applied.
290
291
* Here we define the same two types but this time using attributes.
291
- */
292
+ */
292
293
[ ElasticsearchType ( Name = "company" ) ]
293
294
public class CompanyWithAttributes
294
295
{
@@ -467,13 +468,13 @@ public void UsingAutoMapWithAttributes()
467
468
} ;
468
469
469
470
Expect ( expected ) . WhenSerializing ( descriptor as ICreateIndexRequest ) ;
470
- }
471
-
471
+ }
472
+
472
473
/**
473
474
* Just as we were able to override the inferred properties in our earlier example, explicit (manual)
474
475
* mappings also take precedence over attributes. Therefore we can also override any mappings applied
475
476
* via any attributes defined on the POCO
476
- */
477
+ */
477
478
[ U ]
478
479
public void OverridingAutoMappedAttributes ( )
479
480
{
@@ -492,7 +493,7 @@ public void OverridingAutoMappedAttributes()
492
493
. TtlField ( ttl => ttl
493
494
. Enable ( )
494
495
. Default ( "10m" )
495
- )
496
+ )
496
497
. Properties ( ps => ps
497
498
. String ( s => s
498
499
. Name ( e => e . FirstName )
@@ -633,7 +634,7 @@ public void OverridingAutoMappedAttributes()
633
634
}
634
635
} ;
635
636
636
- Expect ( expected ) . WhenSerializing ( ( ICreateIndexRequest ) descriptor ) ;
637
+ Expect ( expected ) . WhenSerializing ( ( ICreateIndexRequest ) descriptor ) ;
637
638
}
638
639
639
640
[ ElasticsearchType ( Name = "company" ) ]
@@ -699,7 +700,7 @@ public void IgnoringProperties()
699
700
)
700
701
) ;
701
702
702
- settings . Expect ( expected ) . WhenSerializing ( ( ICreateIndexRequest ) descriptor ) ;
703
+ settings . Expect ( expected ) . WhenSerializing ( ( ICreateIndexRequest ) descriptor ) ;
703
704
}
704
705
705
706
/**
@@ -722,14 +723,14 @@ public class A
722
723
723
724
[ U ]
724
725
public void ControllingRecursionDepth ( )
725
- {
726
- /** By default, AutoMap() only goes as far as depth 1 */
726
+ {
727
+ /** By default, AutoMap() only goes as far as depth 1 */
727
728
var descriptor = new CreateIndexDescriptor ( "myindex" )
728
729
. Mappings ( ms => ms
729
730
. Map < A > ( m => m . AutoMap ( ) )
730
- ) ;
731
-
732
- /** Thus we do not map properties on the second occurrence of our Child property */
731
+ ) ;
732
+
733
+ /** Thus we do not map properties on the second occurrence of our Child property */
733
734
var expected = new
734
735
{
735
736
mappings = new
@@ -748,15 +749,15 @@ public void ControllingRecursionDepth()
748
749
}
749
750
} ;
750
751
751
- Expect ( expected ) . WhenSerializing ( ( ICreateIndexRequest ) descriptor ) ;
752
-
753
- /** Now lets specify a maxRecursion of 3 */
752
+ Expect ( expected ) . WhenSerializing ( ( ICreateIndexRequest ) descriptor ) ;
753
+
754
+ /** Now lets specify a maxRecursion of 3 */
754
755
var withMaxRecursionDescriptor = new CreateIndexDescriptor ( "myindex" )
755
756
. Mappings ( ms => ms
756
757
. Map < A > ( m => m . AutoMap ( 3 ) )
757
- ) ;
758
-
759
- /** AutoMap() has now mapped three levels of our Child property */
758
+ ) ;
759
+
760
+ /** AutoMap() has now mapped three levels of our Child property */
760
761
var expectedWithMaxRecursion = new
761
762
{
762
763
mappings = new
@@ -796,9 +797,68 @@ public void ControllingRecursionDepth()
796
797
}
797
798
} ;
798
799
799
- Expect ( expectedWithMaxRecursion ) . WhenSerializing ( ( ICreateIndexRequest ) withMaxRecursionDescriptor ) ;
800
+ Expect ( expectedWithMaxRecursion ) . WhenSerializing ( ( ICreateIndexRequest ) withMaxRecursionDescriptor ) ;
800
801
}
801
802
803
+ [ U ]
804
+ //hide
805
+ public void PutMappingAlsoAdheresToMaxRecursion ( )
806
+ {
807
+ var descriptor = new PutMappingDescriptor < A > ( ) . AutoMap ( ) ;
808
+
809
+ var expected = new
810
+ {
811
+ properties = new
812
+ {
813
+ child = new
814
+ {
815
+ properties = new { } ,
816
+ type = "object"
817
+ }
818
+ }
819
+ } ;
820
+
821
+ Expect ( expected ) . WhenSerializing ( ( IPutMappingRequest ) descriptor ) ;
822
+
823
+ var withMaxRecursionDescriptor = new PutMappingDescriptor < A > ( ) . AutoMap ( 3 ) ;
824
+
825
+ var expectedWithMaxRecursion = new
826
+ {
827
+ properties = new
828
+ {
829
+ child = new
830
+ {
831
+ type = "object" ,
832
+ properties = new
833
+ {
834
+ child = new
835
+ {
836
+ type = "object" ,
837
+ properties = new
838
+ {
839
+ child = new
840
+ {
841
+ type = "object" ,
842
+ properties = new
843
+ {
844
+ child = new
845
+ {
846
+ type = "object" ,
847
+ properties = new { }
848
+ }
849
+ }
850
+ }
851
+ }
852
+ }
853
+ }
854
+ }
855
+ }
856
+ } ;
857
+
858
+ Expect ( expectedWithMaxRecursion ) . WhenSerializing ( ( IPutMappingRequest ) withMaxRecursionDescriptor ) ;
859
+ }
860
+ //endhide
861
+
802
862
/** # Applying conventions through the Visitor pattern
803
863
* It is also possible to apply a transformation on all or specific properties.
804
864
*
@@ -807,16 +867,16 @@ public void ControllingRecursionDepth()
807
867
*
808
868
* For instance, lets create a custom visitor that disables doc values for numeric and boolean types.
809
869
* (Not really a good idea in practice, but let's do it anyway for the sake of a clear example.)
810
- */
870
+ */
811
871
public class DisableDocValuesPropertyVisitor : NoopPropertyVisitor
812
- {
813
- /** Override the Visit method on INumberProperty and set DocValues = false */
872
+ {
873
+ /** Override the Visit method on INumberProperty and set DocValues = false */
814
874
public override void Visit ( INumberProperty type , PropertyInfo propertyInfo , ElasticsearchPropertyAttributeBase attribute )
815
875
{
816
876
type . DocValues = false ;
817
- }
818
-
819
- /** Similarily, override the Visit method on IBooleanProperty and set DocValues = false */
877
+ }
878
+
879
+ /** Similarily, override the Visit method on IBooleanProperty and set DocValues = false */
820
880
public override void Visit ( IBooleanProperty type , PropertyInfo propertyInfo , ElasticsearchPropertyAttributeBase attribute )
821
881
{
822
882
type . DocValues = false ;
@@ -825,17 +885,17 @@ public override void Visit(IBooleanProperty type, PropertyInfo propertyInfo, Ela
825
885
826
886
[ U ]
827
887
public void UsingACustomPropertyVisitor ( )
828
- {
829
- /** Now we can pass an instance of our custom visitor to AutoMap() */
888
+ {
889
+ /** Now we can pass an instance of our custom visitor to AutoMap() */
830
890
var descriptor = new CreateIndexDescriptor ( "myindex" )
831
891
. Mappings ( ms => ms
832
892
. Map < Employee > ( m => m . AutoMap ( new DisableDocValuesPropertyVisitor ( ) ) )
833
- ) ;
834
-
893
+ ) ;
894
+
835
895
/** and anytime it maps a property as a number (INumberProperty) or boolean (IBooleanProperty)
836
896
* it will apply the transformation defined in each Visit() respectively, which in this example
837
897
* disables doc values.
838
- */
898
+ */
839
899
var expected = new
840
900
{
841
901
mappings = new
@@ -875,12 +935,12 @@ public void UsingACustomPropertyVisitor()
875
935
}
876
936
}
877
937
} ;
878
- }
879
-
938
+ }
939
+
880
940
/** You can even take the visitor approach a step further, and instead of visiting on IProperty types, visit
881
941
* directly on your POCO properties (PropertyInfo). For example, lets create a visitor that maps all CLR types
882
942
* to an Elasticsearch string (IStringProperty).
883
- */
943
+ */
884
944
public class EverythingIsAStringPropertyVisitor : NoopPropertyVisitor
885
945
{
886
946
public override IProperty Visit ( PropertyInfo propertyInfo , ElasticsearchPropertyAttributeBase attribute ) => new StringProperty ( ) ;
0 commit comments