How do script_score and function_score work for custom relevance?

Answer

function_score wraps a query and modifies document scores using one or more scoring functions. Built-in functions include weight (multiply score by a constant), field_value_factor (use a document field value to boost score — e.g., boost by popularity count), gauss/linear/exp decay functions (score decays as a field value moves away from an origin — great for "freshness" or "proximity" boosts), and random_score (consistent random ordering per user session). script_score (simpler, ES 7+) wraps any query and allows you to write a Painless script to compute a new score from scratch using any document fields and the original _score. Use these for e-commerce ranking that combines textual relevance with business metrics like sales rank or margin.