Skip to content

Commit c39e4b8

Browse files
Add linear function to rank_feature query (#5373) (#5391)
* Add linear function to rank_feature query * Update docs and skip version in tests Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent 05b4e47 commit c39e4b8

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
@@ -54,6 +54,13 @@ public RankFeatureQueryDescriptor<T> Logarithm(Func<RankFeatureLogarithmFunction
5454
/// <inheritdoc cref="IRankFeatureSigmoidFunction"/>
5555
public RankFeatureQueryDescriptor<T> Sigmoid(Func<RankFeatureSigmoidFunctionDescriptor, IRankFeatureSigmoidFunction> selector) =>
5656
Assign(selector, (a, v) => a.Function = v?.Invoke(new RankFeatureSigmoidFunctionDescriptor()));
57+
58+
/// <inheritdoc cref="IRankFeatureLinearFunction"/>
59+
public RankFeatureQueryDescriptor<T> Linear()
60+
{
61+
Self.Function = new RankFeatureLinearFunction();
62+
return this;
63+
}
5764
}
5865

5966
/// <summary>
@@ -162,6 +169,24 @@ public class RankFeatureSigmoidFunctionDescriptor
162169
public RankFeatureSigmoidFunctionDescriptor Pivot(float pivot) => Assign(pivot, (a, v) => a.Pivot = v);
163170
}
164171

172+
/// <summary>
173+
/// Gives a score equal to the indexed value of S, where S is the value of the rank feature field.
174+
///
175+
/// If a rank feature field is indexed with "positive_score_impact": true, its indexed value is equal to S and rounded to preserve
176+
/// only 9 significant bits for the precision.
177+
///
178+
/// If a rank feature field is indexed with "positive_score_impact": false, its indexed value is equal to 1/S and rounded to
179+
/// preserve only 9 significant bits for the precision.
180+
/// </summary>
181+
public interface IRankFeatureLinearFunction : IRankFeatureFunction
182+
{
183+
}
184+
185+
/// <inheritdoc cref="IRankFeatureLinearFunction" />
186+
public class RankFeatureLinearFunction : IRankFeatureLinearFunction
187+
{
188+
}
189+
165190
internal class RankFeatureQueryFormatter : IJsonFormatter<IRankFeatureQuery>
166191
{
167192
public void Serialize(ref JsonWriter writer, IRankFeatureQuery value, IJsonFormatterResolver formatterResolver)
@@ -206,6 +231,9 @@ public void Serialize(ref JsonWriter writer, IRankFeatureQuery value, IJsonForma
206231
case IRankFeatureLogarithmFunction log:
207232
SerializeScoreFunction(ref writer, "log", log, formatterResolver);
208233
break;
234+
case IRankFeatureLinearFunction log:
235+
SerializeScoreFunction(ref writer, "linear", log, formatterResolver);
236+
break;
209237
}
210238
}
211239

@@ -232,7 +260,8 @@ private static IRankFeatureFunction DeserializeScoreFunction<TScoreFunction>(ref
232260
{ "field", 2 },
233261
{ "saturation", 3 },
234262
{ "log", 4 },
235-
{ "sigmoid", 5 }
263+
{ "sigmoid", 5 },
264+
{ "linear", 6 }
236265
};
237266

238267
public IRankFeatureQuery Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
@@ -266,6 +295,9 @@ public IRankFeatureQuery Deserialize(ref JsonReader reader, IJsonFormatterResolv
266295
case 5:
267296
query.Function = DeserializeScoreFunction<RankFeatureSigmoidFunction>(ref reader, formatterResolver);
268297
break;
298+
case 6:
299+
query.Function = DeserializeScoreFunction<RankFeatureLinearFunction>(ref reader, formatterResolver);
300+
break;
269301
}
270302
}
271303
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)