Skip to content

Commit 8c88599

Browse files
committed
Add weight function to function score query
Closes #1223
1 parent e744b7c commit 8c88599

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public FunctionScoreFunction<T> FieldValueFactor(Action<FieldValueFactorDescript
7878
this._Functions.Add(fn);
7979
return fn;
8080
}
81+
82+
public FunctionScoreFunction<T> Weight(double weight)
83+
{
84+
var fn = new WeightFunction<T>(weight);
85+
this._Functions.Add(fn);
86+
return fn;
87+
}
88+
8189
public IEnumerator<FunctionScoreFunction<T>> GetEnumerator()
8290
{
8391
return _Functions.GetEnumerator();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace Nest
8+
{
9+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
10+
public class WeightFunction<T> : FunctionScoreFunction<T> where T : class
11+
{
12+
[JsonProperty(PropertyName = "weight")]
13+
internal double _Weight { get; set; }
14+
15+
public WeightFunction(double weight)
16+
{
17+
_Weight = weight;
18+
}
19+
}
20+
}

src/Nest/Nest.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4-
<PropertyGroup>
4+
<PropertyGroup>
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
77
<ProjectGuid>{072BA7DA-7B60-407D-8B6E-95E3186BE70C}</ProjectGuid>
@@ -234,6 +234,7 @@
234234
<Compile Include="DSL\Aggregations\ReverseNestedAggregationDescriptor.cs" />
235235
<Compile Include="DSL\Filter\IndicesFilterDescriptor.cs" />
236236
<Compile Include="Domain\DSL\NoMatchShortcut.cs" />
237+
<Compile Include="DSL\Query\Functions\WeightFunction.cs" />
237238
<Compile Include="DSL\UpgradeDescriptor.cs" />
238239
<Compile Include="DSL\UpgradeStatusDescriptor.cs" />
239240
<Compile Include="DSL\VerifyRepositoryDescriptor.cs" />
@@ -1140,4 +1141,4 @@
11401141
</ItemGroup>
11411142
</When>
11421143
</Choose>
1143-
</Project>
1144+
</Project>

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public void FunctionScore_Deserializes()
2020
ff => ff.Gauss(x => x.StartedOn, d => d.Scale("42w")).Weight(1),
2121
ff => ff.Linear(x => x.FloatValue, d => d.Scale("0.3")).Filter(lff=>Filter2).Weight(2),
2222
ff => ff.Exp(x => x.DoubleValue, d => d.Scale("0.5")).Weight(3),
23-
ff => ff.BoostFactor(2).Filter(bff=>Filter1)
23+
ff => ff.BoostFactor(2).Filter(bff=>Filter1),
24+
ff => ff.Weight(5.0).Filter(wf =>Filter1)
2425
)
2526
.Query(qq=>Query1)
2627
.RandomScore(1337)
@@ -45,11 +46,11 @@ public void FunctionScore_Deserializes()
4546
param.Should().NotBeNull();
4647
param.Key.Should().Be("param");
4748
param.Value.Should().Be("paramvalue");
48-
q.Functions.Should().NotBeEmpty().And.HaveCount(4);
49+
q.Functions.Should().NotBeEmpty().And.HaveCount(5);
4950

5051
//TODO rip out state from all these function descriptors
5152
var functions = q.Functions.ToList();
52-
functions.Should().NotBeEmpty().And.HaveCount(4);
53+
functions.Should().NotBeEmpty().And.HaveCount(5);
5354

5455

5556
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public void FunctionScoreQuery()
1414
.FunctionScore(fs => fs
1515
.Query(qq => qq.MatchAll())
1616
.Functions(
17+
f => f.Weight(3.0).Filter(ff => ff.Term(p => p.Name, "elasticsearch")),
1718
f => f.Gauss(x => x.StartedOn, d => d.Scale("42w")),
1819
f => f.Linear(x => x.FloatValue, d => d.Scale("0.3")),
1920
f => f.Exp(x => x.DoubleValue, d => d.Scale("0.5")),
@@ -32,6 +33,7 @@ public void FunctionScoreQuery()
3233
query : {
3334
function_score : {
3435
functions: [
36+
{weight: 3.0, filter: { term: { 'name': 'elasticsearch' }}},
3537
{gauss: { startedOn : { scale: '42w'}}},
3638
{linear: { floatValue : { scale: '0.3'}}},
3739
{exp: { doubleValue: { scale: '0.5'}}},

0 commit comments

Comments
 (0)