Skip to content

Commit 9eaec4d

Browse files
committed
switch from elasticsearchexception to illegalargexception when function score query logic generates bad scores
1 parent c9b2a46 commit 9eaec4d

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search/610_function_score.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,45 @@
138138
}
139139
- length: { hits.hits: 2 }
140140
- match: { hits.total.value: 2 }
141+
142+
---
143+
"formulating a function score query with a negative number returns bad request":
144+
- requires:
145+
cluster_features: [ "search.negative.function.score.bad.request" ]
146+
reason: "Testing the behaviour change with this feature"
147+
- do:
148+
indices.create:
149+
index: test
150+
body:
151+
mappings:
152+
properties:
153+
qty:
154+
type: float
155+
- do:
156+
index:
157+
index: test
158+
id: "1"
159+
body: { qty: -1, uuid: 3456 }
160+
- do:
161+
indices.refresh:
162+
index: [ test ]
163+
- do:
164+
catch: bad_request
165+
search:
166+
index: test
167+
body:
168+
query:
169+
"function_score": {
170+
"query": {
171+
"match_all": {}
172+
},
173+
"field_value_factor": {
174+
"field": "qty",
175+
"factor": 1.2,
176+
"missing": 1,
177+
"modifier": "ln1p"
178+
}
179+
}
180+
- match: { status: 400 }
181+
- match: { error.root_cause.0.type: "illegal_argument_exception" }
182+
- match: { error.root_cause.0.reason: "function score query returned an invalid score: NaN for doc: 0; score must be a non-negative real number" }

server/src/main/java/org/elasticsearch/common/lucene/search/function/FunctionScoreQuery.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ public float score() throws IOException {
448448
These scores are invalid for score based {@link org.apache.lucene.search.TopDocsCollector}s.
449449
See {@link org.apache.lucene.search.TopScoreDocCollector} for details.
450450
*/
451-
throw new ElasticsearchException("function score query returned an invalid score: " + finalScore + " for doc: " + docId);
451+
throw new IllegalArgumentException("function score query returned an invalid score: "
452+
+ finalScore + " for doc: " + docId + "; score must be a non-negative real number");
452453
}
453454
return finalScore;
454455
}

server/src/main/java/org/elasticsearch/search/SearchFeatures.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public Set<NodeFeature> getFeatures() {
3434
public static final NodeFeature BBQ_HNSW_DEFAULT_INDEXING = new NodeFeature("search.vectors.mappers.default_bbq_hnsw");
3535
public static final NodeFeature SEARCH_WITH_NO_DIMENSIONS_BUGFIX = new NodeFeature("search.vectors.no_dimensions_bugfix");
3636
public static final NodeFeature SEARCH_RESCORE_SCRIPT = new NodeFeature("search.rescore.script");
37+
public static final NodeFeature NEGATIVE_FUNCTION_SCORE_BAD_REQUEST = new NodeFeature("search.negative.function.score.bad.request");
3738

3839
@Override
3940
public Set<NodeFeature> getTestFeatures() {
@@ -45,7 +46,8 @@ public Set<NodeFeature> getTestFeatures() {
4546
MULTI_MATCH_CHECKS_POSITIONS,
4647
BBQ_HNSW_DEFAULT_INDEXING,
4748
SEARCH_WITH_NO_DIMENSIONS_BUGFIX,
48-
SEARCH_RESCORE_SCRIPT
49+
SEARCH_RESCORE_SCRIPT,
50+
NEGATIVE_FUNCTION_SCORE_BAD_REQUEST
4951
);
5052
}
5153
}

0 commit comments

Comments
 (0)