Skip to content

Commit

Permalink
Shrink slow log for rank_feature query (#83847)
Browse files Browse the repository at this point in the history
This removes the `boost` from the `toXContent` of `rank_feature` if it
is the default. It also removes the score function if it is the default.

Relates to #76515
  • Loading branch information
nik9000 authored Feb 11, 2022
1 parent 6f8db84 commit 4fddf98
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* Query to run on a [rank_feature] field.
*/
public final class RankFeatureQueryBuilder extends AbstractQueryBuilder<RankFeatureQueryBuilder> {
private static final ScoreFunction DEFAULT_SCORE_FUNCTION = new ScoreFunction.Saturation();

/**
* Scoring function for a [rank_feature] field.
Expand Down Expand Up @@ -309,7 +310,7 @@ private static ScoreFunction readScoreFunction(StreamInput in) throws IOExceptio
if (numNonNulls > 1) {
throw new IllegalArgumentException("Can only specify one of [log], [saturation], [sigmoid] and [linear]");
} else if (numNonNulls == 0) {
query = new RankFeatureQueryBuilder(field, new ScoreFunction.Saturation());
query = new RankFeatureQueryBuilder(field, DEFAULT_SCORE_FUNCTION);
} else {
ScoreFunction scoreFunction = (ScoreFunction) Arrays.stream(args, 3, args.length).filter(Objects::nonNull).findAny().get();
query = new RankFeatureQueryBuilder(field, scoreFunction);
Expand Down Expand Up @@ -368,8 +369,10 @@ protected void doWriteTo(StreamOutput out) throws IOException {
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(getName());
builder.field("field", field);
scoreFunction.doXContent(builder);
printBoostAndQueryName(builder);
if (false == scoreFunction.equals(DEFAULT_SCORE_FUNCTION)) {
scoreFunction.doXContent(builder);
}
boostAndQueryNameToXContent(builder);
builder.endObject();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,21 @@ public void testIllegalCombination() {
e.getMessage()
);
}

public void testParseDefaultsRemoved() throws IOException {
String json = """
{
"rank_feature" : {
"field": "foo",
"boost": 1,
"saturation": {}
}
}""";
checkGeneratedJson("""
{
"rank_feature": {
"field": "foo"
}
}""", parseQuery(json));
}
}

0 comments on commit 4fddf98

Please sign in to comment.