To set up sorting by attribute, you first need to understand how replica indices work.
Convert dates into Unix timestamps: an example
Before
Say you have an index calledarticles
:
JSON
After
You want to create a replica that sorts your data by date. The problem is that thepost_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.
JSON
Create a replica
Now, create a replica of yourarticles
index.
The recommendation is to name your replica indices with a prefix or suffix describing its sorting strategy (for example, articles_date_desc
).
First, create a standard or virtual replica (articles_date_desc
) from the primary index.
The primary index is likely sorted by relevance but you want to change that for articles_date_desc
.
post_date_timestamp
attribute to sort the articles_date_desc
virtual index by date in descending order: