Skip to content

Commit 43dbc78

Browse files
committed
Fix bug with random_score, script_score and weight on function_score query
random_score, script_score and weight were included in the root of the function_score query but according to the documentation at https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-function-score-query.html they should only exist as functions within the function_score query. Removed the properties and fluent methods as using them would serialize to an invalid query. Modifed unit tests and added integration test for random_score. Fixes #1559
1 parent 405d60d commit 43dbc78

File tree

8 files changed

+133
-110
lines changed

8 files changed

+133
-110
lines changed

src/Nest/DSL/Query/FunctionScoreQueryDescriptor.cs

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,6 @@ public interface IFunctionScoreQuery : IQuery
3434
[JsonProperty("max_boost")]
3535
float? MaxBoost { get; set; }
3636

37-
[JsonProperty(PropertyName = "random_score")]
38-
IRandomScoreFunction RandomScore { get; set; }
39-
40-
[JsonProperty(PropertyName = "script_score")]
41-
IScriptFilter ScriptScore { get; set; }
42-
43-
long? Weight { get; set; }
44-
45-
[JsonProperty(PropertyName = "weight")]
46-
double? WeightAsDouble { get; set; }
47-
4837
[JsonProperty(PropertyName = "min_score")]
4938
float? MinScore { get; set; }
5039

@@ -55,7 +44,6 @@ public interface IFunctionScoreQuery : IQuery
5544

5645
public class FunctionScoreQuery : PlainQuery, IFunctionScoreQuery
5746
{
58-
5947
protected override void WrapInContainer(IQueryContainer container)
6048
{
6149
container.FunctionScore = this;
@@ -69,16 +57,6 @@ protected override void WrapInContainer(IQueryContainer container)
6957
public FunctionScoreMode? ScoreMode { get; set; }
7058
public FunctionBoostMode? BoostMode { get; set; }
7159
public float? MaxBoost { get; set; }
72-
public IRandomScoreFunction RandomScore { get; set; }
73-
public IScriptFilter ScriptScore { get; set; }
74-
75-
public long? Weight
76-
{
77-
get { return Convert.ToInt64(this.WeightAsDouble ); }
78-
set { this.WeightAsDouble = value; }
79-
}
80-
81-
public double? WeightAsDouble { get; set; }
8260
public double? Boost { get; set; }
8361
public float? MinScore { get; set; }
8462
}
@@ -99,19 +77,6 @@ public class FunctionScoreQueryDescriptor<T> : IFunctionScoreQuery where T : cla
9977

10078
float? IFunctionScoreQuery.MaxBoost { get; set; }
10179

102-
IRandomScoreFunction IFunctionScoreQuery.RandomScore { get; set; }
103-
104-
IScriptFilter IFunctionScoreQuery.ScriptScore { get; set; }
105-
106-
long? IFunctionScoreQuery.Weight
107-
{
108-
get { return Convert.ToInt64(Self.WeightAsDouble ); }
109-
set { Self.WeightAsDouble = value; }
110-
}
111-
112-
// TODO: Remove in 2.0 and change Weight to double
113-
double? IFunctionScoreQuery.WeightAsDouble { get; set; }
114-
11580
float? IFunctionScoreQuery.MinScore { get; set; }
11681

11782
double? IFunctionScoreQuery.Boost { get; set; }
@@ -125,8 +90,8 @@ bool IQuery.IsConditionless
12590
get
12691
{
12792
return _forcedConditionless
128-
|| (((Self.Query == null || Self.Query.IsConditionless) && (Self.Filter == null || Self.Filter.IsConditionless))
129-
&& Self.RandomScore == null && Self.ScriptScore == null && !Self.Functions.HasAny());
93+
|| ((Self.Query == null || Self.Query.IsConditionless) && (Self.Filter == null || Self.Filter.IsConditionless)
94+
&& !Self.Functions.HasAny());
13095
}
13196
}
13297

@@ -204,38 +169,6 @@ public FunctionScoreQueryDescriptor<T> MaxBoost(float maxBoost)
204169
return this;
205170
}
206171

