|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Globalization; |
4 | | -using System.Linq; |
5 | | - |
6 | | -namespace Nest |
7 | | -{ |
8 | | - public class ElasticInferrer |
9 | | - { |
10 | | - private readonly IConnectionSettingsValues _connectionSettings; |
11 | | - |
12 | | - private IdResolver IdResolver { get; set; } |
13 | | - private IndexNameResolver IndexNameResolver { get; set; } |
14 | | - private TypeNameResolver TypeNameResolver { get; set; } |
15 | | - private FieldResolver FieldResolver { get; set; } |
16 | | - |
17 | | - public string DefaultIndex |
18 | | - { |
19 | | - get |
20 | | - { |
21 | | - var index = (this._connectionSettings == null) ? string.Empty : this._connectionSettings.DefaultIndex; |
22 | | - return index.IsNullOrEmpty() ? "_all" : index; |
23 | | - } |
24 | | - } |
25 | | - |
26 | | - public ElasticInferrer(IConnectionSettingsValues connectionSettings) |
27 | | - { |
28 | | - this._connectionSettings = connectionSettings; |
29 | | - this.IdResolver = new IdResolver(this._connectionSettings); |
30 | | - this.IndexNameResolver = new IndexNameResolver(this._connectionSettings); |
31 | | - this.TypeNameResolver = new TypeNameResolver(this._connectionSettings); |
32 | | - this.FieldResolver = new FieldResolver(this._connectionSettings); |
33 | | - } |
34 | | - |
35 | | - public string Field(Field field) |
36 | | - { |
37 | | - if (field.IsConditionless()) |
38 | | - return null; |
39 | | - |
40 | | - var name = !field.Name.IsNullOrEmpty() |
41 | | - ? field.Name |
42 | | - : field.Expression != null |
43 | | - ? this.FieldResolver.Resolve(field.Expression) |
44 | | - : field.Property != null |
45 | | - ? this.FieldResolver.Resolve(field.Property) |
46 | | - : null; |
47 | | - |
48 | | - if (name == null) |
49 | | - throw new ArgumentException("Could not resolve a field name"); |
50 | | - |
51 | | - if (field != null && field.Boost.HasValue) |
52 | | - name += "^" + field.Boost.Value.ToString(CultureInfo.InvariantCulture); |
53 | | - |
54 | | - return name; |
55 | | - } |
56 | | - |
57 | | - public string PropertyName(PropertyName property) |
58 | | - { |
59 | | - if (property.IsConditionless()) |
60 | | - return null; |
61 | | - |
62 | | - var name = !property.Name.IsNullOrEmpty() |
63 | | - ? property.Name |
64 | | - : property.Expression != null |
65 | | - ? this.FieldResolver.Resolve(property.Expression) |
66 | | - : property.Property != null |
67 | | - ? this.FieldResolver.Resolve(property.Property) |
68 | | - : null; |
69 | | - |
70 | | - if (name == null) |
71 | | - throw new ArgumentException("Could not resolve a property name"); |
72 | | - |
73 | | - return name; |
74 | | - } |
75 | | - |
76 | | - public string IndexName<T>() where T : class |
77 | | - { |
78 | | - return this.IndexName(typeof(T)); |
79 | | - } |
80 | | - |
81 | | - public string IndexName(Type type) |
82 | | - { |
83 | | - return this.IndexNameResolver.GetIndexForType(type); |
84 | | - } |
85 | | - |
86 | | - public string IndexName(IndexName index) |
87 | | - { |
88 | | - if (index == null) |
89 | | - return null; |
90 | | - return index.Resolve(this._connectionSettings); |
91 | | - } |
92 | | - |
93 | | - public string IndexNames(params IndexName[] indices) |
94 | | - { |
95 | | - if (indices == null) return null; |
96 | | - return string.Join(",", indices.Select(i => this.IndexNameResolver.GetIndexForType(i))); |
97 | | - } |
98 | | - |
99 | | - public string IndexNames(IEnumerable<IndexName> indices) |
100 | | - { |
101 | | - return !indices.HasAny() ? null : this.IndexNames(indices.ToArray()); |
102 | | - } |
103 | | - |
104 | | - public string Id<T>(T obj) where T : class |
105 | | - { |
106 | | - if (obj == null) return null; |
107 | | - |
108 | | - return this.IdResolver.GetIdFor(obj); |
109 | | - } |
110 | | - |
111 | | - public string Id(Type objType, object obj) |
112 | | - { |
113 | | - if (obj == null) return null; |
114 | | - |
115 | | - return this.IdResolver.GetIdFor(objType, obj); |
116 | | - } |
117 | | - public string TypeName<T>() where T : class |
118 | | - { |
119 | | - return this.TypeName(typeof(T)); |
120 | | - } |
121 | | - public string TypeName(Type t) |
122 | | - { |
123 | | - return t == null ? null : this.TypeNameResolver.GetTypeNameFor(t); |
124 | | - } |
125 | | - |
126 | | - public string TypeNames(params TypeName[] typeNames) |
127 | | - { |
128 | | - return typeNames == null |
129 | | - ? null |
130 | | - : string.Join(",", typeNames.Select(t => this.TypeNameResolver.GetTypeNameFor(t))); |
131 | | - } |
132 | | - |
133 | | - public string TypeNames(IEnumerable<TypeName> typeNames) |
134 | | - { |
135 | | - return !typeNames.HasAny() ? null : this.TypeNames(typeNames.ToArray()); |
136 | | - } |
137 | | - |
138 | | - public string TypeName(TypeName type) |
139 | | - { |
140 | | - return type == null ? null : this.TypeNameResolver.GetTypeNameFor(type); |
141 | | - } |
142 | | - } |
143 | | -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +namespace Nest |
| 7 | +{ |
| 8 | + public class ElasticInferrer |
| 9 | + { |
| 10 | + private readonly IConnectionSettingsValues _connectionSettings; |
| 11 | + |
| 12 | + private IdResolver IdResolver { get; set; } |
| 13 | + private IndexNameResolver IndexNameResolver { get; set; } |
| 14 | + private TypeNameResolver TypeNameResolver { get; set; } |
| 15 | + private FieldResolver FieldResolver { get; set; } |
| 16 | + |
| 17 | + public string DefaultIndex |
| 18 | + { |
| 19 | + get |
| 20 | + { |
| 21 | + var index = (this._connectionSettings == null) ? string.Empty : this._connectionSettings.DefaultIndex; |
| 22 | + return index.IsNullOrEmpty() ? "_all" : index; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public ElasticInferrer(IConnectionSettingsValues connectionSettings) |
| 27 | + { |
| 28 | + this._connectionSettings = connectionSettings; |
| 29 | + this.IdResolver = new IdResolver(this._connectionSettings); |
| 30 | + this.IndexNameResolver = new IndexNameResolver(this._connectionSettings); |
| 31 | + this.TypeNameResolver = new TypeNameResolver(this._connectionSettings); |
| 32 | + this.FieldResolver = new FieldResolver(this._connectionSettings); |
| 33 | + } |
| 34 | + |
| 35 | + public string Field(Field field) |
| 36 | + { |
| 37 | + if (field.IsConditionless()) |
| 38 | + return null; |
| 39 | + |
| 40 | + var name = !field.Name.IsNullOrEmpty() |
| 41 | + ? field.Name |
| 42 | + : field.Expression != null |
| 43 | + ? this.FieldResolver.Resolve(field.Expression) |
| 44 | + : field.Property != null |
| 45 | + ? this.FieldResolver.Resolve(field.Property) |
| 46 | + : null; |
| 47 | + |
| 48 | + if (name == null) |
| 49 | + throw new ArgumentException("Could not resolve a field name"); |
| 50 | + |
| 51 | + if (field != null && field.Boost.HasValue) |
| 52 | + name += "^" + field.Boost.Value.ToString(CultureInfo.InvariantCulture); |
| 53 | + |
| 54 | + return name; |
| 55 | + } |
| 56 | + |
| 57 | + public string PropertyName(PropertyName property) |
| 58 | + { |
| 59 | + if (property.IsConditionless()) |
| 60 | + return null; |
| 61 | + |
| 62 | + var name = !property.Name.IsNullOrEmpty() |
| 63 | + ? property.Name |
| 64 | + : property.Expression != null |
| 65 | + ? this.FieldResolver.Resolve(property.Expression) |
| 66 | + : property.Property != null |
| 67 | + ? this.FieldResolver.Resolve(property.Property) |
| 68 | + : null; |
| 69 | + |
| 70 | + if (name == null) |
| 71 | + throw new ArgumentException("Could not resolve a property name"); |
| 72 | + |
| 73 | + return name; |
| 74 | + } |
| 75 | + |
| 76 | + public string IndexName<T>() where T : class |
| 77 | + { |
| 78 | + return this.IndexName(typeof(T)); |
| 79 | + } |
| 80 | + |
| 81 | + public string IndexName(Type type) |
| 82 | + { |
| 83 | + return this.IndexNameResolver.GetIndexForType(type); |
| 84 | + } |
| 85 | + |
| 86 | + public string IndexName(IndexName index) |
| 87 | + { |
| 88 | + if (index == null) |
| 89 | + return null; |
| 90 | + return index.Resolve(this._connectionSettings); |
| 91 | + } |
| 92 | + |
| 93 | + public string IndexNames(params IndexName[] indices) |
| 94 | + { |
| 95 | + if (indices == null) return null; |
| 96 | + return string.Join(",", indices.Select(i => this.IndexNameResolver.GetIndexForType(i))); |
| 97 | + } |
| 98 | + |
| 99 | + public string IndexNames(IEnumerable<IndexName> indices) |
| 100 | + { |
| 101 | + return !indices.HasAny() ? null : this.IndexNames(indices.ToArray()); |
| 102 | + } |
| 103 | + |
| 104 | + public string Id<T>(T obj) where T : class |
| 105 | + { |
| 106 | + if (obj == null) return null; |
| 107 | + |
| 108 | + return this.IdResolver.GetIdFor(obj); |
| 109 | + } |
| 110 | + |
| 111 | + public string Id(Type objType, object obj) |
| 112 | + { |
| 113 | + if (obj == null) return null; |
| 114 | + |
| 115 | + return this.IdResolver.GetIdFor(objType, obj); |
| 116 | + } |
| 117 | + |
| 118 | + public string TypeName<T>() where T : class |
| 119 | + { |
| 120 | + return this.TypeName(typeof(T)); |
| 121 | + } |
| 122 | + public string TypeName(Type t) |
| 123 | + { |
| 124 | + return t == null ? null : this.TypeNameResolver.GetTypeNameFor(t); |
| 125 | + } |
| 126 | + |
| 127 | + public string TypeNames(params TypeName[] typeNames) |
| 128 | + { |
| 129 | + return typeNames == null |
| 130 | + ? null |
| 131 | + : string.Join(",", typeNames.Select(t => this.TypeNameResolver.GetTypeNameFor(t))); |
| 132 | + } |
| 133 | + |
| 134 | + public string TypeNames(IEnumerable<TypeName> typeNames) |
| 135 | + { |
| 136 | + return !typeNames.HasAny() ? null : this.TypeNames(typeNames.ToArray()); |
| 137 | + } |
| 138 | + |
| 139 | + public string TypeName(TypeName type) |
| 140 | + { |
| 141 | + return type == null ? null : this.TypeNameResolver.GetTypeNameFor(type); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments