Skip to content

Commit 0132a9c

Browse files
author
Daniel Low
committed
Add 'missing' field to specify what value to use in case the field in field_value_factor does not exist in the document.
From http://www.elastic.co/guide/en/elasticsearch/reference/1.x/query-dsl-function-score-query.html
1 parent 13f820c commit 0132a9c

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class FieldValueFactorDescriptor<T>
3737
[JsonConverter(typeof(StringEnumConverter))]
3838
internal FieldValueFactorModifier? _Modifier { get; set; }
3939

40+
[JsonProperty("missing")]
41+
internal double? _Missing { get; set; }
42+
4043
public FieldValueFactorDescriptor<T> Field(Expression<Func<T, object>> field)
4144
{
4245
this._Field = field;
@@ -54,5 +57,11 @@ public FieldValueFactorDescriptor<T> Modifier(FieldValueFactorModifier modifier)
5457
this._Modifier = modifier;
5558
return this;
5659
}
60+
61+
public FieldValueFactorDescriptor<T> Missing(double missing)
62+
{
63+
this._Missing = missing;
64+
return this;
65+
}
5766
}
5867
}

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ 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")),
17+
f => f.Weight(3.0).Filter(ff => ff.Term(p => p.Name, "elasticsearch")),
1818
f => f.Gauss(x => x.StartedOn, d => d.Scale("42w")),
1919
f => f.Linear(x => x.FloatValue, d => d.Scale("0.3")),
2020
f => f.Exp(x => x.DoubleValue, d => d.Scale("0.5")),
2121
f => f.BoostFactor(2.0),
22-
f => f.FieldValueFactor(op => op.Field(ff => ff.DoubleValue).Factor(2.5).Modifier(FieldValueFactorModifier.SquareRoot))
22+
f => f.FieldValueFactor(op => op.Field(ff => ff.DoubleValue).Factor(2.5).Modifier(FieldValueFactorModifier.SquareRoot).Missing(1.0))
2323
)
2424
.ScoreMode(FunctionScoreMode.Sum)
2525
.BoostMode(FunctionBoostMode.Replace)
@@ -28,21 +28,21 @@ public void FunctionScoreQuery()
2828

2929
var json = TestElasticClient.Serialize(s);
3030
var expected = @"{
31-
from: 0, size: 10,
32-
fields: [""content""],
31+
from: 0, size: 10,
32+
fields: [""content""],
3333
query : {
34-
function_score : {
35-
functions: [
34+
function_score : {
35+
functions: [
3636
{weight: 3.0, filter: { term: { 'name': 'elasticsearch' }}},
37-
{gauss: { startedOn : { scale: '42w'}}},
38-
{linear: { floatValue : { scale: '0.3'}}},
39-
{exp: { doubleValue: { scale: '0.5'}}},
40-
{boost_factor: 2.0 },
41-
{field_value_factor: { field: 'doubleValue', factor: 2.5, modifier: 'sqrt'}}
42-
],
37+
{gauss: { startedOn : { scale: '42w'}}},
38+
{linear: { floatValue : { scale: '0.3'}}},
39+
{exp: { doubleValue: { scale: '0.5'}}},
40+
{boost_factor: 2.0 },
41+
{field_value_factor: { field: 'doubleValue', factor: 2.5, modifier: 'sqrt', missing: 1.0}}
42+
],
4343
query : { match_all : {} },
44-
score_mode: 'sum',
45-
boost_mode: 'replace',
44+
score_mode: 'sum',
45+
boost_mode: 'replace',
4646
}
4747
}
4848
}";
@@ -62,9 +62,9 @@ public void FunctionScoreQueryWithJustWeight()
6262

6363
var json = TestElasticClient.Serialize(s);
6464
var expected = @"{
65-
from: 0, size: 10,
65+
from: 0, size: 10,
6666
query : {
67-
function_score : {
67+
function_score : {
6868
query : { match_all : {} },
6969
weight : 2.0
7070
}
@@ -85,7 +85,7 @@ public void FunctionScoreQueryConditionless()
8585
f => f.Linear(x => x.FloatValue, d => d.Scale("0.3")),
8686
f => f.Exp(x => x.DoubleValue, d => d.Scale("0.5")),
8787
f => f.BoostFactor(2),
88-
f => f.FieldValueFactor(db => db.Field(fa => fa.DoubleValue).Factor(3.4).Modifier(FieldValueFactorModifier.Ln))
88+
f => f.FieldValueFactor(db => db.Field(fa => fa.DoubleValue).Factor(3.4).Modifier(FieldValueFactorModifier.Ln).Missing(1.4))
8989
)
9090
.ScoreMode(FunctionScoreMode.Sum)
9191
.BoostMode(FunctionBoostMode.Replace)
@@ -94,19 +94,19 @@ public void FunctionScoreQueryConditionless()
9494

9595
var json = TestElasticClient.Serialize(s);
9696
var expected = @"{
97-
from: 0, size: 10,
98-
fields: [""content""],
97+
from: 0, size: 10,
98+
fields: [""content""],
9999
query : {
100-
function_score : {
101-
functions: [
102-
{gauss: { startedOn : { scale: '42w'}}},
103-
{linear: { floatValue : { scale: '0.3'}}},
104-
{exp: { doubleValue: { scale: '0.5'}}},
105-
{boost_factor: 2.0 },
106-
{field_value_factor: { field: 'doubleValue', factor: 3.4, modifier: 'ln'}}
107-
],
108-
score_mode: 'sum',
109-
boost_mode: 'replace',
100+
function_score : {
101+
functions: [
102+
{gauss: { startedOn : { scale: '42w'}}},
103+
{linear: { floatValue : { scale: '0.3'}}},
104+
{exp: { doubleValue: { scale: '0.5'}}},
105+
{boost_factor: 2.0 },
106+
{field_value_factor: { field: 'doubleValue', factor: 3.4, modifier: 'ln', missing: 1.4}}
107+
],
108+
score_mode: 'sum',
109+
boost_mode: 'replace',
110110
}
111111
}
112112
}";

0 commit comments

Comments
 (0)