Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions docs/reference/migration/migrate_6_0/search.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ the `match` query but is supported for `match_phrase` and `match_phrase_prefix`.
* The deprecated multi term rewrite parameters `constant_score_auto`, `constant_score_filter` (synonyms for `constant_score`)
have been removed.

* Setting a negative `weight` in Function Score Query is deprecated.

==== Search shards API

The search shards API no longer accepts the `type` url parameter, which didn't
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

package org.elasticsearch.index.query.functionscore;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.io.stream.NamedWriteable;
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.search.function.ScoreFunction;
import org.elasticsearch.common.lucene.search.function.WeightFactorFunction;
import org.elasticsearch.common.xcontent.ToXContent.Params;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryShardContext;
Expand All @@ -34,6 +35,8 @@

public abstract class ScoreFunctionBuilder<FB extends ScoreFunctionBuilder<FB>> implements ToXContentFragment, NamedWriteable {

private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(LogManager.getLogger(ScoreFunctionBuilder.class));

private Float weight;

/**
Expand All @@ -46,7 +49,7 @@ public ScoreFunctionBuilder() {
* Read from a stream.
*/
public ScoreFunctionBuilder(StreamInput in) throws IOException {
weight = in.readOptionalFloat();
weight = checkWeight(in.readOptionalFloat());
}

@Override
Expand All @@ -70,10 +73,18 @@ public final void writeTo(StreamOutput out) throws IOException {
*/
@SuppressWarnings("unchecked")
public final FB setWeight(float weight) {
this.weight = weight;
this.weight = checkWeight(weight);
return (FB) this;
}

private Float checkWeight(Float weight) {
if (weight != null && Float.compare(weight, 0) < 0) {
DEPRECATION_LOGGER.deprecated("Setting a negative [weight] in Function Score Query is deprecated "
+ "and will throw an error in the next major version");
}
return weight;
}

/**
* The weight applied to the function before combining.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.index.query.functionscore;

import com.fasterxml.jackson.core.JsonParseException;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
Expand Down Expand Up @@ -285,6 +286,13 @@ public void testIllegalArguments() {
expectThrows(IllegalArgumentException.class, () -> builder.boostMode(null));
}

public void testDeprecatedArgumanets() {
float weight = -1 * randomFloat();
new FunctionScoreQueryBuilder.FilterFunctionBuilder(new WeightBuilder().setWeight(weight));
assertWarnings("Setting a negative [weight] in Function Score Query is deprecated "
+ "and will throw an error in the next major version");
}

public void testParseFunctionsArray() throws IOException {
String functionScoreQuery = "{\n" +
" \"function_score\":{\n" +
Expand Down