-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathSortFieldDescriptor.cs
180 lines (145 loc) · 4.01 KB
/
SortFieldDescriptor.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Linq.Expressions;
using Newtonsoft.Json;
using Nest.Resolvers;
using Newtonsoft.Json.Converters;
namespace Nest
{
[JsonConverter(typeof(StringEnumConverter))]
public enum SortOrder
{
[EnumMember(Value = "asc")]
Ascending,
[EnumMember(Value = "desc")]
Descending
}
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public interface ISort
{
[JsonProperty("missing")]
string Missing { get; set; }
[JsonProperty("order")]
SortOrder? Order { get; set; }
}
public interface IFieldSort : ISort
{
[JsonProperty("mode")]
ScoreMode? Mode { get; set; }
[JsonProperty("nested_filter")]
FilterContainer NestedFilter { get; set; }
[JsonProperty("nested_path")]
PropertyPathMarker NestedPath { get; set; }
[JsonProperty("ignore_unmapped")]
bool? IgnoreUnmappedFields { get; set; }
}
public class Sort : IFieldSort
{
//public PropertyPathMarker Field { get; set; }
public string Missing { get; set; }
public SortOrder? Order { get; set; }
public ScoreMode? Mode { get; set; }
public FilterContainer NestedFilter { get; set; }
public PropertyPathMarker NestedPath { get; set; }
public bool? IgnoreUnmappedFields { get; set; }
}
public class SortFieldDescriptor<T> : IFieldSort where T : class
{
private IFieldSort Self { get { return this; } }
internal PropertyPathMarker Field { get; set; }
string ISort.Missing { get; set; }
SortOrder? ISort.Order { get; set; }
ScoreMode? IFieldSort.Mode { get; set; }
FilterContainer IFieldSort.NestedFilter { get; set; }
PropertyPathMarker IFieldSort.NestedPath { get; set; }
bool? IFieldSort.IgnoreUnmappedFields { get; set; }
public virtual SortFieldDescriptor<T> OnField(string field)
{
this.Field = field;
return this;
}
public virtual SortFieldDescriptor<T> OnField(Expression<Func<T, object>> objectPath)
{
this.Field = objectPath;
return this;
}
public virtual SortFieldDescriptor<T> MissingLast()
{
Self.Missing = "_last";
return this;
}
public virtual SortFieldDescriptor<T> MissingFirst()
{
Self.Missing = "_first";
return this;
}
public virtual SortFieldDescriptor<T> MissingValue(string value)
{
Self.Missing = value;
return this;
}
public virtual SortFieldDescriptor<T> IgnoreUnmappedFields(bool ignore = true)
{
Self.IgnoreUnmappedFields = ignore;
return this;
}
public virtual SortFieldDescriptor<T> Ascending()
{
Self.Order = SortOrder.Ascending;
return this;
}
public virtual SortFieldDescriptor<T> Descending()
{
Self.Order = SortOrder.Descending;
return this;
}
public virtual SortFieldDescriptor<T> NestedMin()
{
Self.Mode = ScoreMode.min;
return this;
}
public virtual SortFieldDescriptor<T> NestedMax()
{
Self.Mode = ScoreMode.max;
return this;
}
public virtual SortFieldDescriptor<T> NestedSum()
{
Self.Mode = ScoreMode.sum;
return this;
}
public virtual SortFieldDescriptor<T> NestedAvg()
{
Self.Mode = ScoreMode.avg;
return this;
}
public virtual SortFieldDescriptor<T> NestedFilter(Func<FilterDescriptor<T>, FilterContainer> filterSelector)
{
filterSelector.ThrowIfNull("filterSelector");
var filter = new FilterDescriptor<T>();
Self.NestedFilter = filterSelector(filter);
return this;
}
public virtual SortFieldDescriptor<T> NestedPath(string path)
{
Self.NestedPath = path;
return this;
}
public SortFieldDescriptor<T> NestedPath(Expression<Func<T, object>> objectPath)
{
Self.NestedPath = objectPath;
return this;
}
/// <summary>
/// Pass true to sort ascending false to sort descending
/// </summary>
public virtual SortFieldDescriptor<T> ToggleSort(bool ascending)
{
Self.Order = ascending ? SortOrder.Ascending : SortOrder.Descending;
return this;
}
}
}