Skip to content

Commit 4fddf98

Browse files
authored
Shrink slow log for rank_feature query (#83847)
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
1 parent 6f8db84 commit 4fddf98

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilder.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* Query to run on a [rank_feature] field.
3333
*/
3434
public final class RankFeatureQueryBuilder extends AbstractQueryBuilder<RankFeatureQueryBuilder> {
35+
private static final ScoreFunction DEFAULT_SCORE_FUNCTION = new ScoreFunction.Saturation();
3536

3637
/**
3738
* Scoring function for a [rank_feature] field.
@@ -309,7 +310,7 @@ private static ScoreFunction readScoreFunction(StreamInput in) throws IOExceptio
309310
if (numNonNulls > 1) {
310311
throw new IllegalArgumentException("Can only specify one of [log], [saturation], [sigmoid] and [linear]");
311312
} else if (numNonNulls == 0) {
312-
query = new RankFeatureQueryBuilder(field, new ScoreFunction.Saturation());
313+
query = new RankFeatureQueryBuilder(field, DEFAULT_SCORE_FUNCTION);
313314
} else {
314315
ScoreFunction scoreFunction = (ScoreFunction) Arrays.stream(args, 3, args.length).filter(Objects::nonNull).findAny().get();
315316
query = new RankFeatureQueryBuilder(field, scoreFunction);
@@ -368,8 +369,10 @@ protected void doWriteTo(StreamOutput out) throws IOException {
368369
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
369370
builder.startObject(getName());
370371
builder.field("field", field);
371-
scoreFunction.doXContent(builder);
372-
printBoostAndQueryName(builder);
372+
if (false == scoreFunction.equals(DEFAULT_SCORE_FUNCTION)) {
373+
scoreFunction.doXContent(builder);
374+
}
375+
boostAndQueryNameToXContent(builder);
373376
builder.endObject();
374377
}
375378

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,21 @@ public void testIllegalCombination() {
147147
e.getMessage()
148148
);
149149
}
150+
151+
public void testParseDefaultsRemoved() throws IOException {
152+
String json = """
153+
{
154+
"rank_feature" : {
155+
"field": "foo",
156+
"boost": 1,
157+
"saturation": {}
158+
}
159+
}""";
160+
checkGeneratedJson("""
161+
{
162+
"rank_feature": {
163+
"field": "foo"
164+
}
165+
}""", parseQuery(json));
166+
}
150167
}

0 commit comments

Comments
 (0)