Repository navigation
Expand file tree
/
Copy pathAggregationRequest.cs
More file actions
207 lines (173 loc) · 4.85 KB
/
Copy pathAggregationRequest.cs
File metadata and controls
207 lines (173 loc) · 4.85 KB
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
using NRedisStack.Search.Aggregation;
using NRedisStack.Search.Literals;
namespace NRedisStack.Search;
public class AggregationRequest : IDialectAwareParam
{
private readonly List<object> args = []; // Check if Readonly
private bool isWithCursor = false;
public int? dialect { get; private set; } = null;
public AggregationRequest(string query, int? defaultDialect = null)
{
dialect = defaultDialect;
args.Add(query);
}
public AggregationRequest() : this("*") { }
public AggregationRequest Verbatim()
{
args.Add(SearchArgs.VERBATIM);
return this;
}
/// <summary>
/// Exposes the full-text score values to the aggregation pipeline via the <c>ADDSCORES</c> option.
/// The score of each result is then available as the <c>@__score</c> field, e.g. for use in
/// <c>SORTBY</c> or <c>APPLY</c>.
/// </summary>
public AggregationRequest AddScores()
{
args.Add(SearchArgs.ADDSCORES);
return this;
}
public AggregationRequest Load(params FieldName[] fields)
{
if (fields.Length > 0)
{
args.Add(SearchArgs.LOAD);
int loadCountIndex = args.Count;
int loadCount = fields.Sum(fn => fn.AddCommandArguments(args));
args.Insert(loadCountIndex, loadCount);
}
return this;
}
public AggregationRequest LoadAll()
{
args.Add(SearchArgs.LOAD);
args.Add("*");
return this;
}
public AggregationRequest Timeout(long timeout)
{
args.Add(SearchArgs.TIMEOUT);
args.Add(timeout);
return this;
}
public AggregationRequest GroupBy(string field, params Reducer[] reducers)
{
return GroupBy([field], reducers);
}
public AggregationRequest GroupBy(IList<string> fields, IList<Reducer> reducers)
{
Group g = new(fields);
foreach (Reducer r in reducers)
{
g.Reduce(r);
}
GroupBy(g);
return this;
}
public AggregationRequest GroupBy(Group group)
{
args.Add(SearchArgs.GROUPBY);
group.SerializeRedisArgs(args);
return this;
}
public AggregationRequest SortBy(string property) => SortBy(SortedField.Asc(property));
public AggregationRequest SortBy(params SortedField[] fields) => SortBy(-1, fields);
public AggregationRequest SortBy(int max, params SortedField[] fields)
{
args.Add(SearchArgs.SORTBY);
args.Add(fields.Length * 2);
foreach (SortedField field in fields)
{
args.Add(field.FieldName);
args.Add(field.Order.ToString());
}
if (max > 0)
{
args.Add(SearchArgs.MAX);
args.Add(max);
}
return this;
}
public AggregationRequest Apply(string projection, string alias)
{
args.Add(SearchArgs.APPLY);
args.Add(projection);
args.Add(SearchArgs.AS);
args.Add(alias);
return this;
}
public AggregationRequest Limit(int count) => Limit(0, count);
public AggregationRequest Limit(int offset, int count)
{
new Limit(offset, count).SerializeRedisArgs(args);
return this;
}
public AggregationRequest Filter(string filter)
{
args.Add(SearchArgs.FILTER);
args.Add(filter);
return this;
}
public AggregationRequest Cursor(int? count = null, long? maxIdle = null)
{
isWithCursor = true;
args.Add(SearchArgs.WITHCURSOR);
if (count != null)
{
Count = count;
args.Add(SearchArgs.COUNT);
args.Add(count);
}
if (maxIdle != null && maxIdle < long.MaxValue && maxIdle >= 0)
{
args.Add(SearchArgs.MAXIDLE);
args.Add(maxIdle);
}
return this;
}
internal int? Count { get; set; }
public AggregationRequest Params(Dictionary<string, object> nameValue)
{
if (nameValue.Count > 0)
{
args.Add(SearchArgs.PARAMS);
args.Add(nameValue.Count * 2);
foreach (var entry in nameValue)
{
args.Add(entry.Key);
args.Add(entry.Value);
}
}
return this;
}
public AggregationRequest Dialect(int dialect)
{
this.dialect = dialect;
return this;
}
private void Dialect()
{
if (dialect != null)
{
args.Add(SearchArgs.DIALECT);
args.Add(dialect);
}
}
public List<object> GetArgs()
{
return args;
}
public void SerializeRedisArgs()
{
Dialect();
}
public bool IsWithCursor()
{
return isWithCursor;
}
int? IDialectAwareParam.Dialect
{
get { return dialect; }
set { dialect = value; }
}
}