Sort an index by date
By design, Algolia provides one ranking strategy per index: when you want to provide different rankings for the same data, you need to use different indices for each ranking. These additional indices are called replicas.
To set up sorting by attribute, you first need to understand how replica indices work.
To sort by attribute, create a replica index and then modify the ranking formula of the replica. You can do this from Algolia’s dashboard or through the API.
For chronological sorting, consider how Algolia handles dates. For example, imagine you have a blog and want to create a replica to sort search results from the most recent to the oldest article. Because Algolia doesn’t interpret dates as ISO 8601 strings (such as “2008-09-15T15:53:00”), you must convert your dates into Unix timestamps (numeric values such as 1221486780) before sorting them.
Converting dates into Unix timestamps: an example
Before
Imagine you have an index called articles
that looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[
{
"post_title": "Let's start the adventure",
"post_date": "2012-07-01",
"author_name": "Nicolas Dessaigne",
"author_image_url": "https://secure.gravatar.com/avatar/785489bc2ac2e08ae66648a8936c1101?s=40&d=mm&r=g",
"permalink": "https://blog.algolia.com/lets-start-the-adventure/",
"excerpt": "Welcome to The Algolia Blog! It's always difficult to write the first post of a blog! What should I talk about? The company, the founders, the business, the culture? And all that knowing that virtually nobody will read except diggers in a few years (hopefully)!\nLet's concentrate instead on what we'll be blogging about. Company news obviously, but not only. I expect we'll write quite a few posts about technology, algorithms, entrepreneurship, marketing, and whatever else we'll want to share with you 🙂\nAnd most important, feel free to participate in comments or by contacting us directly. We appreciate your feedback!\nWelcome to the Algolia blog!"
},
{
"post_title": "Great discussions at LeWeb'12 London",
"post_date": "2012-07-03",
"author_name": "Nicolas Dessaigne",
"author_image_url": "https://secure.gravatar.com/avatar/785489bc2ac2e08ae66648a8936c1101?s=40&d=mm&r=g",
"permalink": "https://blog.algolia.com/great-discussions-at-leweb12-london/",
"image": "https://blog.algolia.com/wp-content/uploads/2014/03/latency-360x200.png",
"excerpt": "… take long for us to decide it was the way to go, since the perception of speed is so natural that the benefit far outweighs the longer integration code. We'll now work on simplifying it!\nWe'll soon do a post about this demo. In the meantime, stay tuned!\n \n "
}
]
After
You want to create a replica that sorts your data by date. The problem is that the post_date
attribute has dates formatted as strings, which Algolia can’t process for sorting. Before creating a replica, you must transform these dates into Unix timestamps.
You don’t have to remove or change post_date
. Add a post_date_timestamp
attribute with the proper format instead.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[
{
"post_title": "Let's start the adventure",
"post_date": "2012-07-01",
"post_date_timestamp": 1341100800,
"author_name": "Nicolas Dessaigne",
"author_image_url": "https://secure.gravatar.com/avatar/785489bc2ac2e08ae66648a8936c1101?s=40&d=mm&r=g",
"permalink": "https://blog.algolia.com/lets-start-the-adventure/",
"excerpt": "Welcome to The Algolia Blog! It's always difficult to write the first post of a blog! What should I talk about? The company, the founders, the business, the culture? And all that knowing that virtually nobody will read except diggers in a few years (hopefully)!\nLet's concentrate instead on what we'll be blogging about. Company news obviously, but not only. I expect we'll write quite a few posts about technology, algorithms, entrepreneurship, marketing, and whatever else we'll want to share with you 🙂\nAnd most important, feel free to participate in comments or by contacting us directly. We appreciate your feedback!\nWelcome to the Algolia blog!"
},
{
"post_title": "Great discussions at LeWeb'12 London",
"post_date": "2012-07-03",
"post_date_timestamp": 1341273600,
"author_name": "Nicolas Dessaigne",
"author_image_url": "https://secure.gravatar.com/avatar/785489bc2ac2e08ae66648a8936c1101?s=40&d=mm&r=g",
"permalink": "https://blog.algolia.com/great-discussions-at-leweb12-london/",
"image": "https://blog.algolia.com/wp-content/uploads/2014/03/latency-360x200.png",
"excerpt": "… take long for us to decide it was the way to go, since the perception of speed is so natural that the benefit far outweighs the longer integration code. We'll now work on simplifying it!\nWe'll soon do a post about this demo. In the meantime, stay tuned!\n \n "
}
]
Creating a replica
Now, create a replica of your articles
index. The recommendation is to name your replica indices with a prefix/suffix describing its sorting strategy (for example, articles_date_desc
).
For this sorting strategy, choose a standard or a virtual replica.
Configuring standard replicas
First, create the standard replica on the primary index.
1
2
3
4
5
$index->setSettings([
'replicas' => [
'articles_date_desc'
]
]);
Then, use the post_date_timestamp
attribute to sort by date on articles_date_desc
.
1
2
3
4
5
6
7
8
9
10
11
12
13
$replicaIndex->setSettings([
'ranking' => [
'desc(post_date_timestamp)',
'typo',
'geo',
'words',
'filters',
'proximity',
'attribute',
'exact',
'custom'
]
]);
Configuring virtual replicas
First, create the virtual replica on the primary index.
1
2
3
4
5
$index->setSettings([
'replicas' => [
'virtual(articles_date_desc)'
]
]);
Then, use the post_date_timestamp
attribute to sort by date on articles_date_desc
.
1
2
3
4
5
$replicaIndex->setSettings([
'customRanking' => [
'desc(post_date_timestamp)',
]
]);