-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathField.cs
195 lines (155 loc) · 5.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
// 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.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json.Serialization;
using Elastic.Transport;
namespace Elastic.Clients.Elasticsearch;
[JsonConverter(typeof(FieldConverter))]
[DebuggerDisplay("{" + nameof(DebugDisplay) + ",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");
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)
{
name.ThrowIfNullOrEmpty(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;
}
/// <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; }
/// <summary>
/// An expression from which the name of the field can be inferred
/// </summary>
public Expression Expression { get; }
/// <summary>
/// The name of the field
/// </summary>
public string Name { get; }
/// <summary>
/// A property from which the name of the field can be inferred
/// </summary>
public PropertyInfo Property { get; }
internal string DebugDisplay =>
$"{Expression?.ToString() ?? PropertyDebug ?? Name}{(Boost.HasValue ? "^" + Boost.Value : string.Empty)}"
+ $"{(!string.IsNullOrEmpty(Format) ? " format: " + Format : string.Empty)}"
+ $"{(_type == null ? string.Empty : " typeof: " + _type.Name)}";
private string PropertyDebug => Property == null ? null : $"PropertyInfo: {Property.Name}";
public bool Equals(Field other) => _type != null
? other != null && _type == other._type && _comparisonValue.Equals(other._comparisonValue)
: other != null && _comparisonValue.Equals(other._comparisonValue);
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 GetStringCore(elasticsearchSettings);
}
private string GetStringCore(IElasticsearchClientSettings settings)
{
if (settings is null)
{
throw new ArgumentNullException(nameof(settings),
$"Can not resolve {nameof(Field)} if no {nameof(IElasticsearchClientSettings)} is provided");
}
return settings.Inferrer.Field(this);
}
public override string ToString() => DebugDisplay;
public Fields And(Field field) => new(new[] { this, field });
public Fields And<T, TValue>(Expression<Func<T, TValue>> field, double? boost = null, string format = null)
where T : class =>
new(new[] { this, new Field(field, boost, format) });
public Fields And<T>(Expression<Func<T, object>> field, double? boost = null, string format = null)
where T : class =>
new(new[] { this, new Field(field, boost, format) });
public Fields And(string field, double? boost = null, string format = null) =>
new(new[] { this, new Field(field, boost, format) });
public Fields And(PropertyInfo property, double? boost = null, string format = null) =>
new(new[] { this, new Field(property, boost, format) });
private static string ParseFieldName(string name, out double? boost)
{
boost = null;
if (name == 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;
}
public static implicit operator Field(string name) => name.IsNullOrEmpty() ? null : new Field(name);
public static implicit operator Field(Expression expression) =>
expression == null ? null : new Field(expression);
public static implicit operator Field(PropertyInfo property) => property == null ? null : new Field(property);
public override int GetHashCode()
{
unchecked
{
var hashCode = _comparisonValue?.GetHashCode() ?? 0;
hashCode = (hashCode * 397) ^ (_type?.GetHashCode() ?? 0);
return hashCode;
}
}
public override bool Equals(object obj)
{
switch (obj)
{
case string s:
return Equals(s);
case PropertyInfo p:
return Equals(p);
case Field f:
return Equals(f);
default:
return false;
}
}
public static bool operator ==(Field x, Field y) => Equals(x, y);
public static bool operator !=(Field x, Field y) => !Equals(x, y);
}