Skip to content

Commit c07c4d2

Browse files
committed
Add Weight option to function score query
Closes #1071
1 parent fe3105d commit c07c4d2

File tree

3 files changed

+32
-43
lines changed

3 files changed

+32
-43
lines changed

src/Nest/DSL/Query/Functions/IFunctionScoreFunction.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,35 @@ namespace Nest
88
[JsonConverter(typeof(ReadAsTypeConverter<FunctionScoreFunction<object>>))]
99
public interface IFunctionScoreFunction
1010
{
11+
FilterContainer Filter { get; set; }
12+
long? Weight { get; set; }
1113
}
1214

1315
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
14-
public class FunctionScoreFunction<T> : IFunctionScoreFunction
16+
public class FunctionScoreFunction<T> : IFunctionScoreFunction
1517
where T : class
1618
{
17-
[JsonProperty(PropertyName = "filter")]
18-
internal FilterContainer FilterDescriptor { get; set; }
19+
IFunctionScoreFunction Self { get { return this; } }
20+
21+
[JsonProperty("filter")]
22+
FilterContainer IFunctionScoreFunction.Filter { get; set; }
23+
24+
[JsonProperty("weight")]
25+
long? IFunctionScoreFunction.Weight { get; set; }
1926

2027
public FunctionScoreFunction<T> Filter(Func<FilterDescriptor<T>, FilterContainer> filterSelector)
2128
{
2229
filterSelector.ThrowIfNull("filterSelector");
2330
var filter = new FilterDescriptor<T>();
2431
var f = filterSelector(filter);
2532

26-
this.FilterDescriptor = f;
33+
this.Self.Filter = f;
34+
return this;
35+
}
36+
37+
public FunctionScoreFunction<T> Weight(long weight)
38+
{
39+
this.Self.Weight = weight;
2740
return this;
2841
}
2942
}

src/Tests/Nest.Tests.Unit/QueryParsers/Queries/FunctionScoreQueryTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public void FunctionScore_Deserializes()
1717
.BoostMode(FunctionBoostMode.Average)
1818
.MaxBoost(0.95f)
1919
.Functions(
20-
ff => ff.Gauss(x => x.StartedOn, d => d.Scale("42w")),
21-
ff => ff.Linear(x => x.FloatValue, d => d.Scale("0.3")).Filter(lff=>Filter2),
22-
ff => ff.Exp(x => x.DoubleValue, d => d.Scale("0.5")),
20+
ff => ff.Gauss(x => x.StartedOn, d => d.Scale("42w")).Weight(1),
21+
ff => ff.Linear(x => x.FloatValue, d => d.Scale("0.3")).Filter(lff=>Filter2).Weight(2),
22+
ff => ff.Exp(x => x.DoubleValue, d => d.Scale("0.5")).Weight(3),
2323
ff => ff.BoostFactor(2).Filter(bff=>Filter1)
2424
)
2525
.Query(qq=>Query1)

src/Tests/Nest.Tests.Unit/Search/Sorting/FunctionScoreTests.cs

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,34 +55,6 @@ public void TestRandomSortWithSeed()
5555

5656
[Test]
5757
public void TestScriptScore()
58-
{
59-
var s = new SearchDescriptor<ElasticsearchProject>()
60-
.Query(q => q.FunctionScore(
61-
fs => fs.ScriptScore(ss => ss.Script("_score / pow(param1, param2)").Params(p => p.Add("param1", 1.75).Add("param2", 4)).Lang("mvel")
62-
)
63-
)
64-
);
65-
var json = TestElasticClient.Serialize(s);
66-
var expected = @"{
67-
query: {
68-
function_score: {
69-
script_score: {
70-
script: ""_score / pow(param1, param2)"",
71-
params: {
72-
param1: 1.75,
73-
param2: 4
74-
},
75-
lang: ""mvel""
76-
}
77-
}
78-
}
79-
}";
80-
81-
Assert.True(json.JsonEquals(expected), json);
82-
}
83-
84-
[Test]
85-
public void TestScriptScoreWithFilter()
8658
{
8759
var s = new SearchDescriptor<ElasticsearchProject>()
8860
.Query(q => q
@@ -97,7 +69,7 @@ public void TestScriptScoreWithFilter()
9769
.Lang("mvel")
9870
).Filter(filter => filter
9971
.Term("term1", "termValue")
100-
)
72+
).Weight(5)
10173
)
10274
)
10375
);
@@ -119,7 +91,8 @@ public void TestScriptScoreWithFilter()
11991
term : {
12092
""term1"":""termValue""
12193
}
122-
}
94+
},
95+
weight: 5
12396
}
12497
]
12598
}
@@ -130,7 +103,7 @@ public void TestScriptScoreWithFilter()
130103
}
131104

132105
[Test]
133-
public void TestBoostFactorWithFilter()
106+
public void TestBoostFactor()
134107
{
135108
var s = new SearchDescriptor<ElasticsearchProject>().Query(
136109
q => q.FunctionScore(
@@ -139,6 +112,7 @@ public void TestBoostFactorWithFilter()
139112
.Filter(
140113
filter => filter.Term("term1", "termValue")
141114
)
115+
.Weight(5)
142116
)
143117
)
144118
);
@@ -153,7 +127,8 @@ public void TestBoostFactorWithFilter()
153127
term : {
154128
""term1"":""termValue""
155129
}
156-
}
130+
},
131+
weight: 5
157132
}
158133
]
159134
}
@@ -164,14 +139,14 @@ public void TestBoostFactorWithFilter()
164139
}
165140

166141
[Test]
167-
public void TestDecayFunctionWithFilter()
142+
public void TestDecayFunction()
168143
{
169144
var s = new SearchDescriptor<ElasticsearchProject>().Query(
170145
q => q.FunctionScore(
171146
fs => fs.Functions(
172147
f => f.Gauss("floatValue", g => g.Origin("5").Scale("0.1"))
173-
.Filter(gf => gf.Term("term1", "termValue")
174-
)
148+
.Filter(gf => gf.Term("term1", "termValue"))
149+
.Weight(5)
175150
)
176151
)
177152
);
@@ -191,7 +166,8 @@ public void TestDecayFunctionWithFilter()
191166
""term"": {
192167
""term1"": ""termValue""
193168
}
194-
}
169+
},
170+
""weight"": 5
195171
}
196172
]
197173
}

0 commit comments

Comments
 (0)