-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathRangeQueryDescriptor.cs
227 lines (188 loc) · 5.74 KB
/
RangeQueryDescriptor.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Nest.DSL.Query.Behaviour;
using Newtonsoft.Json;
using System.Linq.Expressions;
namespace Nest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public interface IRangeQuery : IFieldNameQuery
{
[JsonProperty("gte")]
string GreaterThanOrEqualTo { get; set; }
[JsonProperty("lte")]
string LowerThanOrEqualTo { get; set; }
[JsonProperty("gt")]
string GreaterThan { get; set; }
[JsonProperty("lt")]
string LowerThan { get; set; }
[JsonProperty(PropertyName = "boost")]
double? Boost { get; set; }
[JsonProperty(PropertyName = "_cache")]
[Obsolete("scheduled to be removed in 2.o copy paste errror from filters")]
bool? Cache { get; set; }
[JsonProperty("time_zone")]
string TimeZone { get; set; }
[JsonProperty("format")]
string Format { get; set; }
PropertyPathMarker Field { get; set; }
}
public class RangeQuery : PlainQuery, IRangeQuery
{
protected override void WrapInContainer(IQueryContainer container)
{
container.Range = this;
}
bool IQuery.IsConditionless { get { return false; } }
PropertyPathMarker IFieldNameQuery.GetFieldName()
{
return this.Field;
}
void IFieldNameQuery.SetFieldName(string fieldName)
{
this.Field = fieldName;
}
public string GreaterThanOrEqualTo { get; set; }
public string LowerThanOrEqualTo { get; set; }
public string GreaterThan { get; set; }
public string LowerThan { get; set; }
public double? Boost { get; set; }
public bool? Cache { get; set; }
public string Name { get; set; }
public string TimeZone { get; set; }
public string Format { get; set; }
public PropertyPathMarker Field { get; set; }
}
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class RangeQueryDescriptor<T> : IRangeQuery where T : class
{
private IRangeQuery Self { get { return this; } }
string IRangeQuery.GreaterThanOrEqualTo { get; set; }
string IRangeQuery.LowerThanOrEqualTo { get; set; }
string IRangeQuery.GreaterThan { get; set; }
string IRangeQuery.LowerThan { get; set; }
double? IRangeQuery.Boost { get; set; }
bool? IRangeQuery.Cache { get; set; }
string IRangeQuery.TimeZone { get; set; }
string IRangeQuery.Format { get; set; }
PropertyPathMarker IRangeQuery.Field { get; set; }
bool IQuery.IsConditionless
{
get
{
return this.Self.Field.IsConditionless()
|| (
this.Self.GreaterThanOrEqualTo == null
&& this.Self.LowerThanOrEqualTo == null
&& this.Self.GreaterThan == null
&& this.Self.LowerThan == null
);
}
}
string IQuery.Name { get; set; }
void IFieldNameQuery.SetFieldName(string fieldName)
{
this.Self.Field = fieldName;
}
PropertyPathMarker IFieldNameQuery.GetFieldName()
{
return this.Self.Field;
}
public RangeQueryDescriptor<T> Name(string name)
{
Self.Name = name;
return this;
}
public RangeQueryDescriptor<T> OnField(string field)
{
this.Self.Field = field;
return this;
}
public RangeQueryDescriptor<T> OnField(Expression<Func<T, object>> objectPath)
{
this.Self.Field = objectPath;
return this;
}
/// <summary>
/// Boosts the range query by the specified boost factor
/// </summary>
/// <param name="boost">Boost factor</param>
public RangeQueryDescriptor<T> Boost(double boost)
{
this.Self.Boost = boost;
return this;
}
public RangeQueryDescriptor<T> Greater(double? from)
{
this.Self.GreaterThan = from.HasValue ? from.Value.ToString(CultureInfo.InvariantCulture) : null;
return this;
}
public RangeQueryDescriptor<T> GreaterOrEquals(double? from)
{
this.Self.GreaterThanOrEqualTo = from.HasValue ? from.Value.ToString(CultureInfo.InvariantCulture) : null;
return this;
}
public RangeQueryDescriptor<T> Lower(double? to)
{
this.Self.LowerThan = to.HasValue ? to.Value.ToString(CultureInfo.InvariantCulture) : null;
return this;
}
public RangeQueryDescriptor<T> LowerOrEquals(double? to)
{
this.Self.LowerThanOrEqualTo = to.HasValue ? to.Value.ToString(CultureInfo.InvariantCulture) : null;
return this;
}
public RangeQueryDescriptor<T> Greater(string from)
{
this.Self.GreaterThan = from;
return this;
}
public RangeQueryDescriptor<T> GreaterOrEquals(string from)
{
this.Self.GreaterThanOrEqualTo = from;
return this;
}
public RangeQueryDescriptor<T> Lower(string to)
{
this.Self.LowerThan = to;
return this;
}
public RangeQueryDescriptor<T> LowerOrEquals(string to)
{
this.Self.LowerThanOrEqualTo = to;
return this;
}
public RangeQueryDescriptor<T> Greater(DateTime? from, string format = "yyyy-MM-dd'T'HH:mm:ss.fff")
{
this.Self.GreaterThan = from.HasValue ? from.Value.ToString(format, CultureInfo.InvariantCulture) : null;
return this;
}
public RangeQueryDescriptor<T> GreaterOrEquals(DateTime? from, string format = "yyyy-MM-dd'T'HH:mm:ss.fff")
{
this.Self.GreaterThanOrEqualTo = from.HasValue ? from.Value.ToString(format, CultureInfo.InvariantCulture) : null;
return this;
}
public RangeQueryDescriptor<T> Lower(DateTime? to, string format = "yyyy-MM-dd'T'HH:mm:ss.fff")
{
this.Self.LowerThan = to.HasValue ? to.Value.ToString(format, CultureInfo.InvariantCulture) : null;
return this;
}
public RangeQueryDescriptor<T> LowerOrEquals(DateTime? to, string format = "yyyy-MM-dd'T'HH:mm:ss.fff")
{
this.Self.LowerThanOrEqualTo = to.HasValue ? to.Value.ToString(format, CultureInfo.InvariantCulture) : null;
return this;
}
public RangeQueryDescriptor<T> TimeZone(string timeZone)
{
this.Self.TimeZone = timeZone;
return this;
}
public RangeQueryDescriptor<T> Format(string format)
{
this.Self.Format = format;
return this;
}
}
}