diff --git a/docs/reference/migration/migrate_6_0/search.asciidoc b/docs/reference/migration/migrate_6_0/search.asciidoc index fe3e046f52920..b750ce4610a84 100644 --- a/docs/reference/migration/migrate_6_0/search.asciidoc +++ b/docs/reference/migration/migrate_6_0/search.asciidoc @@ -250,3 +250,9 @@ can set the <> of the multi term q rewrite. Or, if you use `span_multi` on `prefix` query only, you can activate the <> field option of the `text` field instead. This will rewrite any prefix query on the field to a a single term query that matches the indexed prefix. + +[float] +==== Negative boosts are deprecated + +Setting a negative `boost` in a query is deprecated and will throw an error in the next version. +To deboost a specific query you can use a `boost` comprise between 0 and 1. \ No newline at end of file diff --git a/server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java index 7e667af054452..c330de507cd75 100644 --- a/server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java @@ -19,6 +19,8 @@ package org.elasticsearch.index.query; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.spans.SpanBoostQuery; @@ -29,6 +31,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.xcontent.AbstractObjectParser; import org.elasticsearch.common.xcontent.NamedObjectNotFoundException; @@ -50,6 +53,9 @@ */ public abstract class AbstractQueryBuilder> implements QueryBuilder { + private static final Logger logger = LogManager.getLogger(AbstractQueryBuilder.class); + private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger); + /** Default for boost to apply to resulting Lucene query. Defaults to 1.0*/ public static final float DEFAULT_BOOST = 1.0f; public static final ParseField NAME_FIELD = new ParseField("_name"); @@ -159,6 +165,10 @@ public final float boost() { @SuppressWarnings("unchecked") @Override public final QB boost(float boost) { + if (Float.compare(boost, 0f) < 0) { + deprecationLogger.deprecatedAndMaybeLog("negative boost", "setting a negative [boost] on a query " + + "is deprecated and will throw an error in the next version. You can use a value between 0 and 1 to deboost."); + } this.boost = boost; return (QB) this; } diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java index b2f6e6933b8fa..7fa3d934f1b27 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java @@ -77,7 +77,6 @@ import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.instanceOf; - public abstract class AbstractQueryTestCase> extends AbstractBuilderTestCase { private static final int NUMBER_OF_TESTQUERIES = 20; @@ -101,6 +100,13 @@ public final QB createTestQueryBuilder() { */ protected abstract QB doCreateTestQueryBuilder(); + public void testNegativeBoosts() { + QB testQuery = createTestQueryBuilder(); + testQuery.boost(-0.5f); + assertWarnings("setting a negative [boost] on a query" + + " is deprecated and will throw an error in the next version. You can use a value between 0 and 1 to deboost."); + } + /** * Generic test that creates new query from the test query and checks both for equality * and asserts equality on the two queries.