Skip to content

Commit

Permalink
Deprecate negative query boost (#34486) (#34512)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jimczi authored Oct 16, 2018
1 parent 89ab78c commit 679bc36
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/reference/migration/migrate_6_0/search.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,9 @@ can set the <<query-dsl-multi-term-rewrite, rewrite method>> of the multi term q
rewrite. Or, if you use `span_multi` on `prefix` query only, you can activate the
<<index-prefix-config,`index_prefixes`>> 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.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -50,6 +53,9 @@
*/
public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder<QB>> 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");
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.instanceOf;


public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>> extends AbstractBuilderTestCase {

private static final int NUMBER_OF_TESTQUERIES = 20;
Expand All @@ -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.
Expand Down

0 comments on commit 679bc36

Please sign in to comment.