207-
public FunctionScoreQueryDescriptor<T> RandomScore(int? seed = null)
208-
{
209-
Self.RandomScore = new RandomScoreFunction();
210-
if (seed.HasValue)
211-
{
212-
Self.RandomScore.Seed = seed.Value;
213-
}
214-
return this;
215-
}
216-
217-
public FunctionScoreQueryDescriptor<T> ScriptScore(Action<ScriptFilterDescriptor> scriptSelector)
218-
{
219-
var descriptor = new ScriptFilterDescriptor();
220-
if (scriptSelector != null)
221-
scriptSelector(descriptor);
222-
223-
Self.ScriptScore = descriptor;
224-
225-
return this;
226-
}
227-
228-
public FunctionScoreQueryDescriptor<T> Weight(double weight)
229-
{
230-
Self.WeightAsDouble = weight;
231-
return this;
232-
}
233-
public FunctionScoreQueryDescriptor<T> Weight(long weight)
234-
{
235-
Self.Weight = weight;
236-
return this;
237-
}
238-
239172
public FunctionScoreQueryDescriptor<T> MinScore(float minScore)
240173
{
241174
Self.MinScore = minScore;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ public FunctionScoreFunction<T> Weight(double weight)
8686
return fn;
8787
}
8888

89+
public FunctionScoreFunction<T> RandomScore(int? seed = null)
90+
{
91+
var fn = new RandomScoreFunction<T>(seed);
92+
this._Functions.Add(fn);
93+
return fn;
94+
}
95+
8996
public IEnumerator<FunctionScoreFunction<T>> GetEnumerator()
9097
{
9198
return _Functions.GetEnumerator();

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66

77
namespace Nest
88
{
9+
public class RandomScoreFunction<T> : FunctionScoreFunction<T> where T : class
10+
{
11+
[JsonProperty(PropertyName = "random_score")]
12+
internal RandomScoreFunction _RandomScore { get; set; }
13+
14+
public RandomScoreFunction(int? seed = null)
15+
{
16+
var randomScore = seed.HasValue
17+
? new RandomScoreFunction(seed.Value)
18+
: new RandomScoreFunction();
19+
20+
this._RandomScore = randomScore;
21+
}
22+
}
23+
924
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
1025
[JsonConverter(typeof(ReadAsTypeConverter<RandomScoreFunction>))]
1126
public interface IRandomScoreFunction

src/Tests/Nest.Tests.Integration/Nest.Tests.Integration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
<Compile Include="Search\Filter\RangeFilterTests.cs" />
201201
<Compile Include="Search\Query\BoolQueryResults.cs" />
202202
<Compile Include="Search\Query\FilteredQueryTests.cs" />
203+
<Compile Include="Search\Query\FunctionScoreQueryTest.cs" />
203204
<Compile Include="Search\Query\SpanQueryTests.cs" />
204205
<Compile Include="Search\Query\TemplateQueryTests.cs" />
205206
<Compile Include="Search\Query\TermToString.cs" />
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Elasticsearch.Net;
4+
using FluentAssertions;
5+
using Nest.Tests.MockData;
6+
using Nest.Tests.MockData.Domain;
7+
using NUnit.Framework;
8+
9+
namespace Nest.Tests.Integration.Search.Query
10+
{
11+
[TestFixture]
12+
public class FunctionScoreQueryTest : IntegrationTests
13+
{
14+
[Test]
15+
public void RandomScore()
16+
{
17+
var results1 = Client.Search<ElasticsearchProject>(s => s
18+
.Query(q =>
19+
q.FunctionScore(fs => fs
20+
.Functions(ff => ff
21+
.RandomScore(1337)
22+
)
23+
)
24+
)
25+
.Take(1)
26+
);
27+
28+
var results2 = Client.Search<ElasticsearchProject>(s => s
29+
.Query(q =>
30+
q.FunctionScore(fs => fs
31+
.Functions(ff => ff
32+
.RandomScore(1338)
33+
)
34+
)
35+
)
36+
.Take(1)
37+
);
38+
39+
results1.IsValid.Should().BeTrue();
40+
results2.IsValid.Should().BeTrue();
41+
42+
results1.Documents.FirstOrDefault().Should().NotBeNull();
43+
results2.Documents.FirstOrDefault().Should().NotBeNull();
44+
45+
results1.Documents.First().Id.Should().NotBe(results2.Documents.First().Id);
46+
}
47+
}
48+
}

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,27 @@ public void FunctionScore_Deserializes()
2222
ff => ff.Linear(x => x.FloatValue, d => d.Scale("0.3")).Filter(lff=>Filter2).Weight(2),
2323
ff => ff.Exp(x => x.DoubleValue, d => d.Scale("0.5")).Weight(3),
2424
ff => ff.BoostFactor(2).Filter(bff=>Filter1),
25-
ff => ff.Weight(5.0).Filter(wf =>Filter1)
25+
ff => ff.Weight(5.0).Filter(wf =>Filter1),
26+
ff => ff.RandomScore(1337),
27+
ff => ff.ScriptScore(s => s
28+
.Script("My complex script")
29+
.Params(p => p.Add("param", "paramvalue"))
30+
.Lang("mvel")
31+
)
2632
)
2733
.Query(qq=>Query1)
28-
.RandomScore(1337)
2934
.ScoreMode(FunctionScoreMode.First)
30-
.ScriptScore(s=>s
31-
.Script("My complex script")
32-
.Params(p=>p.Add("param", "paramvalue"))
33-
.Lang("mvel")
34-
)
3535
)
3636
);
3737

