-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
SearchScoreFunctionBuilder.cs
275 lines (246 loc) · 12 KB
/
SearchScoreFunctionBuilder.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
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
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using MongoDB.Bson;
using MongoDB.Driver.Core.Misc;
namespace MongoDB.Driver.Search
{
/// <summary>
/// A builder for a score function.
/// </summary>
/// <typeparam name="TDocument">The type of the document.</typeparam>
public sealed class SearchScoreFunctionBuilder<TDocument>
{
/// <summary>
/// Creates a function that adds a series of numbers.
/// </summary>
/// <param name="operands">An array of expressions, which can have negative values.</param>
/// <returns>An addition score function.</returns>
public SearchScoreFunction<TDocument> Add(IEnumerable<SearchScoreFunction<TDocument>> operands) =>
new ArithmeticSearchScoreFunction<TDocument>("add", operands);
/// <summary>
/// Creates a function that adds a series of numbers.
/// </summary>
/// <param name="operands">An array of expressions, which can have negative values.</param>
/// <returns>An addition score function.</returns>
public SearchScoreFunction<TDocument> Add(params SearchScoreFunction<TDocument>[] operands) =>
Add((IEnumerable<SearchScoreFunction<TDocument>>)operands);
/// <summary>
/// Creates a function that represents a constant number.
/// </summary>
/// <param name="value">Number that indicates a fixed value.</param>
/// <returns>A constant score function.</returns>
public SearchScoreFunction<TDocument> Constant(double value) =>
new ConstantSearchScoreFunction<TDocument>(value);
/// <summary>
/// Creates a function that decays, or reduces by multiplying, the final scores of the
/// documents based on the distance of a numeric field from a specified origin point.
/// </summary>
/// <param name="path">The path to the numeric field.</param>
/// <param name="origin">The point of origin from which to calculate the distance.</param>
/// <param name="scale">
/// The distance from <paramref name="origin"/> plus or minus <paramref name="offset"/> at
/// which scores must be multiplied.
/// </param>
/// <param name="decay">
/// The rate at which to multiply score values, which must be a positive number between
/// 0 and 1 exclusive.
/// </param>
/// <param name="offset">
/// The number of use to determine the distance from <paramref name="origin"/>.
/// </param>
/// <returns>A Guassian score function.</returns>
public SearchScoreFunction<TDocument> Gauss(
SearchPathDefinition<TDocument> path,
double origin,
double scale,
double decay = 0.5,
double offset = 0)
=> new GaussSearchScoreFunction<TDocument>(path, origin, scale, decay, offset);
/// <summary>
/// Creates a function that decays, or reduces by multiplying, the final scores of the
/// documents based on the distance of a numeric field from a specified origin point.
/// </summary>
/// <param name="path">The path to the numeric field.</param>
/// <param name="origin">The point of origin from which to calculate the distance.</param>
/// <param name="scale">
/// The distance from <paramref name="origin"/> plus or minus <paramref name="offset"/> at
/// which scores must be multiplied.
/// </param>
/// <param name="decay">
/// The rate at which to multiply score values, which must be a positive number between
/// 0 and 1 exclusive.
/// </param>
/// <param name="offset">
/// The number of use to determine the distance from <paramref name="origin"/>.
/// </param>
/// <returns>A Guassian score function.</returns>
public SearchScoreFunction<TDocument> Gauss(
Expression<Func<TDocument, double>> path,
double origin,
double scale,
double decay = 0.5,
double offset = 0)
=> Gauss(new ExpressionFieldDefinition<TDocument>(path), origin, scale, decay, offset);
/// <summary>
/// Creates a function that calculates the base-10 logarithm of a number.
/// </summary>
/// <param name="operand">The number.</param>
/// <returns>A logarithmic score function.</returns>
public SearchScoreFunction<TDocument> Log(SearchScoreFunction<TDocument> operand) =>
new UnarySearchScoreFunction<TDocument>("log", operand);
/// <summary>
/// Creates a function that adds 1 to a number and then calculates its base-10 logarithm.
/// </summary>
/// <param name="operand">The number.</param>
/// <returns>A logarithmic score function.</returns>
public SearchScoreFunction<TDocument> Log1p(SearchScoreFunction<TDocument> operand) =>
new UnarySearchScoreFunction<TDocument>("log1p", operand);
/// <summary>
/// Creates a function that multiplies a series of numbers.
/// </summary>
/// <param name="operands">An array of expressions, which can have negative values.</param>
/// <returns>A multiplication score function.</returns>
public SearchScoreFunction<TDocument> Multiply(IEnumerable<SearchScoreFunction<TDocument>> operands) =>
new ArithmeticSearchScoreFunction<TDocument>("multiply", operands);
/// <summary>
/// Creates a function that multiplies a series of numbers.
/// </summary>
/// <param name="operands">An array of expressions, which can have negative values.</param>
/// <returns>A mulitplication score function.</returns>
public SearchScoreFunction<TDocument> Multiply(params SearchScoreFunction<TDocument>[] operands) =>
Multiply((IEnumerable<SearchScoreFunction<TDocument>>)operands);
/// <summary>
/// Creates a function that incorporates an indexed numeric field value into the score.
/// </summary>
/// <param name="path">The path to the numeric field.</param>
/// <param name="undefined">
/// The value to use if the numeric field specified using <paramref name="path"/> is
/// missing in the document.
/// </param>
/// <returns>A path score function.</returns>
public SearchScoreFunction<TDocument> Path(SearchPathDefinition<TDocument> path, double undefined = 0) =>
new PathSearchScoreFunction<TDocument>(path, undefined);
/// <summary>
/// Creates a function that incorporates an indexed numeric field value into the score.
/// </summary>
/// <param name="path">The path to the numeric field.</param>
/// <param name="undefined">
/// The value to use if the numeric field specified using <paramref name="path"/> is
/// missing in the document.
/// </param>
/// <returns>A path score function.</returns>
public SearchScoreFunction<TDocument> Path(Expression<Func<TDocument, double>> path, double undefined = 0) =>
Path(new ExpressionFieldDefinition<TDocument>(path), undefined);
/// <summary>
/// Creates a function that represents the relevance score, which is the score Atlas Search
/// assigns documents based on relevance.
/// </summary>
/// <returns>A relevance score function.</returns>
public SearchScoreFunction<TDocument> Relevance() => new RelevanceSearchScoreFunction<TDocument>();
}
internal sealed class ArithmeticSearchScoreFunction<TDocument> : SearchScoreFunction<TDocument>
{
private readonly SearchScoreFunction<TDocument>[] _operands;
private readonly string _operatorName;
public ArithmeticSearchScoreFunction(string operatorName, IEnumerable<SearchScoreFunction<TDocument>> operands)
{
_operatorName = operatorName;
_operands = Ensure.IsNotNull(operands, nameof(operands)).ToArray();
}
public override BsonDocument Render(SearchDefinitionRenderContext<TDocument> renderContext) =>
new(_operatorName, new BsonArray(_operands.Select(o => o.Render(renderContext))));
}
internal sealed class ConstantSearchScoreFunction<TDocument> : SearchScoreFunction<TDocument>
{
private readonly double _value;
public ConstantSearchScoreFunction(double value)
{
_value = value;
}
public override BsonDocument Render(SearchDefinitionRenderContext<TDocument> renderContext) =>
new("constant", _value);
}
internal sealed class GaussSearchScoreFunction<TDocument> : SearchScoreFunction<TDocument>
{
private readonly double _decay;
private readonly double _offset;
private readonly double _origin;
private readonly SearchPathDefinition<TDocument> _path;
private readonly double _scale;
public GaussSearchScoreFunction(
SearchPathDefinition<TDocument> path,
double origin,
double scale,
double decay,
double offset)
{
_path = Ensure.IsNotNull(path, nameof(path));
_origin = origin;
_scale = scale;
_decay = Ensure.IsBetween(decay, 0, 1, nameof(decay));
_offset = offset;
}
public override BsonDocument Render(SearchDefinitionRenderContext<TDocument> renderContext) =>
new("gauss", new BsonDocument()
{
{ "path", _path.Render(renderContext) },
{ "origin", _origin },
{ "scale", _scale },
{ "decay", _decay, _decay != 0.5 },
{ "offset", _offset, _offset != 0 },
});
}
internal sealed class PathSearchScoreFunction<TDocument> : SearchScoreFunction<TDocument>
{
private readonly SearchPathDefinition<TDocument> _path;
private readonly double _undefined;
public PathSearchScoreFunction(SearchPathDefinition<TDocument> path, double undefined)
{
_path = Ensure.IsNotNull(path, nameof(path));
_undefined = undefined;
}
public override BsonDocument Render(SearchDefinitionRenderContext<TDocument> renderContext)
{
var renderedPath = _path.Render(renderContext);
var pathDocument = _undefined == 0 ? renderedPath : new BsonDocument()
{
{ "value", renderedPath },
{ "undefined", _undefined }
};
return new("path", pathDocument);
}
}
internal sealed class RelevanceSearchScoreFunction<TDocument> : SearchScoreFunction<TDocument>
{
public override BsonDocument Render(SearchDefinitionRenderContext<TDocument> renderContext) =>
new("score", "relevance");
}
internal sealed class UnarySearchScoreFunction<TDocument> : SearchScoreFunction<TDocument>
{
private readonly SearchScoreFunction<TDocument> _operand;
private readonly string _operatorName;
public UnarySearchScoreFunction(string operatorName, SearchScoreFunction<TDocument> operand)
{
_operatorName = operatorName;
_operand = Ensure.IsNotNull(operand, nameof(operand));
}
public override BsonDocument Render(SearchDefinitionRenderContext<TDocument> renderContext) =>
new(_operatorName, _operand.Render(renderContext));
}
}