Skip to content

Commit afd4058

Browse files
russcamStuart Cam
authored andcommitted
Serialize only script in script_score function (#3909)
This commit updates the ScoreFunctionJsonFormatter to serialize only the script inside of the script_score JSON object. Fixes #3904 (cherry picked from commit ac60472)
1 parent 3d38ebc commit afd4058

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/Nest/QueryDsl/Compound/FunctionScore/Functions/ScoreFunctionJsonFormatter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,11 @@ public void Serialize(ref JsonWriter writer, IScoreFunction value, IJsonFormatte
158158
private static void WriteScriptScore(ref JsonWriter writer, IScriptScoreFunction value, IJsonFormatterResolver formatterResolver)
159159
{
160160
writer.WritePropertyName("script_score");
161-
var scriptFormatter = formatterResolver.GetFormatter<IScriptScoreFunction>();
162-
scriptFormatter.Serialize(ref writer, value, formatterResolver);
161+
writer.WriteBeginObject();
162+
writer.WritePropertyName("script");
163+
var scriptFormatter = formatterResolver.GetFormatter<IScript>();
164+
scriptFormatter.Serialize(ref writer, value?.Script, formatterResolver);
165+
writer.WriteEndObject();
163166
}
164167

165168
private static void WriteRandomScore(ref JsonWriter writer, IRandomScoreFunction value, IJsonFormatterResolver formatterResolver)

src/Tests/Tests/QueryDsl/Compound/FunctionScore/FunctionScoreQueryUsageTests.cs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,19 @@ public FunctionScoreQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
3737
MinScore = 1.0,
3838
Functions = new List<IScoreFunction>
3939
{
40-
new ExponentialDecayFunction { Origin = 1.0, Decay = 0.5, Field = Field<Project>(p => p.NumberOfCommits), Scale = 0.1, Weight = 2.1 },
40+
new ExponentialDecayFunction
41+
{
42+
Origin = 1.0,
43+
Decay = 0.5,
44+
Field = Field<Project>(p => p.NumberOfCommits),
45+
Scale = 0.1,
46+
Weight = 2.1,
47+
Filter = new NumericRangeQuery
48+
{
49+
Field = Field<Project>(f => f.NumberOfContributors),
50+
GreaterThan = 10
51+
}
52+
},
4153
new GaussDateDecayFunction
4254
{ Origin = DateMath.Now, Field = Field<Project>(p => p.LastActivity), Decay = 0.5, Scale = TimeSpan.FromDays(1) },
4355
new LinearGeoDecayFunction
@@ -52,7 +64,7 @@ public FunctionScoreQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
5264
new RandomScoreFunction { Seed = 1337, Field = "_seq_no" },
5365
new RandomScoreFunction { Seed = "randomstring", Field = "_seq_no" },
5466
new WeightFunction { Weight = 1.0 },
55-
new ScriptScoreFunction { Script = new InlineScript("Math.log(2 + doc['numberOfCommits'].value)") }
67+
new ScriptScoreFunction { Script = new InlineScript("Math.log(2 + doc['numberOfCommits'].value)"), Weight = 2.0 }
5668
}
5769
};
5870

@@ -76,7 +88,17 @@ public FunctionScoreQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
7688
decay = 0.5
7789
}
7890
},
79-
weight = 2.1
91+
weight = 2.1,
92+
filter = new
93+
{
94+
range = new
95+
{
96+
numberOfContributors = new
97+
{
98+
gt = 10.0
99+
}
100+
}
101+
}
80102
},
81103
new
82104
{
@@ -127,7 +149,8 @@ public FunctionScoreQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
127149
{
128150
source = "Math.log(2 + doc['numberOfCommits'].value)"
129151
}
130-
}
152+
},
153+
weight = 2.0
131154
}
132155
},
133156
max_boost = 20.0,
@@ -150,15 +173,36 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
150173
.MaxBoost(20.0)
151174
.MinScore(1.0)
152175
.Functions(f => f
153-
.Exponential(b => b.Field(p => p.NumberOfCommits).Decay(0.5).Origin(1.0).Scale(0.1).Weight(2.1))
176+
.Exponential(b => b
177+
.Field(p => p.NumberOfCommits)
178+
.Decay(0.5)
179+
.Origin(1.0)
180+
.Scale(0.1)
181+
.Weight(2.1)
182+
.Filter(fi => fi
183+
.Range(r => r
184+
.Field(p => p.NumberOfContributors)
185+
.GreaterThan(10)
186+
)
187+
)
188+
)
154189
.GaussDate(b => b.Field(p => p.LastActivity).Origin(DateMath.Now).Decay(0.5).Scale("1d"))
155-
.LinearGeoLocation(b =>
156-
b.Field(p => p.LocationPoint).Origin(new GeoLocation(70, -70)).Scale(Distance.Miles(1)).MultiValueMode(MultiValueMode.Average))
190+
.LinearGeoLocation(b => b
191+
.Field(p => p.LocationPoint)
192+
.Origin(new GeoLocation(70, -70))
193+
.Scale(Distance.Miles(1))
194+
.MultiValueMode(MultiValueMode.Average)
195+
)
157196
.FieldValueFactor(b => b.Field(p => p.NumberOfContributors).Factor(1.1).Missing(0.1).Modifier(FieldValueFactorModifier.Square))
158197
.RandomScore(r => r.Seed(1337).Field("_seq_no"))
159198
.RandomScore(r => r.Seed("randomstring").Field("_seq_no"))
160199
.Weight(1.0)
161-
.ScriptScore(s => s.Script(ss => ss.Source("Math.log(2 + doc['numberOfCommits'].value)")))
200+
.ScriptScore(s => s
201+
.Script(ss => ss
202+
.Source("Math.log(2 + doc['numberOfCommits'].value)")
203+
)
204+
.Weight(2)
205+
)
162206
)
163207
);
164208
}

0 commit comments

Comments
 (0)