3838
q.BoostMode.Should().Be(FunctionBoostMode.Average);
3939
q.MaxBoost.Should().Be(0.95f);
4040
q.MinScore.Should().Be(1.1f);
41-
q.RandomScore.Should().NotBeNull();
42-
q.RandomScore.Seed.Should().Be(1337);
4341
q.ScoreMode.Should().Be(FunctionScoreMode.First);
44-
q.ScriptScore.Should().NotBeNull();
45-
q.ScriptScore.Lang.Should().Be("mvel");
46-
q.ScriptScore.Script.Should().Be("My complex script");
47-
var param = q.ScriptScore.Params.FirstOrDefault();
48-
param.Should().NotBeNull();
49-
param.Key.Should().Be("param");
50-
param.Value.Should().Be("paramvalue");
51-
q.Functions.Should().NotBeEmpty().And.HaveCount(5);
5242

5343
//TODO rip out state from all these function descriptors
5444
var functions = q.Functions.ToList();
55-
functions.Should().NotBeEmpty().And.HaveCount(5);
45+
functions.Should().NotBeEmpty().And.HaveCount(7);
5646

5747

5848
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public void FunctionScoreQuery()
2626
.Modifier(FieldValueFactorModifier.SquareRoot)
2727
.Missing(1.0)
2828
.Default(0.0)
29+
),
30+
f => f.RandomScore(1337),
31+
f => f.ScriptScore(ss => ss
32+
.Script("My complex script")
33+
.Params(p => p.Add("param", "paramvalue"))
34+
.Lang("mvel")
2935
)
3036
)
3137
.ScoreMode(FunctionScoreMode.Sum)
@@ -46,7 +52,9 @@ public void FunctionScoreQuery()
4652
{linear: { floatValue : { scale: '0.3'}}},
4753
{exp: { doubleValue: { scale: '0.5'}}},
4854
{boost_factor: 2.0 },
49-
{field_value_factor: { field: 'doubleValue', factor: 2.5, modifier: 'sqrt', missing: 1.0, default: 0.0}}
55+
{field_value_factor: { field: 'doubleValue', factor: 2.5, modifier: 'sqrt', missing: 1.0, default: 0.0}},
56+
{random_score: { seed: 1337 }},
57+
{script_score: { script: 'My complex script', params: { param : 'paramvalue' }, lang: 'mvel' }}
5058
],
5159
query : { match_all : {} },
5260
score_mode: 'sum',
@@ -64,7 +72,6 @@ public void FunctionScoreQueryWithJustWeight()
6472
.Query(q => q
6573
.FunctionScore(fs => fs
6674
.Query(qq => qq.MatchAll())
67-
.Weight(2)
6875
)
6976
);
7077

@@ -73,8 +80,7 @@ public void FunctionScoreQueryWithJustWeight()
7380
from: 0, size: 10,
7481
query : {
7582
function_score : {
76-
query : { match_all : {} },
77-
weight : 2.0
83+
query : { match_all : {} }
7884
}
7985
}
8086
}";

0 commit comments

Comments
 (0)