Skip to content

Conversation

@john-wagster
Copy link
Contributor

When the user forms up a function score query that will cause an exception in the scoring logic we should just inform them instead of failing:

For instance for something like:

curl -XPUT --header 'Content-Type: application/json' "http://localhost:9200/test" -d '{
  "mappings" : {
    "properties": {
      "sell_qty": {
        "type": "integer"
      }
    }
  }
}'

curl -XPUT --header 'Content-Type: application/json' "http://localhost:9200/test/_doc/1?refresh" -d '{
  "sell_qty": -100
}'

curl -XGET --header 'Content-Type: application/json' "http://localhost:9200/test/_search" -d '{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "field_value_factor": {
        "field": "sell_qty",
        "factor": 1.2,
        "missing": 1,
        "modifier": "ln1p"
      }
    }
  }
}' | python -mjson.tool

we return

{
    "error": {
        "root_cause": [
            {
                "type": "exception",
                "reason": "function score query returned an invalid score: NaN for doc: 0; score must be a non-negative real number"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "test",
                "node": "rFxTg8RgTMC4yztbQlhK_g",
                "reason": {
                    "type": "exception",
                    "reason": "function score query returned an invalid score: NaN for doc: 0; score must be a non-negative real number"
                }
            }
        ]
    },
    "status": 500
}

but should instead throw back an IllegalArgumentException as it's a user provided error to pass a function that will generate negative scores.

Relevant stack:

caused by: org.elasticsearch.ElasticsearchException: function score query returned an invalid score: NaN for doc: 0
	at org.elasticsearch.server@9.2.0/org.elasticsearch.common.lucene.search.function.FunctionScoreQuery$FunctionFactorScorer.score(FunctionScoreQuery.java:451)
	at org.apache.lucene.core@10.2.2/org.apache.lucene.search.FilterScorable.score(FilterScorable.java:45)
	at org.apache.lucene.core@10.2.2/org.apache.lucene.search.ConjunctionBulkScorer$1.score(ConjunctionBulkScorer.java:64)
	at org.apache.lucene.core@10.2.2/org.apache.lucene.search.ScoreCachingWrappingScorer.score(ScoreCachingWrappingScorer.java:86)
	at org.apache.lucene.core@10.2.2/org.apache.lucene.search.FilterScorable.score(FilterScorable.java:45)
	at org.apache.lucene.core@10.2.2/org.apache.lucene.search.TopScoreDocCollector$1.collect(TopScoreDocCollector.java:106)
	at org.apache.lucene.core@10.2.2/org.apache.lucene.search.MultiCollector$MultiLeafCollector.collect(MultiCollector.java:221)
...

@john-wagster john-wagster added >bug :Search Relevance/Search Catch all for Search Relevance v9.2.0 labels Aug 21, 2025
@john-wagster john-wagster requested a review from a team August 21, 2025 19:15
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-relevance (Team:Search Relevance)

@elasticsearchmachine elasticsearchmachine added the Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch label Aug 21, 2025
@elasticsearchmachine
Copy link
Collaborator

Hi @john-wagster, I've created a changelog YAML for you.

@john-wagster
Copy link
Contributor Author

john-wagster commented Aug 21, 2025

Keen for opinions here. I was looking at some failures around this and it seemed like IllegalArgumentException made the most sense but not 100% sure.

Copy link
Member

@benwtrent benwtrent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! This should return a bad request.

@john-wagster john-wagster merged commit 94648e4 into elastic:main Aug 24, 2025
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>bug :Search Relevance/Search Catch all for Search Relevance Team:Search Relevance Meta label for the Search Relevance team in Elasticsearch v9.2.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Switch to Sending a Bad Request User When Function Score Query Generates Negative Scores

3 participants