Skip to content

Commit

Permalink
Serialize only script in script_score function (#3909)
Browse files Browse the repository at this point in the history
This commit updates the ScoreFunctionJsonFormatter to serialize only the
script inside of the script_score JSON object.

Fixes #3904

(cherry picked from commit ac60472)
  • Loading branch information
russcam authored and Stuart Cam committed Jul 19, 2019
1 parent 3d38ebc commit afd4058
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,11 @@ public void Serialize(ref JsonWriter writer, IScoreFunction value, IJsonFormatte
private static void WriteScriptScore(ref JsonWriter writer, IScriptScoreFunction value, IJsonFormatterResolver formatterResolver)
{
writer.WritePropertyName("script_score");
var scriptFormatter = formatterResolver.GetFormatter<IScriptScoreFunction>();
scriptFormatter.Serialize(ref writer, value, formatterResolver);
writer.WriteBeginObject();
writer.WritePropertyName("script");
var scriptFormatter = formatterResolver.GetFormatter<IScript>();
scriptFormatter.Serialize(ref writer, value?.Script, formatterResolver);
writer.WriteEndObject();
}

private static void WriteRandomScore(ref JsonWriter writer, IRandomScoreFunction value, IJsonFormatterResolver formatterResolver)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,19 @@ public FunctionScoreQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
MinScore = 1.0,
Functions = new List<IScoreFunction>
{
new ExponentialDecayFunction { Origin = 1.0, Decay = 0.5, Field = Field<Project>(p => p.NumberOfCommits), Scale = 0.1, Weight = 2.1 },
new ExponentialDecayFunction
{
Origin = 1.0,
Decay = 0.5,
Field = Field<Project>(p => p.NumberOfCommits),
Scale = 0.1,
Weight = 2.1,
Filter = new NumericRangeQuery
{
Field = Field<Project>(f => f.NumberOfContributors),
GreaterThan = 10
}
},
new GaussDateDecayFunction
{ Origin = DateMath.Now, Field = Field<Project>(p => p.LastActivity), Decay = 0.5, Scale = TimeSpan.FromDays(1) },
new LinearGeoDecayFunction
Expand All @@ -52,7 +64,7 @@ public FunctionScoreQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
new RandomScoreFunction { Seed = 1337, Field = "_seq_no" },
new RandomScoreFunction { Seed = "randomstring", Field = "_seq_no" },
new WeightFunction { Weight = 1.0 },
new ScriptScoreFunction { Script = new InlineScript("Math.log(2 + doc['numberOfCommits'].value)") }
new ScriptScoreFunction { Script = new InlineScript("Math.log(2 + doc['numberOfCommits'].value)"), Weight = 2.0 }
}
};

Expand All @@ -76,7 +88,17 @@ public FunctionScoreQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
decay = 0.5
}
},
weight = 2.1
weight = 2.1,
filter = new
{
range = new
{
numberOfContributors = new
{
gt = 10.0
}
}
}
},
new
{
Expand Down Expand Up @@ -127,7 +149,8 @@ public FunctionScoreQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
{
source = "Math.log(2 + doc['numberOfCommits'].value)"
}
}
},
weight = 2.0
}
},
max_boost = 20.0,
Expand All @@ -150,15 +173,36 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
.MaxBoost(20.0)
.MinScore(1.0)
.Functions(f => f
.Exponential(b => b.Field(p => p.NumberOfCommits).Decay(0.5).Origin(1.0).Scale(0.1).Weight(2.1))
.Exponential(b => b
.Field(p => p.NumberOfCommits)
.Decay(0.5)
.Origin(1.0)
.Scale(0.1)
.Weight(2.1)
.Filter(fi => fi
.Range(r => r
.Field(p => p.NumberOfContributors)
.GreaterThan(10)
)
)
)
.GaussDate(b => b.Field(p => p.LastActivity).Origin(DateMath.Now).Decay(0.5).Scale("1d"))
.LinearGeoLocation(b =>
b.Field(p => p.LocationPoint).Origin(new GeoLocation(70, -70)).Scale(Distance.Miles(1)).MultiValueMode(MultiValueMode.Average))
.LinearGeoLocation(b => b
.Field(p => p.LocationPoint)
.Origin(new GeoLocation(70, -70))
.Scale(Distance.Miles(1))
.MultiValueMode(MultiValueMode.Average)
)
.FieldValueFactor(b => b.Field(p => p.NumberOfContributors).Factor(1.1).Missing(0.1).Modifier(FieldValueFactorModifier.Square))
.RandomScore(r => r.Seed(1337).Field("_seq_no"))
.RandomScore(r => r.Seed("randomstring").Field("_seq_no"))
.Weight(1.0)
.ScriptScore(s => s.Script(ss => ss.Source("Math.log(2 + doc['numberOfCommits'].value)")))
.ScriptScore(s => s
.Script(ss => ss
.Source("Math.log(2 + doc['numberOfCommits'].value)")
)
.Weight(2)
)
)
);
}
Expand Down

0 comments on commit afd4058

Please sign in to comment.