Skip to content

Commit 679bc36

Browse files
authored
Deprecate negative query boost (#34486) (#34512)
This change deprecates negative query boosts. Negative scores are not allowed in Lucene 8 so it is easier to just disallow negative boosts entirely. Relates #33309
1 parent 89ab78c commit 679bc36

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

docs/reference/migration/migrate_6_0/search.asciidoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,9 @@ can set the <<query-dsl-multi-term-rewrite, rewrite method>> of the multi term q
250250
rewrite. Or, if you use `span_multi` on `prefix` query only, you can activate the
251251
<<index-prefix-config,`index_prefixes`>> field option of the `text` field instead. This will
252252
rewrite any prefix query on the field to a a single term query that matches the indexed prefix.
253+
254+
[float]
255+
==== Negative boosts are deprecated
256+
257+
Setting a negative `boost` in a query is deprecated and will throw an error in the next version.
258+
To deboost a specific query you can use a `boost` comprise between 0 and 1.

server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package org.elasticsearch.index.query;
2121

22+
import org.apache.logging.log4j.LogManager;
23+
import org.apache.logging.log4j.Logger;
2224
import org.apache.lucene.search.BoostQuery;
2325
import org.apache.lucene.search.Query;
2426
import org.apache.lucene.search.spans.SpanBoostQuery;
@@ -29,6 +31,7 @@
2931
import org.elasticsearch.common.Strings;
3032
import org.elasticsearch.common.io.stream.StreamInput;
3133
import org.elasticsearch.common.io.stream.StreamOutput;
34+
import org.elasticsearch.common.logging.DeprecationLogger;
3235
import org.elasticsearch.common.lucene.BytesRefs;
3336
import org.elasticsearch.common.xcontent.AbstractObjectParser;
3437
import org.elasticsearch.common.xcontent.NamedObjectNotFoundException;
@@ -50,6 +53,9 @@
5053
*/
5154
public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder<QB>> implements QueryBuilder {
5255

56+
private static final Logger logger = LogManager.getLogger(AbstractQueryBuilder.class);
57+
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger);
58+
5359
/** Default for boost to apply to resulting Lucene query. Defaults to 1.0*/
5460
public static final float DEFAULT_BOOST = 1.0f;
5561
public static final ParseField NAME_FIELD = new ParseField("_name");
@@ -159,6 +165,10 @@ public final float boost() {
159165
@SuppressWarnings("unchecked")
160166
@Override
161167
public final QB boost(float boost) {
168+
if (Float.compare(boost, 0f) < 0) {
169+
deprecationLogger.deprecatedAndMaybeLog("negative boost", "setting a negative [boost] on a query " +
170+
"is deprecated and will throw an error in the next version. You can use a value between 0 and 1 to deboost.");
171+
}
162172
this.boost = boost;
163173
return (QB) this;
164174
}

test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
import static org.hamcrest.Matchers.greaterThan;
7878
import static org.hamcrest.Matchers.instanceOf;
7979

80-
8180
public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>> extends AbstractBuilderTestCase {
8281

8382
private static final int NUMBER_OF_TESTQUERIES = 20;
@@ -101,6 +100,13 @@ public final QB createTestQueryBuilder() {
101100
*/
102101
protected abstract QB doCreateTestQueryBuilder();
103102

103+
public void testNegativeBoosts() {
104+
QB testQuery = createTestQueryBuilder();
105+
testQuery.boost(-0.5f);
106+
assertWarnings("setting a negative [boost] on a query" +
107+
" is deprecated and will throw an error in the next version. You can use a value between 0 and 1 to deboost.");
108+
}
109+
104110
/**
105111
* Generic test that creates new query from the test query and checks both for equality
106112
* and asserts equality on the two queries.

0 commit comments

Comments
 (0)