diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilder.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilder.java index 2b3ec09abdfd7..25f0786534110 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilder.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilder.java @@ -32,6 +32,7 @@ * Query to run on a [rank_feature] field. */ public final class RankFeatureQueryBuilder extends AbstractQueryBuilder { + private static final ScoreFunction DEFAULT_SCORE_FUNCTION = new ScoreFunction.Saturation(); /** * Scoring function for a [rank_feature] field. @@ -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); @@ -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(); } diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java index c4e7e35c646a1..4bd2d0714e93d 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java @@ -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)); + } }