- Elasticsearch - The Definitive Guide:
- Foreword
- Preface
- Getting Started
- You Know, for Search…
- Installing Elasticsearch
- Running Elasticsearch
- Talking to Elasticsearch
- Document Oriented
- Finding Your Feet
- Indexing Employee Documents
- Retrieving a Document
- Search Lite
- Search with Query DSL
- More-Complicated Searches
- Full-Text Search
- Phrase Search
- Highlighting Our Searches
- Analytics
- Tutorial Conclusion
- Distributed Nature
- Next Steps
- Life Inside a Cluster
- Data In, Data Out
- What Is a Document?
- Document Metadata
- Indexing a Document
- Retrieving a Document
- Checking Whether a Document Exists
- Updating a Whole Document
- Creating a New Document
- Deleting a Document
- Dealing with Conflicts
- Optimistic Concurrency Control
- Partial Updates to Documents
- Retrieving Multiple Documents
- Cheaper in Bulk
- Distributed Document Store
- Searching—The Basic Tools
- Mapping and Analysis
- Full-Body Search
- Sorting and Relevance
- Distributed Search Execution
- Index Management
- Inside a Shard
- You Know, for Search…
- Search in Depth
- Structured Search
- Full-Text Search
- Multifield Search
- Proximity Matching
- Partial Matching
- Controlling Relevance
- Theory Behind Relevance Scoring
- Lucene’s Practical Scoring Function
- Query-Time Boosting
- Manipulating Relevance with Query Structure
- Not Quite Not
- Ignoring TF/IDF
- function_score Query
- Boosting by Popularity
- Boosting Filtered Subsets
- Random Scoring
- The Closer, The Better
- Understanding the price Clause
- Scoring with Scripts
- Pluggable Similarity Algorithms
- Changing Similarities
- Relevance Tuning Is the Last 10%
- Dealing with Human Language
- Aggregations
- Geolocation
- Modeling Your Data
- Administration, Monitoring, and Deployment
WARNING: The 1.x versions of Elasticsearch have passed their EOL dates. If you are running a 1.x version, we strongly advise you to upgrade.
This documentation is no longer maintained and may be removed. For the latest information, see the current Elasticsearch documentation.
function_score Query
editfunction_score Query
editThe function_score
query is the
ultimate tool for taking control of the scoring process. It allows you to
apply a function to each document that matches the main query in order to
alter or completely replace the original query _score
.
In fact, you can apply different functions to subsets of the main result set by using filters, which gives you the best of both worlds: efficient scoring with cacheable filters.
It supports several predefined functions out of the box:
-
weight
-
Apply a simple boost to each document without the boost being
normalized: a
weight
of2
results in2 * _score
. -
field_value_factor
-
Use the value of a field in the document to alter the
_score
, such as factoring in apopularity
count or number ofvotes
. -
random_score
- Use consistently random scoring to sort results differently for every user, while maintaining the same sort order for a single user.
-
Decay functions—
linear
,exp
,gauss
-
Incorporate sliding-scale values like
publish_date
,geo_location
, orprice
into the_score
to prefer recently published documents, documents near a latitude/longitude (lat/lon) point, or documents near a specified price point. -
script_score
- Use a custom script to take complete control of the scoring logic. If your needs extend beyond those of the functions in this list, write a custom script to implement the logic that you need.
Without the function_score
query, we would not be able to combine the score
from a full-text query with a factor like recency. We would have to sort
either by _score
or by date
; the effect of one would obliterate the
effect of the other. This query allows you to blend the two together: to still
sort by full-text relevance, but giving extra weight to recently published
documents, or popular documents, or products that are near the user’s price
point. As you can imagine, a query that supports all of this can look fairly
complex. We’ll start with a simple use case and work our way up the
complexity ladder.