-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathField.cs
262 lines (195 loc) · 6.83 KB
/
Field.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json.Serialization;
using Elastic.Transport;
#if ELASTICSEARCH_SERVERLESS
namespace Elastic.Clients.Elasticsearch.Serverless;
#else
namespace Elastic.Clients.Elasticsearch;
#endif
[JsonConverter(typeof(FieldConverter))]
[DebuggerDisplay($"{nameof(DebuggerDisplay)},nq")]
public sealed class Field :
IEquatable<Field>,
IUrlParameter
{
private readonly object _comparisonValue;
private readonly Type? _type;
// Pseudo and metadata fields
public static Field IdField = new("_id");
public static Field ScoreField = new("_score");
public static Field KeyField = new("_key");
public static Field CountField = new("_count");
/// <summary>
/// The name of the field
/// </summary>
public string? Name { get; }
/// <summary>
/// An expression from which the name of the field can be inferred
/// </summary>
public Expression? Expression { get; }
/// <summary>
/// A property from which the name of the field can be inferred
/// </summary>
public PropertyInfo? Property { get; }
/// <summary>
/// A boost to apply to the field
/// </summary>
public double? Boost { get; set; }
/// <summary>
/// A format to apply to the field.
/// </summary>
/// <remarks>
/// Can be used only for Doc Value Fields Elasticsearch 6.4.0+
/// </remarks>
public string? Format { get; set; }
internal bool CachableExpression { get; }
#region Constructors
public Field(string name) : this(name, null, null)
{
}
public Field(string name, double boost) : this(name, boost, null)
{
}
public Field(string name, string format) : this(name, null, format)
{
}
public Field(string name, double? boost, string? format)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException($"{name} can not be null or empty.", nameof(name));
Name = ParseFieldName(name, out var b);
Boost = b ?? boost;
Format = format;
_comparisonValue = Name;
}
public Field(Expression expression, double? boost = null, string? format = null)
{
Expression = expression ?? throw new ArgumentNullException(nameof(expression));
Boost = boost;
Format = format;
_comparisonValue = expression.ComparisonValueFromExpression(out var type, out var cachable);
_type = type;
CachableExpression = cachable;
}
public Field(PropertyInfo property, double? boost = null, string? format = null)
{
Property = property ?? throw new ArgumentNullException(nameof(property));
Boost = boost;
Format = format;
_comparisonValue = property;
_type = property.DeclaringType;
}
#endregion Constructors
#region Factory Methods
public static Field? FromString(string? name) => string.IsNullOrEmpty(name) ? null : new Field(name);
public static Field? FromExpression(Expression? expression) => expression is null ? null : new Field(expression);
public static Field? FromProperty(PropertyInfo? property) => property is null ? null : new Field(property);
#endregion Factory Methods
#region Conversion Operators
public static implicit operator Field?(string? name) => FromString(name);
public static implicit operator Field?(Expression? expression) => FromExpression(expression);
public static implicit operator Field?(PropertyInfo? property) => FromProperty(property);
#endregion Conversion Operators
#region Combinator Methods
public Fields And(Field field)
{
if (field is null)
throw new ArgumentNullException(nameof(field));
return new([this, field]);
}
public Fields And<T, TValue>(Expression<Func<T, TValue>> expression, double? boost = null, string? format = null)
{
if (expression is null)
throw new ArgumentNullException(nameof(expression));
return new([this, new Field(expression, boost, format)]);
}
public Fields And<T>(Expression<Func<T, object>> expression, double? boost = null, string? format = null)
{
if (expression is null)
throw new ArgumentNullException(nameof(expression));
return new([this, new Field(expression, boost, format)]);
}
public Fields And(string field, double? boost = null, string? format = null)
{
if (field is null)
throw new ArgumentNullException(nameof(field));
return new([this, new Field(field, boost, format)]);
}
public Fields And(PropertyInfo property, double? boost = null, string? format = null)
{
if (property is null)
throw new ArgumentNullException(nameof(property));
return new([this, new Field(property, boost, format)]);
}
#endregion Combinator Methods
#region Equality
public static bool operator ==(Field? a, Field? b) => Equals(a, b);
public static bool operator !=(Field? a, Field? b) => !Equals(a, b);
public bool Equals(Field? other) =>
other switch
{
not null when _type is not null => (_type == other._type) && _comparisonValue.Equals(other._comparisonValue),
not null when _type is null => _comparisonValue.Equals(other._comparisonValue),
_ => false
};
public override bool Equals(object? obj) =>
obj switch
{
Field f => Equals(f),
string s => Equals(s),
Expression e => Equals(e),
PropertyInfo p => Equals(p),
_ => false
};
public override int GetHashCode()
{
unchecked
{
var hashCode = _comparisonValue?.GetHashCode() ?? 0;
hashCode = (hashCode * 397) ^ (_type?.GetHashCode() ?? 0);
return hashCode;
}
}
#endregion Equality
#region IUrlParameter
string IUrlParameter.GetString(ITransportConfiguration settings)
{
if (settings is not IElasticsearchClientSettings elasticsearchSettings)
{
throw new ArgumentNullException(nameof(settings),
$"Can not resolve {nameof(Field)} if no {nameof(IElasticsearchClientSettings)} is provided");
}
return elasticsearchSettings.Inferrer.Field(this);
}
#endregion IUrlParameter
#region Debugging
public override string ToString() =>
$"{Expression?.ToString() ?? PropertyDebug ?? Name}{(Boost.HasValue ? "^" + Boost.Value : string.Empty)}" +
$"{(!string.IsNullOrEmpty(Format) ? " format: " + Format : string.Empty)}" +
$"{(_type == null ? string.Empty : " typeof: " + _type.Name)}";
internal string DebuggerDisplay => ToString();
private string? PropertyDebug => Property is null ? null : $"PropertyInfo: {Property.Name}";
#endregion Debugging
[return: NotNullIfNotNull(nameof(name))]
private static string? ParseFieldName(string? name, out double? boost)
{
boost = null;
if (name is null)
return null;
var caretIndex = name.IndexOf('^');
if (caretIndex == -1)
return name;
var parts = name.Split(new[] { '^' }, 2, StringSplitOptions.RemoveEmptyEntries);
name = parts[0];
boost = double.Parse(parts[1], CultureInfo.InvariantCulture);
return name;
}
}