Skip to content

Commit 2f644af

Browse files
committed
Add linear function to rank_feature query
1 parent 422da5a commit 2f644af

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/Nest/QueryDsl/Specialized/RankFeature/RankFeatureQuery.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ public RankFeatureQueryDescriptor<T> Logarithm(Func<RankFeatureLogarithmFunction
5656
/// <inheritdoc cref="IRankFeatureSigmoidFunction"/>
5757
public RankFeatureQueryDescriptor<T> Sigmoid(Func<RankFeatureSigmoidFunctionDescriptor, IRankFeatureSigmoidFunction> selector) =>
5858
Assign(selector, (a, v) => a.Function = v?.Invoke(new RankFeatureSigmoidFunctionDescriptor()));
59+
60+
/// <inheritdoc cref="IRankFeatureLinearFunction"/>
61+
public RankFeatureQueryDescriptor<T> Linear()
62+
{
63+
Self.Function = new RankFeatureLinearFunction();
64+
return this;
65+
}
5966
}
6067

6168
/// <summary>
@@ -164,6 +171,24 @@ public class RankFeatureSigmoidFunctionDescriptor
164171
public RankFeatureSigmoidFunctionDescriptor Pivot(float pivot) => Assign(pivot, (a, v) => a.Pivot = v);
165172
}
166173

174+
/// <summary>
175+
/// Gives a score equal to the indexed value of S, where S is the value of the rank feature field.
176+
///
177+
/// If a rank feature field is indexed with "positive_score_impact": true, its indexed value is equal to S and rounded to preserve
178+
/// only 9 significant bits for the precision.
179+
///
180+
/// If a rank feature field is indexed with "positive_score_impact": false, its indexed value is equal to 1/S and rounded to
181+
/// preserve only 9 significant bits for the precision.
182+
/// </summary>
183+
public interface IRankFeatureLinearFunction : IRankFeatureFunction
184+
{
185+
}
186+
187+
/// <inheritdoc cref="IRankFeatureLinearFunction" />
188+
public class RankFeatureLinearFunction : IRankFeatureLinearFunction
189+
{
190+
}
191+
167192
internal class RankFeatureQueryFormatter : IJsonFormatter<IRankFeatureQuery>
168193
{
169194
public void Serialize(ref JsonWriter writer, IRankFeatureQuery value, IJsonFormatterResolver formatterResolver)
@@ -208,6 +233,9 @@ public void Serialize(ref JsonWriter writer, IRankFeatureQuery value, IJsonForma
208233
case IRankFeatureLogarithmFunction log:
209234
SerializeScoreFunction(ref writer, "log", log, formatterResolver);
210235
break;
236+
case IRankFeatureLinearFunction log:
237+
SerializeScoreFunction(ref writer, "linear", log, formatterResolver);
238+
break;
211239
}
212240
}
213241

@@ -234,7 +262,8 @@ private static IRankFeatureFunction DeserializeScoreFunction<TScoreFunction>(ref
234262
{ "field", 2 },
235263
{ "saturation", 3 },
236264
{ "log", 4 },
237-
{ "sigmoid", 5 }
265+
{ "sigmoid", 5 },
266+
{ "linear", 6 }
238267
};
239268

240269
public IRankFeatureQuery Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
@@ -268,6 +297,9 @@ public IRankFeatureQuery Deserialize(ref JsonReader reader, IJsonFormatterResolv
268297
case 5:
269298
query.Function = DeserializeScoreFunction<RankFeatureSigmoidFunction>(ref reader, formatterResolver);
270299
break;
300+
case 6:
301+
query.Function = DeserializeScoreFunction<RankFeatureLinearFunction>(ref reader, formatterResolver);
302+
break;
271303
}
272304
}
273305
else

tests/Tests/QueryDsl/Specialized/RankFeature/RankFeatureQueryUsageTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,26 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
7171
.Field(f => f.Rank)
7272
);
7373
}
74+
75+
public class RankFeatureLinearFunctionUsageTests : QueryDslUsageTestsBase
76+
{
77+
public RankFeatureLinearFunctionUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
78+
protected override QueryContainer QueryInitializer => new RankFeatureQuery
79+
{
80+
Name = "named_query",
81+
Boost = 1.1,
82+
Field = Infer.Field<Project>(f => f.Rank),
83+
Function = new RankFeatureLinearFunction()
84+
};
85+
86+
protected override object QueryJson =>
87+
new { rank_feature = new { _name = "named_query", boost = 1.1, field = "rank", linear = new { } } };
88+
89+
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
90+
.RankFeature(rf => rf
91+
.Name("named_query")
92+
.Boost(1.1)
93+
.Field(f => f.Rank)
94+
.Linear());
95+
}
7496
}

0 commit comments

Comments
 (0)