Skip to content

Commit 90315b3

Browse files
committed
Map field value factor in function score
1 parent 621f1cb commit 90315b3

File tree

2 files changed

+110
-39
lines changed

2 files changed

+110
-39
lines changed

src/Nest/DSL/Query/FunctionScoreQueryDescriptor.cs

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
using System;
2-
using System.Collections;
3-
using System.Collections.Generic;
4-
using System.Linq;
1+
using Nest.Resolvers;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Converters;
4+
using System;
5+
using System.Collections;
6+
using System.Collections.Generic;
7+
using System.Linq;
58
using System.Linq.Expressions;
6-
using System.Text;
7-
using Elasticsearch.Net;
8-
using Nest.Resolvers;
9-
using Newtonsoft.Json;
10-
using Newtonsoft.Json.Converters;
119

1210
namespace Nest
1311
{
@@ -33,7 +31,7 @@ public class FunctionScoreQueryDescriptor<T> : IQuery where T : class
3331
RandomScoreFunction _RandomScore { get; set; }
3432

3533
[JsonProperty(PropertyName = "script_score")]
36-
ScriptFilterDescriptor _ScriptScore { get; set; }
34+
ScriptFilterDescriptor _ScriptScore { get; set; }
3735

3836
bool IQuery.IsConditionless
3937
{
@@ -98,7 +96,7 @@ public FunctionScoreQueryDescriptor<T> ScriptScore(Action<ScriptFilterDescriptor
9896
this._ScriptScore = descriptor;
9997

10098
return this;
101-
}
99+
}
102100
}
103101

104102
public enum FunctionScoreMode
@@ -156,9 +154,16 @@ public BoostFactorFunction<T> BoostFactor(double value)
156154
var fn = new BoostFactorFunction<T>(value);
157155
this._Functions.Add(fn);
158156
return fn;
159-
}
160-
161-
public ScriptScoreFunction<T> ScriptScore(Action<ScriptFilterDescriptor> scriptSelector)
157+
}
158+
159+
public FieldValueFactor<T> FieldValueFactor(Action<FieldValueFactorDescriptor<T>> db)
160+
{
161+
var fn = new FieldValueFactor<T>(db);
162+
this._Functions.Add(fn);
163+
return fn;
164+
}
165+
166+
public ScriptScoreFunction<T> ScriptScore(Action<ScriptFilterDescriptor> scriptSelector)
162167
{
163168
var fn = new ScriptScoreFunction<T>(scriptSelector);
164169
this._Functions.Add(fn);
@@ -174,12 +179,45 @@ IEnumerator IEnumerable.GetEnumerator()
174179
{
175180
return _Functions.GetEnumerator();
176181
}
177-
}
178-
179-
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
182+
}
183+
184+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
180185
public class FunctionScoreFunction<T>
181186
{
182-
}
187+
}
188+
189+
190+
public class FieldValueFactorDescriptor<T>
191+
{
192+
[JsonProperty("field")]
193+
internal PropertyPathMarker _Field { get; set; }
194+
195+
[JsonProperty("factor")]
196+
internal double _Factor { get; set; }
197+
198+
[JsonProperty("modifier")]
199+
[JsonConverter(typeof(StringEnumConverter))]
200+
201+
internal FieldValueFactorModifier _Modifier { get; set; }
202+
203+
public FieldValueFactorDescriptor<T> Field(Expression<Func<T, object>> field)
204+
{
205+
this._Field = field;
206+
return this;
207+
}
208+
209+
public FieldValueFactorDescriptor<T> Factor(double factor)
210+
{
211+
this._Factor = factor;
212+
return this;
213+
}
214+
215+
public FieldValueFactorDescriptor<T> Modifier(FieldValueFactorModifier modifier)
216+
{
217+
this._Modifier = modifier;
218+
return this;
219+
}
220+
}
183221

