Sort an Index By Date

When you want to provide chronological sorting, you need to take into account how Algolia handles dates.

For example, imagine you have a blog, and you want to create a replica to sort search results from most recent to oldest article. Because Algolia doesn’t interpret dates as ISO 8601 strings (e.g., “2008-09-15T15:53:00”), you need to convert your dates into Unix timestamps, as numeric values (e.g., 1221486780) before you can sort on them.

Modifying the data: an example

Before

Let’s say 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 "
  }
]

We want to create a replica that sorts this data by date. The problem is that the post_date attribute currently has dates formatted as strings, which Algolia can’t process for sorting. For that, you need to transform these dates as Unix timestamps.

After

Before creating a replica, you need to add the date as a Unix timestamp. You don’t have to remove or change post_date; instead, you can add a post_date_timestamp attribute with the proper format.

Note that this attribute needs to be a numeric value for Algolia to be able to sort on it.

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, you can create a replica of our articles index. We recommend naming your replica indices with a prefix/suffix describing its sorting strategy (e.g., articles_date_desc).

1
2
3
4
5
$index->setSettings([
  'replicas' => [
    'articles_date_desc'
  ]
]);

Configuring the index

Finally, you can 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'
  ]
]);

To learn more on how to create replicas with the API and the dashboard, see Creating Replicas.

Did you find this page helpful?