Skip to content

Function is optional for rank_feature query #4393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/Nest/QueryDsl/Specialized/RankFeature/RankFeatureQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class RankFeatureQuery : FieldNameQueryBase, IRankFeatureQuery
{
protected override bool Conditionless => IsConditionless(this);

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

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

Expand Down Expand Up @@ -189,18 +189,22 @@ public void Serialize(ref JsonWriter writer, IRankFeatureQuery value, IJsonForma
writer.WritePropertyName("field");
var fieldFormatter = formatterResolver.GetFormatter<Field>();
fieldFormatter.Serialize(ref writer, value.Field, formatterResolver);
writer.WriteValueSeparator();
switch (value.Function)

if (value.Function != null)
{
case IRankFeatureSigmoidFunction sigmoid:
SerializeScoreFunction(ref writer, "sigmoid", sigmoid, formatterResolver);
break;
case IRankFeatureSaturationFunction saturation:
SerializeScoreFunction(ref writer, "saturation", saturation, formatterResolver);
break;
case IRankFeatureLogarithmFunction log:
SerializeScoreFunction(ref writer, "log", log, formatterResolver);
break;
writer.WriteValueSeparator();
switch (value.Function)
{
case IRankFeatureSigmoidFunction sigmoid:
SerializeScoreFunction(ref writer, "sigmoid", sigmoid, formatterResolver);
break;
case IRankFeatureSaturationFunction saturation:
SerializeScoreFunction(ref writer, "saturation", saturation, formatterResolver);
break;
case IRankFeatureLogarithmFunction log:
SerializeScoreFunction(ref writer, "log", log, formatterResolver);
break;
}
}

writer.WriteEndObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ public RankFeatureQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base
q =>
{
q.Field = null;
q.Function = null;
}
};

protected override QueryContainer QueryInitializer => new RankFeatureQuery()
{
Name = "named_query",
Boost = 1.1,
Boost = 1.1,
Field = Infer.Field<Project>(f => f.Rank),
Function = new RankFeatureSaturationFunction()
};
Expand All @@ -47,4 +46,25 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
.Saturation()
);
}

public class RankFeatureQueryNoFunctionUsageTests : QueryDslUsageTestsBase
{
public RankFeatureQueryNoFunctionUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { }
protected override QueryContainer QueryInitializer => new RankFeatureQuery
{
Name = "named_query",
Boost = 1.1,
Field = Infer.Field<Project>(f => f.Rank),
};

protected override object QueryJson =>
new { rank_feature = new { _name = "named_query", boost = 1.1, field = "rank" } };

protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
.RankFeature(rf => rf
.Name("named_query")
.Boost(1.1)
.Field(f => f.Rank)
);
}
}