184222
public class FunctionScoreDecayFieldDescriptor
185223
{
@@ -303,9 +341,38 @@ public BoostFactorFunction(double boostFactor)
303341
{
304342
_BoostFactor = boostFactor;
305343
}
306-
}
307-
308-
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
344+
}
345+
346+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
347+
public class FieldValueFactor<T> : FunctionScoreFilteredFunction<T> where T : class
348+
{
349+
[JsonProperty(PropertyName = "field_value_factor")]
350+
internal FieldValueFactorDescriptor<T> _FieldValueFactor { get; set; }
351+
352+
public FieldValueFactor(Action<FieldValueFactorDescriptor<T>> descriptorBuilder)
353+
{
354+
var descriptor = new FieldValueFactorDescriptor<T>();
355+
descriptorBuilder(descriptor);
356+
357+
this._FieldValueFactor = descriptor;
358+
}
359+
}
360+
361+
public enum FieldValueFactorModifier
362+
{
363+
none,
364+
log,
365+
log1p,
366+
log2p,
367+
ln,
368+
ln1p,
369+
ln2p,
370+
square,
371+
sqrt,
372+
reciprocal
373+
}
374+
375+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
309376
public class ScriptScoreFunction<T> : FunctionScoreFilteredFunction<T> where T : class
310377
{
311378
[JsonProperty(PropertyName = "script_score")]

src/Tests/Nest.Tests.Unit/Search/Query/Singles/FunctionScoreQueryJson.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ namespace Nest.Tests.Unit.Search.Query.Singles
88
public class FunctionScoreQueryTests
99
{
1010
[Test]
11-
public void FunctionScoreQuery()
12-
{
13-
var s = new SearchDescriptor<ElasticsearchProject>().From(0).Size(10)
14-
.Query(q => q
15-
.FunctionScore(fs => fs
16-
.Query(qq => qq.MatchAll())
17-
.Functions(
18-
f => f.Gauss(x => x.StartedOn, d => d.Scale("42w")),
19-
f => f.Linear(x => x.FloatValue, d => d.Scale("0.3")),
20-
f => f.Exp(x => x.DoubleValue, d => d.Scale("0.5")),
21-
f => f.BoostFactor(2)
22-
)
23-
.ScoreMode(FunctionScoreMode.sum)
24-
.BoostMode(FunctionBoostMode.replace)
25-
)
11+
public void FunctionScoreQuery()
12+
{
13+
var s = new SearchDescriptor<ElasticsearchProject>().From(0).Size(10)
14+
.Query(q => q
15+
.FunctionScore(fs => fs
16+
.Query(qq => qq.MatchAll())
17+
.Functions(
18+
f => f.Gauss(x => x.StartedOn, d => d.Scale("42w")),
19+
f => f.Linear(x => x.FloatValue, d => d.Scale("0.3")),
20+
f => f.Exp(x => x.DoubleValue, d => d.Scale("0.5")),
21+
f => f.BoostFactor(2.0),
22+
f => f.FieldValueFactor(op => op.Field(ff=>ff.DoubleValue).Factor(2.5).Modifier(FieldValueFactorModifier.sqrt))
23+
)
24+
.ScoreMode(FunctionScoreMode.sum)
25+
.BoostMode(FunctionBoostMode.replace)
26+
)
2627
).Fields(x => x.Content);
2728

2829
var json = TestElasticClient.Serialize(s);
@@ -34,7 +35,8 @@ public void FunctionScoreQuery()
3435
{gauss: { startedOn : { scale: '42w'}}},
3536
{linear: { floatValue : { scale: '0.3'}}},
3637
{exp: { doubleValue: { scale: '0.5'}}},
37-
{boost_factor: 2.0 }
38+
{boost_factor: 2.0 },
39+
{field_value_factor: { field: 'doubleValue', factor: 2.5, modifier: 'sqrt'}}
3840
],
3941
query : { match_all : {} },
4042
score_mode: 'sum',
@@ -57,7 +59,8 @@ public void FunctionScoreQueryConditionless()
5759
f => f.Gauss(x => x.StartedOn, d => d.Scale("42w")),
5860
f => f.Linear(x => x.FloatValue, d => d.Scale("0.3")),
5961
f => f.Exp(x => x.DoubleValue, d => d.Scale("0.5")),
60-
f => f.BoostFactor(2)
62+
f => f.BoostFactor(2),
63+
f => f.FieldValueFactor(db=>db.Field(fa=>fa.DoubleValue).Factor(3.4).Modifier(FieldValueFactorModifier.ln))
6164
)
6265
.ScoreMode(FunctionScoreMode.sum)
6366
.BoostMode(FunctionBoostMode.replace)
@@ -73,7 +76,8 @@ public void FunctionScoreQueryConditionless()
7376
{gauss: { startedOn : { scale: '42w'}}},
7477
{linear: { floatValue : { scale: '0.3'}}},
7578
{exp: { doubleValue: { scale: '0.5'}}},
76-
{boost_factor: 2.0 }
79+
{boost_factor: 2.0 },
80+
{field_value_factor: { field: 'doubleValue', factor: 3.4, modifier: 'ln'}}
7781
],
7882
score_mode: 'sum',
7983
boost_mode: 'replace',

0 commit comments

Comments
 (0)