Sort an Index By Date
By design, Algolia provides one ranking formula 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.
설계상, Algolia는 인덱스당 하나의 순위 공식을 제공합니다. 동일한 데이터에 대해 다른 순위를 제공하려면 각 순위에 대해 다른 색인을 사용해야 합니다. 이러한 추가 인덱스를 복제본이라고 합니다.
If you want to set up sort by attribute it is important that you understand replica indices.
속성별로 정렬을 설정하려면 복제본 색인을 이해하는 것이 중요합니다.
To sort by attribute, you will first need to create a replica index and then modify the ranking formula of the replica. This can be done through the Dashboard and the API.
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.
속성별로 정렬하려면 먼저 복제본 색인을 작성한 후 복제본의 순위 수식을 수정해야 합니다. 이것은 Dashboard와 API를 통해 가능합니다.
연대순 정렬을 제공하려면 Algolia가 날짜를 처리하는 방식을 고려해야 합니다.
예를 들어, 블로그가 있고 가장 최근의 문서에서 가장 오래된 문서까지 검색 결과를 정렬하기 위해 복제본을 작성한다고 가정해 보십시오. Algolia는 날짜를 ISO 8601 문자열로 해석하지 않으므로(예: "2008-09-15T15:53:00") 날짜를 숫자 값(예: 1221486780)으로 변환해야 정렬할 수 있습니다.
Modifying the data: an example#
Before#
Let’s say you have an index called articles that looks like this:
[
{
"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_dateattribute currently has dates formatted as strings, which Algolia can’t process for sorting. For that, you need to transform these dates as Unix timestamps.
우리는 이 데이터를 날짜별로 정렬한 복제본을 만들고 싶습니다. 문제는 post_date 속성에는 현재 문자열로 포맷된 날짜가 있어 알골리아가 정렬을 진행할 수 없다는 것이다. 이렇게 하려면 이러한 날짜를 Unix 타임스탬프로 변환해야 합니다.
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.
복제본을 작성하기 전에 날짜를 Unix 타임스탬프로 추가해야 합니다. post_date를 제거하거나 변경할 필요가 없으며, 대신 적절한 형식으로 post_date_timestamp 특성을 추가할 수 있습니다.
이 속성은 Algolia가 정렬할 수 있는 숫자 값이어야 합니다.
[
{
"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).
For this sorting strategy, you can choose either a standard or virtual replica. To learn more on how to create replicas with the API and the dashboard, see the guide on creating replicas.
이제 articles 색인의 복제본을 만들 수 있습니다. 복제본 인덱스의 정렬 전략을 설명하는 접두사/suffix(예: reguments_date_desc)를 사용하여 복제본 인덱스의 이름을 지정하는 것이 좋습니다.
이 정렬 전략에서 표준 복제본 또는 가상 복제본을 선택할 수 있습니다. API 및 대시보드를 사용하여 복제본을 만드는 방법에 대한 자세한 내용은 복제본 작성 안내서를 참조하십시오.
Configuring standard replicas
You first need to create the standard replica on the primary index.
먼저 기본 색인에 표준 복제본을 작성해야 합니다.
index.setSettings({
replicas: [
'articles_date_desc'
]
}).then(() => {
// done
});
Then, you can use the post_date_timestamp attribute to sort by date on articles_date_desc.
그런 다음 post_date_timestamp 특성을 사용하여 reguments_date_desc에서 날짜별로 정렬할 수 있습니다.
replicaIndex.setSettings({
ranking: [
"desc(post_date_timestamp)",
"typo",
"geo",
"words",
"filters",
"proximity",
"attribute",
"exact",
"custom"
]
}).then(() => {
// done
});
Configuring virtual replicas#
You first need to create the virtual replica on the primary index.먼저 기본 인덱스에 가상 복제본을 만들어야 합니다.
index.setSettings({
replicas: [
'virtual(articles_date_desc)'
]
}).then(() => {
// done
});
Then, you can use the post_date_timestamp attribute to sort by date on articles_date_desc.
그런 다음 post_date_timestamp 특성을 사용하여 reguments_date_desc에서 날짜별로 정렬할 수 있습니다.
replicaIndex.setSettings({
customRanking: [
"desc(post_date_timestamp)",
]
}).then(() => {
// done
});
'개발 > ALGOLIA' 카테고리의 다른 글
알고리아 Managing Results(3-15) (0) | 2022.03.04 |
---|---|
알고리아 Managing Results(3-14) (0) | 2022.03.04 |
알고리아 Managing Results(3-12) (0) | 2022.03.03 |
알고리아 Managing Results(3-11) (0) | 2022.03.03 |
알고리아 Managing Results(3-10) (0) | 2022.03.03 |