@@ -10,69 +10,63 @@ namespace Nest
1010{
1111 public interface IFieldSelection < out T >
1212 {
13- /// <summary>
14- /// As of elasticsearch fields are always returned as an array. except for internal metadata values such as routing.
15- /// </summary>
16- /// <typeparam name="K">The type to return the value as, remember that if your field is a string K should be string[]</typeparam>
17- K FieldValues < K > ( string path ) ;
18-
13+ K [ ] FieldValues < K > ( string path ) ;
14+
15+ K FieldValue < K > ( string path ) ;
16+
1917 K [ ] FieldValues < TBindTo , K > ( Expression < Func < TBindTo , object > > objectPath )
2018 where TBindTo : class ;
2119
20+ K FieldValue < TBindTo , K > ( Expression < Func < TBindTo , object > > objectPath )
21+ where TBindTo : class ;
22+
2223 IDictionary < string , object > FieldValuesDictionary { get ; set ; }
2324 }
2425
2526 public class FieldSelection < T > : IFieldSelection < T >
2627 {
28+ private IFieldSelection < T > Self => this ;
29+
2730 private ElasticInferrer Infer { get ; set ; }
28- public FieldSelection ( IConnectionSettingsValues settings , IDictionary < string , object > valuesDictionary = null )
29- {
30- this . Infer = settings . Inferrer ;
31- ( ( IFieldSelection < T > ) this ) . FieldValuesDictionary = valuesDictionary ;
32- }
3331
34- [ JsonConverter ( typeof ( VerbatimDictionaryKeysJsonConverter ) ) ]
3532 IDictionary < string , object > IFieldSelection < T > . FieldValuesDictionary { get ; set ; }
3633
37- /// <summary>
38- /// As of elasticsearch fields are always returned as an array. except for internal metadata values such as routing.
39- /// </summary>
40- /// <typeparam name="K">The type to return the value as, remember that if your field is a string K should be string[]</typeparam>
41- public K FieldValues < K > ( string path )
34+ public FieldSelection ( ElasticInferrer inferrer , IDictionary < string , object > valuesDictionary = null )
4235 {
43- return this . FieldArray < K > ( path ) ;
36+ this . Infer = inferrer ;
37+ this . Self . FieldValuesDictionary = valuesDictionary ;
4438 }
4539
46- /// <summary>
47- /// As of elasticsearch fields are always returned as an array.
48- /// except for internal metadata values such as routing.
49- /// </summary>
40+ public K [ ] FieldValues < K > ( string path )
41+ {
42+ return this . FieldArray < K [ ] > ( path ) ;
43+ }
44+
45+ public K FieldValue < K > ( string path ) => FieldValues < K > ( path ) . FirstOrDefault ( ) ;
46+
47+
5048 public K [ ] FieldValues < TBindTo , K > ( Expression < Func < TBindTo , object > > objectPath )
5149 where TBindTo : class
5250 {
5351 var path = this . Infer . Field ( objectPath ) ;
5452 return this . FieldArray < K [ ] > ( path ) ;
5553 }
5654
57- /// <summary>
58- /// As of elasticsearch fields are always returned as an array.
59- /// except for internal metadata values such as routing.
60- /// </summary>
55+ public K FieldValue < TBindTo , K > ( Expression < Func < TBindTo , object > > objectPath )
56+ where TBindTo : class => FieldValues < TBindTo , K > ( objectPath ) . FirstOrDefault ( ) ;
57+
6158 public K [ ] FieldValues < K > ( Expression < Func < T , K > > objectPath )
6259 {
6360 var path = this . Infer . Field ( objectPath ) ;
6461 return this . FieldArray < K [ ] > ( path ) ;
6562 }
6663
67- /// <summary>
68- /// As of elasticsearch fields are always returned as an array. except for internal metadata values such as routing.
69- /// </summary>
70- /// <typeparam name="K">The type to return the value as, remember that if your field is a string K should be string[]</typeparam>
64+ public K FieldValue < K > ( Expression < Func < T , K > > objectPath ) => FieldValues < K > ( objectPath ) . FirstOrDefault ( ) ;
65+
7166 private K FieldArray < K > ( string path )
7267 {
73- var fieldValues = ( ( IFieldSelection < T > ) this ) . FieldValuesDictionary ;
7468 object o ;
75- if ( fieldValues != null && fieldValues . TryGetValue ( path , out o ) )
69+ if ( this . Self . FieldValuesDictionary != null && this . Self . FieldValuesDictionary . TryGetValue ( path , out o ) )
7670 {
7771 var t = typeof ( K ) ;
7872 if ( o is JArray && t . GetInterfaces ( ) . Contains ( typeof ( IEnumerable ) ) )
0 commit comments