-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
SearchScoreDefinitionBuilder.cs
149 lines (131 loc) · 5.89 KB
/
SearchScoreDefinitionBuilder.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
/* 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.Linq.Expressions;
using MongoDB.Bson;
using MongoDB.Driver.Core.Misc;
namespace MongoDB.Driver.Search
{
/// <summary>
/// A builder for a score modifier.
/// </summary>
/// <typeparam name="TDocument">The type of the document.</typeparam>
public sealed class SearchScoreDefinitionBuilder<TDocument>
{
/// <summary>
/// Creates a score modifier that multiplies a result's base score by a given number.
/// </summary>
/// <param name="value">The number to multiply the default base score by.</param>
/// <returns>
/// A boost score modifier.
/// </returns>
public SearchScoreDefinition<TDocument> Boost(double value) =>
new BoostValueSearchScoreDefinition<TDocument>(value);
/// <summary>
/// Creates a score modifier that multiples a result's base score by the value of a numeric
/// field in the documents.
/// </summary>
/// <param name="path">
/// The path to the numeric field whose value to multiply the default base score by.
/// </param>
/// <param name="undefined">
/// The numeric value to substitute if the numeric field is not found in the documents.
/// </param>
/// <returns>
/// A boost score modifier.
/// </returns>
public SearchScoreDefinition<TDocument> Boost(SearchPathDefinition<TDocument> path, double undefined = 0) =>
new BoostPathSearchScoreDefinition<TDocument>(path, undefined);
/// <summary>
/// Creates a score modifier that multiplies a result's base score by the value of a numeric
/// field in the documents.
/// </summary>
/// <param name="path">
/// The path to the numeric field whose value to multiply the default base score by.
/// </param>
/// <param name="undefined">
/// The numeric value to substitute if the numeric field is not found in the documents.
/// </param>
/// <returns>
/// A boost score modifier.
/// </returns>
public SearchScoreDefinition<TDocument> Boost(Expression<Func<TDocument, double>> path, double undefined = 0) =>
Boost(new ExpressionFieldDefinition<TDocument>(path), undefined);
/// <summary>
/// Creates a score modifier that replaces the base score with a given number.
/// </summary>
/// <param name="value">The number to replace the base score with.</param>
/// <returns>
/// A constant score modifier.
/// </returns>
public SearchScoreDefinition<TDocument> Constant(double value) =>
new ConstantSearchScoreDefinition<TDocument>(value);
/// <summary>
/// Creates a score modifier that computes the final score through an expression.
/// </summary>
/// <param name="function">The expression used to compute the score.</param>
/// <returns>
/// A function score modifier.
/// </returns>
public SearchScoreDefinition<TDocument> Function(SearchScoreFunction<TDocument> function) =>
new FunctionSearchScoreDefinition<TDocument>(function);
}
internal sealed class BoostPathSearchScoreDefinition<TDocument> : SearchScoreDefinition<TDocument>
{
private readonly SearchPathDefinition<TDocument> _path;
private readonly double _undefined;
public BoostPathSearchScoreDefinition(SearchPathDefinition<TDocument> path, double undefined)
{
_path = Ensure.IsNotNull(path, nameof(path));
_undefined = undefined;
}
public override BsonDocument Render(SearchDefinitionRenderContext<TDocument> renderContext) =>
new("boost", new BsonDocument
{
{ "path", _path.Render(renderContext) },
{ "undefined", _undefined, _undefined != 0 }
});
}
internal sealed class BoostValueSearchScoreDefinition<TDocument> : SearchScoreDefinition<TDocument>
{
private readonly double _value;
public BoostValueSearchScoreDefinition(double value)
{
_value = Ensure.IsGreaterThanZero(value, nameof(value));
}
public override BsonDocument Render(SearchDefinitionRenderContext<TDocument> renderContext) =>
new("boost", new BsonDocument("value", _value));
}
internal sealed class ConstantSearchScoreDefinition<TDocument> : SearchScoreDefinition<TDocument>
{
private readonly double _value;
public ConstantSearchScoreDefinition(double value)
{
_value = Ensure.IsGreaterThanZero(value, nameof(value));
}
public override BsonDocument Render(SearchDefinitionRenderContext<TDocument> renderContext) =>
new("constant", new BsonDocument("value", _value));
}
internal sealed class FunctionSearchScoreDefinition<TDocument> : SearchScoreDefinition<TDocument>
{
private readonly SearchScoreFunction<TDocument> _function;
public FunctionSearchScoreDefinition(SearchScoreFunction<TDocument> function)
{
_function = Ensure.IsNotNull(function, nameof(function));
}
public override BsonDocument Render(SearchDefinitionRenderContext<TDocument> renderContext) =>
new("function", _function.Render(renderContext));
}
}