Skip to content

Commit d8dc456

Browse files
Add linear function to rank_feature query (#5373) (#5390)
* Add linear function to rank_feature query * Update docs and skip version in tests Co-authored-by: Steve Gordon <[email protected]>
1 parent 067eecb commit d8dc456

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

docs/query-dsl/specialized/rank-feature/rank-feature-query-usage.asciidoc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,41 @@ new RankFeatureQuery
9999
}
100100
----
101101

102+
==== Fluent DSL example
103+
104+
[source,csharp]
105+
----
106+
q
107+
.RankFeature(rf => rf
108+
.Name("named_query")
109+
.Boost(1.1)
110+
.Field(f => f.Rank)
111+
.Linear())
112+
----
113+
114+
==== Object Initializer syntax example
115+
116+
[source,csharp]
117+
----
118+
new RankFeatureQuery
119+
{
120+
Name = "named_query",
121+
Boost = 1.1,
122+
Field = Infer.Field<Project>(f => f.Rank),
123+
Function = new RankFeatureLinearFunction()
124+
}
125+
----
126+
127+
[source,javascript]
128+
.Example json output
129+
----
130+
{
131+
"rank_feature": {
132+
"_name": "named_query",
133+
"boost": 1.1,
134+
"field": "rank",
135+
"linear": {}
136+
}
137+
}
138+
----
139+

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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
56
using Nest;
67
using Tests.Core.ManagedElasticsearch.Clusters;
78
using Tests.Domain;
@@ -71,4 +72,27 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
7172
.Field(f => f.Rank)
7273
);
7374
}
75+
76+
[SkipVersion("<7.12.0", "Introduced in 7.12.0")]
77+
public class RankFeatureLinearFunctionUsageTests : QueryDslUsageTestsBase
78+
{
79+
public RankFeatureLinearFunctionUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
80+
protected override QueryContainer QueryInitializer => new RankFeatureQuery
81+
{
82+
Name = "named_query",
83+
Boost = 1.1,
84+
Field = Infer.Field<Project>(f => f.Rank),
85+
Function = new RankFeatureLinearFunction()
86+
};
87+
88+
protected override object QueryJson =>
89+
new { rank_feature = new { _name = "named_query", boost = 1.1, field = "rank", linear = new { } } };
90+
91+
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
92+
.RankFeature(rf => rf
93+
.Name("named_query")
94+
.Boost(1.1)
95+
.Field(f => f.Rank)
96+
.Linear());
97+
}
7498
}

0 commit comments

Comments
 (0)