Elasticsearch 8.5 Inconsistent Scoring When Using Function Score Query with Nested Fields
I'm learning this framework and I've spent hours debugging this and I'm stuck trying to After trying multiple solutions online, I still can't figure this out... I'm experiencing issues with scoring in my Elasticsearch 8.5 implementation when using a function score query on nested fields. I've set up a nested document structure for my products and their associated reviews like this: ```json { "mappings": { "properties": { "product_name": { "type": "text" }, "reviews": { "type": "nested", "properties": { "review_text": { "type": "text" }, "rating": { "type": "integer" } } } } } } ``` I'm trying to boost products based on the average rating of their reviews using a function score query. Hereโs the query I constructed: ```json { "query": { "function_score": { "query": { "match": { "product_name": "gadget" } }, "functions": [ { "filter": { "nested": { "path": "reviews", "query": { "match_all": {} } } }, "script_score": { "script": { "source": "params.ratingSum / params.reviewCount", "params": { "ratingSum": "doc['reviews.rating'].value", "reviewCount": "doc['reviews.rating'].length" } } } } ] } } } ``` However, the results I'm getting seem inconsistent. For some products, the scores are unexpectedly low despite high ratings. I verified that the ratings are correctly indexed but noticed that the script output varies. I also tried simplifying the query to just use a basic nested aggregation which returned expected results, but when I integrate this into the function score, the expected result diverges drastically. Iโve tried adjusting the script and using different ways to access the nested fields, including using `nested` aggregations instead. However, those approaches donโt yield the scoring behavior I need. I also confirmed that the nested representation is correct by running simple queries directly against the `reviews` path. The error messages I encountered were quite vague, usually just indicating that the scoring didnโt match expectations without further detail. If anyone has insights on how to properly utilize function score queries with nested fields, or if there are alternative approaches I'm missing, I would greatly appreciate your help! This is for a service running on Debian. I appreciate any insights! I'm working in a Linux environment.