Skip to content

Commit 76799bb

Browse files
lipsilljimczi
authored andcommitted
Deprecate negative weight in Function Score Query (#33624)
This change issues a deprecation message for negative `weight` in Function Score query. This is a preparation for Lucene 8 (used in 7.0) where negative scores are forbidden. Closes #31927
1 parent 496b03d commit 76799bb

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

docs/reference/migration/migrate_6_0/search.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ the `match` query but is supported for `match_phrase` and `match_phrase_prefix`.
9999
* The deprecated multi term rewrite parameters `constant_score_auto`, `constant_score_filter` (synonyms for `constant_score`)
100100
have been removed.
101101

102+
* Setting a negative `weight` in Function Score Query is deprecated.
103+
102104
==== Search shards API
103105

104106
The search shards API no longer accepts the `type` url parameter, which didn't

server/src/main/java/org/elasticsearch/index/query/functionscore/ScoreFunctionBuilder.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919

2020
package org.elasticsearch.index.query.functionscore;
2121

22+
import org.apache.logging.log4j.LogManager;
2223
import org.elasticsearch.common.io.stream.NamedWriteable;
2324
import org.elasticsearch.common.io.stream.StreamInput;
2425
import org.elasticsearch.common.io.stream.StreamOutput;
26+
import org.elasticsearch.common.logging.DeprecationLogger;
2527
import org.elasticsearch.common.lucene.search.function.ScoreFunction;
2628
import org.elasticsearch.common.lucene.search.function.WeightFactorFunction;
27-
import org.elasticsearch.common.xcontent.ToXContent.Params;
2829
import org.elasticsearch.common.xcontent.ToXContentFragment;
2930
import org.elasticsearch.common.xcontent.XContentBuilder;
3031
import org.elasticsearch.index.query.QueryShardContext;
@@ -34,6 +35,8 @@
3435

3536
public abstract class ScoreFunctionBuilder<FB extends ScoreFunctionBuilder<FB>> implements ToXContentFragment, NamedWriteable {
3637

38+
private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(LogManager.getLogger(ScoreFunctionBuilder.class));
39+
3740
private Float weight;
3841

3942
/**
@@ -46,7 +49,7 @@ public ScoreFunctionBuilder() {
4649
* Read from a stream.
4750
*/
4851
public ScoreFunctionBuilder(StreamInput in) throws IOException {
49-
weight = in.readOptionalFloat();
52+
weight = checkWeight(in.readOptionalFloat());
5053
}
5154

5255
@Override
@@ -70,10 +73,18 @@ public final void writeTo(StreamOutput out) throws IOException {
7073
*/
7174
@SuppressWarnings("unchecked")
7275
public final FB setWeight(float weight) {
73-
this.weight = weight;
76+
this.weight = checkWeight(weight);
7477
return (FB) this;
7578
}
7679

80+
private Float checkWeight(Float weight) {
81+
if (weight != null && Float.compare(weight, 0) < 0) {
82+
DEPRECATION_LOGGER.deprecated("Setting a negative [weight] in Function Score Query is deprecated "
83+
+ "and will throw an error in the next major version");
84+
}
85+
return weight;
86+
}
87+
7788
/**
7889
* The weight applied to the function before combining.
7990
*/

server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.index.query.functionscore;
2121

2222
import com.fasterxml.jackson.core.JsonParseException;
23+
2324
import org.apache.lucene.index.Term;
2425
import org.apache.lucene.search.MatchAllDocsQuery;
2526
import org.apache.lucene.search.Query;
@@ -285,6 +286,13 @@ public void testIllegalArguments() {
285286
expectThrows(IllegalArgumentException.class, () -> builder.boostMode(null));
286287
}
287288

289+
public void testDeprecatedArgumanets() {
290+
float weight = -1 * randomFloat();
291+
new FunctionScoreQueryBuilder.FilterFunctionBuilder(new WeightBuilder().setWeight(weight));
292+
assertWarnings("Setting a negative [weight] in Function Score Query is deprecated "
293+
+ "and will throw an error in the next major version");
294+
}
295+
288296
public void testParseFunctionsArray() throws IOException {
289297
String functionScoreQuery = "{\n" +
290298
" \"function_score\":{\n" +

0 commit comments

Comments
 (0)