Skip to content

Commit 544de13

Browse files
authored
Disallow negative query boost (#34486)
This change disallows negative query boosts. Negative scores are not allowed in Lucene 8 so it is easier to just disallow negative boosts entirely. We should also deprecate negative boosts in 6x in order to ensure that users are aware when they'll upgrade to ES 7. Relates #33309
1 parent 4b2052c commit 544de13

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

docs/reference/migration/migrate_7_0/search.asciidoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Scroll queries are not meant to be cached.
6161

6262
[float]
6363
==== Scroll queries cannot use `rescore` anymore
64+
6465
Including a rescore clause on a query that creates a scroll (`scroll=1m`) has
6566
been deprecated in 6.5 and will now return a `400 - Bad request`. Allowing
6667
rescore on scroll queries would break the scroll sort. In the 6.x line, the
@@ -123,3 +124,9 @@ max number of concurrent shard requests per node. The default is now `5`.
123124

124125
`max_score` used to be set to `0` whenever scores are not tracked. `null` is now used
125126
instead which is a more appropriate value for a scenario where scores are not available.
127+
128+
[float]
129+
==== Negative boosts are not allowed
130+
131+
Setting a negative `boost` in a query, deprecated in 6x, are not allowed in this version.
132+
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ public final float boost() {
159159
@SuppressWarnings("unchecked")
160160
@Override
161161
public final QB boost(float boost) {
162+
if (Float.compare(boost, 0f) < 0) {
163+
throw new IllegalArgumentException("negative [boost] are not allowed in [" + toString() + "], " +
164+
"use a value between 0 and 1 to deboost");
165+
}
162166
this.boost = boost;
163167
return (QB) this;
164168
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ public final QB createTestQueryBuilder() {
101101
*/
102102
protected abstract QB doCreateTestQueryBuilder();
103103

104+
public void testNegativeBoosts() {
105+
QB testQuery = createTestQueryBuilder();
106+
IllegalArgumentException exc =
107+
expectThrows(IllegalArgumentException.class, () -> testQuery.boost(-0.5f));
108+
assertThat(exc.getMessage(), containsString("negative [boost]"));
109+
}
110+
104111
/**
105112
* Generic test that creates new query from the test query and checks both for equality
106113
* and asserts equality on the two queries.

0 commit comments

Comments
 (0)