-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuggestOptionsBuilder.cs
More file actions
287 lines (244 loc) · 12.5 KB
/
SuggestOptionsBuilder.cs
File metadata and controls
287 lines (244 loc) · 12.5 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Azure.Search.Documents;
using AzureSearchQueryBuilder.Helpers;
using Newtonsoft.Json;
namespace AzureSearchQueryBuilder.Builders
{
/// <summary>
/// The <see cref="SuggestOptionsBuilder"/>`1[<typeparamref name="TModel"/>] builder.
/// </summary>
/// <typeparam name="TModel">The type of the model representing the search index documents.</typeparam>
public class SuggestOptionsBuilder<TModel> : OptionsBuilder<TModel, SuggestOptions>, ISuggestOptionsBuilder<TModel>, IOrderedSuggestOptionsBuilder<TModel>
{
private IList<string> _orderBy;
private IList<string> _select;
/// <summary>
/// Constructor.
/// </summary>
private SuggestOptionsBuilder(JsonSerializerSettings jsonSerializerSettings)
: base(jsonSerializerSettings)
{
}
/// <summary>
/// Gets a list of expressions to sort the results by.
/// Each expression can be either a field name or a call to the geo.distance() function.
/// Each expression can be followed by asc to indicated ascending, and desc to indicate descending.
/// The default is ascending order.
/// There is a limit of 32 clauses for $orderby.
/// </summary>
public IEnumerable<string> OrderBy { get => this._orderBy; }
/// <summary>
/// Gets a collection of fields to include in the result set.
/// </summary>
public IEnumerable<string> Select { get => this._select; }
/// <summary>
/// Gets the value indicating that suggestions should be found even if there is a substituted or missing character in the search text.
/// </summary>
public bool UseFuzzyMatching { get; private set; }
/// <summary>
/// Create a new <see cref="ISuggestOptionsBuilder" />`1[<typeparamref name="TModel"/>]"/>.
/// </summary>
/// <returns>a new <see cref="ISuggestOptionsBuilder" />`1[<typeparamref name="TModel"/>]"/>.</returns>
public static ISuggestOptionsBuilder<TModel> Create(JsonSerializerSettings jsonSerializerSettings) => new SuggestOptionsBuilder<TModel>(jsonSerializerSettings);
/// <summary>
/// Build a <see cref="SuggestOptions"/> object.
/// </summary>
/// <returns>the <see cref="SuggestOptions"/> object.</returns>
public override SuggestOptions Build()
{
SuggestOptions suggestOptions = new SuggestOptions()
{
Filter = this.Filter,
HighlightPostTag = this.HighlightPostTag,
HighlightPreTag = this.HighlightPreTag,
MinimumCoverage = this.MinimumCoverage,
Size = this.Size,
UseFuzzyMatching = this.UseFuzzyMatching,
};
if (this.OrderBy != null)
{
foreach (string orderBy in this.OrderBy)
{
suggestOptions.OrderBy.Add(orderBy);
}
}
if (this.SearchFields != null)
{
foreach (string searchField in this.SearchFields)
{
suggestOptions.SearchFields.Add(searchField);
}
}
if (this.Select != null)
{
foreach (string select in this.Select)
{
suggestOptions.Select.Add(select);
}
}
return suggestOptions;
}
/// <summary>
/// Uses an expression for sorting the elements of a sequence in ascending order using a specified comparer.
/// </summary>
/// <typeparam name="TProperty">The type of the property to be ordered by.</typeparam>
/// <param name="lambdaExpression">An expression to extract a property from each element.</param>
/// <returns>the updated builder.</returns>
public IOrderedSuggestOptionsBuilder<TModel> WithOrderBy<TProperty>(Expression<PropertyLambdaDelegate<TModel, TProperty>> lambdaExpression)
{
if (lambdaExpression == null) throw new ArgumentNullException(nameof(lambdaExpression));
this._orderBy = new List<string>();
string orderBy = PropertyNameUtility.GetPropertyName(lambdaExpression, this.JsonSerializerSettings, false);
this._orderBy.Add($"{orderBy} asc");
return this;
}
/// <summary>
/// Uses an expression for sorting the elements of a sequence in descending order using a specified comparer.
/// </summary>
/// <typeparam name="TProperty">The type of the property to be ordered by.</typeparam>
/// <param name="lambdaExpression">An expression to extract a property from each element.</param>
/// <returns>the updated builder.</returns>
public IOrderedSuggestOptionsBuilder<TModel> WithOrderByDescending<TProperty>(Expression<PropertyLambdaDelegate<TModel, TProperty>> lambdaExpression)
{
if (lambdaExpression == null) throw new ArgumentNullException(nameof(lambdaExpression));
this._orderBy = new List<string>();
string orderBy = PropertyNameUtility.GetPropertyName(lambdaExpression, this.JsonSerializerSettings, false);
this._orderBy.Add($"{orderBy} desc");
return this;
}
/// <summary>
/// Adds a property to the collection of fields to include in the result set.
/// </summary>
/// <typeparam name="TProperty">The type of the property being selected.</typeparam>
/// <param name="lambdaExpression">An expression to extract a property.</param>
/// <returns>the updated builder.</returns>
public ISuggestOptionsBuilder<TModel> WithSelect<TProperty>(Expression<PropertyLambdaDelegate<TModel, TProperty>> lambdaExpression)
{
if (lambdaExpression == null) throw new ArgumentNullException(nameof(lambdaExpression));
if (this._select == null)
{
this._select = new List<string>();
}
string selectField = PropertyNameUtility.GetPropertyName(lambdaExpression, this.JsonSerializerSettings, false);
this._select.Add(selectField);
return this;
}
/// <summary>
/// Uses an expression for performing a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.
/// </summary>
/// <typeparam name="TProperty">The type of the property to be ordered by.</typeparam>
/// <param name="lambdaExpression">An expression to extract a property from each element.</param>
/// <returns>the updated builder.</returns>
public IOrderedSuggestOptionsBuilder<TModel> WithThenBy<TProperty>(Expression<PropertyLambdaDelegate<TModel, TProperty>> lambdaExpression)
{
if (lambdaExpression == null) throw new ArgumentNullException(nameof(lambdaExpression));
if (this._orderBy == null || this._orderBy.Count < 1) throw new ArgumentException($"{nameof(ISuggestOptionsBuilder<TModel>.OrderBy)} has not been initialized", nameof(lambdaExpression));
if (this._orderBy.Count >= 32) throw new ArgumentException($"{nameof(ISuggestOptionsBuilder<TModel>.OrderBy)} has exceeded the maximum 32 clauses", nameof(lambdaExpression));
string orderBy = PropertyNameUtility.GetPropertyName(lambdaExpression, this.JsonSerializerSettings, false);
this._orderBy.Add($"{orderBy} asc");
return this;
}
/// <summary>
/// Uses an expression for performing a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.
/// </summary>
/// <typeparam name="TProperty">The type of the property to be ordered by.</typeparam>
/// <param name="lambdaExpression">An expression to extract a property from each element.</param>
/// <returns>the updated builder.</returns>
public IOrderedSuggestOptionsBuilder<TModel> WithThenByDescending<TProperty>(Expression<PropertyLambdaDelegate<TModel, TProperty>> lambdaExpression)
{
if (lambdaExpression == null) throw new ArgumentNullException(nameof(lambdaExpression));
if (this._orderBy == null || this._orderBy.Count < 1) throw new ArgumentException($"{nameof(ISuggestOptionsBuilder<TModel>.OrderBy)} has not been initialized", nameof(lambdaExpression));
if (this._orderBy.Count >= 32) throw new ArgumentException($"{nameof(ISuggestOptionsBuilder<TModel>.OrderBy)} has exceeded the maximum 32 clauses", nameof(lambdaExpression));
string orderBy = PropertyNameUtility.GetPropertyName(lambdaExpression, this.JsonSerializerSettings, false);
this._orderBy.Add($"{orderBy} desc");
return this;
}
/// <summary>
/// Sets the use fuzzy matching value.
/// </summary>
/// <param name="useFuzzyMatching">The desired fuzzy matching mode.</param>
/// <returns>the updated builder.</returns>
public ISuggestOptionsBuilder<TModel> WithUseFuzzyMatching(bool useFuzzyMatching)
{
this.UseFuzzyMatching = useFuzzyMatching;
return this;
}
#region IOrderedSuggestOptionsBuilder Explicit Implementation
IOrderedSuggestOptionsBuilder<TModel> IOrderedSuggestOptionsBuilder<TModel>.WithSelect<TProperty>(Expression<PropertyLambdaDelegate<TModel, TProperty>> lambdaExpression)
{
this.WithSelect(lambdaExpression);
return this;
}
IOrderedSuggestOptionsBuilder<TModel> IOrderedSuggestOptionsBuilder<TModel>.WithUseFuzzyMatching(bool useFuzzyMatching)
{
this.WithUseFuzzyMatching(useFuzzyMatching);
return this;
}
IOrderedSuggestOptionsBuilder<TModel> IOrderedSuggestOptionsBuilder<TModel>.Where(Expression<BooleanLambdaDelegate<TModel>> lambdaExpression)
{
this.Where(lambdaExpression);
return this;
}
IOrderedSuggestOptionsBuilder<TModel> IOrderedSuggestOptionsBuilder<TModel>.WithHighlightPostTag(string highlightPostTag)
{
this.WithHighlightPostTag(highlightPostTag);
return this;
}
IOrderedSuggestOptionsBuilder<TModel> IOrderedSuggestOptionsBuilder<TModel>.WithHighlightPreTag(string highlightPreTag)
{
this.WithHighlightPreTag(highlightPreTag);
return this;
}
IOrderedSuggestOptionsBuilder<TModel> IOrderedSuggestOptionsBuilder<TModel>.WithMinimumCoverage(double? minimumCoverage)
{
this.WithMinimumCoverage(minimumCoverage);
return this;
}
IOrderedSuggestOptionsBuilder<TModel> IOrderedSuggestOptionsBuilder<TModel>.WithSearchField<TProperty>(Expression<PropertyLambdaDelegate<TModel, TProperty>> lambdaExpression)
{
this.WithSearchField(lambdaExpression);
return this;
}
IOrderedSuggestOptionsBuilder<TModel> IOrderedSuggestOptionsBuilder<TModel>.WithSize(int? size)
{
this.WithSize(size);
return this;
}
#endregion
#region ISuggestOptionsBuilder Explicit Implementation
ISuggestOptionsBuilder<TModel> ISuggestOptionsBuilder<TModel>.Where(Expression<BooleanLambdaDelegate<TModel>> lambdaExpression)
{
this.Where(lambdaExpression);
return this;
}
ISuggestOptionsBuilder<TModel> ISuggestOptionsBuilder<TModel>.WithHighlightPostTag(string highlightPostTag)
{
this.WithHighlightPostTag(highlightPostTag);
return this;
}
ISuggestOptionsBuilder<TModel> ISuggestOptionsBuilder<TModel>.WithHighlightPreTag(string highlightPreTag)
{
this.WithHighlightPreTag(highlightPreTag);
return this;
}
ISuggestOptionsBuilder<TModel> ISuggestOptionsBuilder<TModel>.WithMinimumCoverage(double? minimumCoverage)
{
this.WithMinimumCoverage(minimumCoverage);
return this;
}
ISuggestOptionsBuilder<TModel> ISuggestOptionsBuilder<TModel>.WithSearchField<TProperty>(Expression<PropertyLambdaDelegate<TModel, TProperty>> lambdaExpression)
{
this.WithSearchField(lambdaExpression);
return this;
}
ISuggestOptionsBuilder<TModel> ISuggestOptionsBuilder<TModel>.WithSize(int? size)
{
this.WithSize(size);
return this;
}
#endregion
}
}