Chapter 8. Sorting and Relevance
By default, results are returned sorted by relevance—with the most
relevant docs first. Later in this chapter, we explain what we mean by
relevance and how it is calculated, but let’s start by looking at the sort
parameter and how to use it.
Sorting
In order to sort by relevance, we need to represent relevance as a value. In
Elasticsearch, the relevance score is represented by the floating-point
number returned in the search results as the _score
, so the default sort
order is _score
descending.
Sometimes, though, you don’t have a meaningful relevance score. For instance,
the following query just returns all tweets whose user_id
field has the
value 1
:
GET
/
_search
{
"query"
:
{
"filtered"
:
{
"filter"
:
{
"term"
:
{
"user_id"
:
1
}
}
}
}
}
Filters have no bearing on _score
, and the missing-but-implied match_all
query just sets the _score
to a neutral value of 1
for all documents. In
other words, all documents are considered to be equally relevant.
Sorting by Field Values
In this case, it probably makes sense to sort tweets by recency, with the most
recent tweets first. We can do this with the sort
parameter:
GET
/
_search
{
"query"
:
{
"filtered"
:
{
"filter"
:
{
"term"
:
{
"user_id"
:
1
}}
}
},
"sort"
:
{
"date"
:
{
"order"
:
"desc"
}}
}
You will notice two differences in the results:
"hits"
:
{
"total"
:
6
,
"max_score"
:
null
,
"hits"
:
[
{
"_index" ...
Get Elasticsearch: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.