Skip to content

Commit 92c9e6b

Browse files
committed
Function is optional for rank_feature query (#4393)
This commit updates the rank_feature query to make function an optional field, and to omit the JSON value separator when not set Fixes #4392 (cherry picked from commit df19170)
1 parent bc0b523 commit 92c9e6b

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

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

+16-12
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class RankFeatureQuery : FieldNameQueryBase, IRankFeatureQuery
2525
{
2626
protected override bool Conditionless => IsConditionless(this);
2727

28-
internal static bool IsConditionless(IRankFeatureQuery q) => q.Field.IsConditionless() || q.Function == null;
28+
internal static bool IsConditionless(IRankFeatureQuery q) => q.Field.IsConditionless();
2929

3030
internal override void InternalWrapInContainer(IQueryContainer container) => container.RankFeature = this;
3131

@@ -189,18 +189,22 @@ public void Serialize(ref JsonWriter writer, IRankFeatureQuery value, IJsonForma
189189
writer.WritePropertyName("field");
190190
var fieldFormatter = formatterResolver.GetFormatter<Field>();
191191
fieldFormatter.Serialize(ref writer, value.Field, formatterResolver);
192-
writer.WriteValueSeparator();
193-
switch (value.Function)
192+
193+
if (value.Function != null)
194194
{
195-
case IRankFeatureSigmoidFunction sigmoid:
196-
SerializeScoreFunction(ref writer, "sigmoid", sigmoid, formatterResolver);
197-
break;
198-
case IRankFeatureSaturationFunction saturation:
199-
SerializeScoreFunction(ref writer, "saturation", saturation, formatterResolver);
200-
break;
201-
case IRankFeatureLogarithmFunction log:
202-
SerializeScoreFunction(ref writer, "log", log, formatterResolver);
203-
break;
195+
writer.WriteValueSeparator();
196+
switch (value.Function)
197+
{
198+
case IRankFeatureSigmoidFunction sigmoid:
199+
SerializeScoreFunction(ref writer, "sigmoid", sigmoid, formatterResolver);
200+
break;
201+
case IRankFeatureSaturationFunction saturation:
202+
SerializeScoreFunction(ref writer, "saturation", saturation, formatterResolver);
203+
break;
204+
case IRankFeatureLogarithmFunction log:
205+
SerializeScoreFunction(ref writer, "log", log, formatterResolver);
206+
break;
207+
}
204208
}
205209

206210
writer.WriteEndObject();

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ public RankFeatureQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base
2424
q =>
2525
{
2626
q.Field = null;
27-
q.Function = null;
2827
}
2928
};
3029

3130
protected override QueryContainer QueryInitializer => new RankFeatureQuery()
3231
{
3332
Name = "named_query",
34-
Boost = 1.1,
33+
Boost = 1.1,
3534
Field = Infer.Field<Project>(f => f.Rank),
3635
Function = new RankFeatureSaturationFunction()
3736
};
@@ -47,4 +46,25 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
4746
.Saturation()
4847
);
4948
}
49+
50+
public class RankFeatureQueryNoFunctionUsageTests : QueryDslUsageTestsBase
51+
{
52+
public RankFeatureQueryNoFunctionUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
53+
protected override QueryContainer QueryInitializer => new RankFeatureQuery
54+
{
55+
Name = "named_query",
56+
Boost = 1.1,
57+
Field = Infer.Field<Project>(f => f.Rank),
58+
};
59+
60+
protected override object QueryJson =>
61+
new { rank_feature = new { _name = "named_query", boost = 1.1, field = "rank" } };
62+
63+
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
64+
.RankFeature(rf => rf
65+
.Name("named_query")
66+
.Boost(1.1)
67+
.Field(f => f.Rank)
68+
);
69+
}
5070
}

0 commit comments

Comments
 (0)