-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathElasticInferrer.cs
158 lines (139 loc) · 4.25 KB
/
ElasticInferrer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Nest.Resolvers;
namespace Nest
{
public static class Infer
{
public static IndexNameMarker Index<T>()
{
return typeof(T);
}
public static IndexNameMarker Index(Type t)
{
return t;
}
public static TypeNameMarker Type<T>()
{
return typeof(T);
}
public static TypeNameMarker Type(Type t)
{
return t;
}
}
public class ElasticInferrer
{
private readonly IConnectionSettingsValues _connectionSettings;
private IdResolver IdResolver { get; set; }
private IndexNameResolver IndexNameResolver { get; set; }
private TypeNameResolver TypeNameResolver { get; set; }
private PropertyNameResolver PropertyNameResolver { get; set; }
public string DefaultIndex
{
get
{
var index = (this._connectionSettings == null) ? string.Empty : this._connectionSettings.DefaultIndex;
return index.IsNullOrEmpty() ? "_all" : index;
}
}
public ElasticInferrer(IConnectionSettingsValues connectionSettings)
{
this._connectionSettings = connectionSettings;
this.IdResolver = new IdResolver();
this.IndexNameResolver = new IndexNameResolver(this._connectionSettings);
this.TypeNameResolver = new TypeNameResolver(this._connectionSettings);
this.PropertyNameResolver = new PropertyNameResolver(this._connectionSettings);
}
public string PropertyPath(PropertyPathMarker marker)
{
if (marker.IsConditionless())
return null;
var name = !marker.Name.IsNullOrEmpty() ? marker.Name : this.PropertyNameResolver.Resolve(marker.Type);
if (marker.Boost.HasValue)
name += "^" + marker.Boost.Value.ToString(CultureInfo.InvariantCulture);
return name;
}
public string PropertyPaths(IEnumerable<PropertyPathMarker> fields)
{
if (!fields.HasAny() || fields.All(f=>f.IsConditionless())) return null;
return string.Join(",", fields.Select(PropertyPath).Where(f => !f.IsNullOrEmpty()));
}
public string PropertyPath(MemberInfo member)
{
return member == null ? null : this.PropertyNameResolver.Resolve(member);
}
public string PropertyName(PropertyNameMarker marker)
{
if (marker.IsConditionless())
return null;
return !marker.Name.IsNullOrEmpty()
? marker.Name
: marker.Expression != null
? this.PropertyNameResolver.ResolveToLastToken(marker.Expression)
: this.TypeName(marker.Type);
}
public string PropertyNames(IEnumerable<PropertyNameMarker> fields)
{
if (!fields.HasAny() || fields.All(f=>f.IsConditionless())) return null;
return string.Join(",", fields.Select(PropertyName).Where(f => !f.IsNullOrEmpty()));
}
public string PropertyName(MemberInfo member)
{
return member == null ? null : this.PropertyNameResolver.ResolveToLastToken(member);
}
public string IndexName<T>() where T : class
{
return this.IndexName(typeof(T));
}
public string IndexName(Type type)
{
return this.IndexNameResolver.GetIndexForType(type);
}
public string IndexName(IndexNameMarker index)
{
if (index == null)
return null;
return index.Resolve(this._connectionSettings);
}
public string IndexNames(params IndexNameMarker[] indices)
{
if (indices == null) return null;
return string.Join(",", indices.Select(i => this.IndexNameResolver.GetIndexForType(i)));
}
public string IndexNames(IEnumerable<IndexNameMarker> indices)
{
return !indices.HasAny() ? null : this.IndexNames(indices.ToArray());
}
public string Id<T>(T obj) where T : class
{
if (obj == null) return null;
return this.IdResolver.GetIdFor(obj);
}
public string TypeName<T>() where T : class
{
return this.TypeName(typeof(T));
}
public string TypeName(Type t)
{
return t == null ? null : this.TypeNameResolver.GetTypeNameFor(t);
}
public string TypeNames(params TypeNameMarker[] typeNames)
{
return typeNames == null
? null
: string.Join(",", typeNames.Select(t => this.TypeNameResolver.GetTypeNameFor(t)));
}
public string TypeNames(IEnumerable<TypeNameMarker> typeNames)
{
return !typeNames.HasAny() ? null : this.TypeNames(typeNames.ToArray());
}
public string TypeName(TypeNameMarker type)
{
return type == null ? null : this.TypeNameResolver.GetTypeNameFor(type);
}
}
}