Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate negative query boost (#34486) #34512

Merged
merged 1 commit into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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