RethinkDB 用时间索引两个字段
RethinkDB indexing two fields with time
我有 RethinkDB
和 data/table 让我们说 "news" 有大量的数据行:
[
{
"author": "author1",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author2",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description2",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author3",
"category_id": "sport",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description3",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author3",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description4",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
}
.....
]
我需要为 category_id
和 created_at
(timestamp
) 创建索引并查询特定类别并仅按日期(特定日期)进行过滤。我想优化和加速查询结果
我可以在 javascript
中通过像这样的过滤器为 category_id
business
和 day
15 :
r.table("news").filter({category_id: 'business'}).filter(
r.row("created_at").day().eq(15)
)
但是我如何为 category_id
和 created_at
创建索引并在 created_at
的某一天查询它。
r.table("news").indexCreate( ???
谢谢
应该是这样的:
r.table('news').indexCreate('businessAndDate', function (doc) {
return doc('category_id').add(doc('created_at').day().coerceTo('string'));
});
那么您可以:
r.table('news').getAll('business15', {index: 'businessAndDate'})
您可能希望在数据之间插入一个分隔符(如 'business-15'
),并且不仅仅使用日期(否则,为什么要使用完整的时间戳?),这取决于您以及多少.add
你愿意写。 ;)
根据the reference,这被称为任意索引。
我有 RethinkDB
和 data/table 让我们说 "news" 有大量的数据行:
[
{
"author": "author1",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author2",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description2",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author3",
"category_id": "sport",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description3",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author3",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description4",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
}
.....
]
我需要为 category_id
和 created_at
(timestamp
) 创建索引并查询特定类别并仅按日期(特定日期)进行过滤。我想优化和加速查询结果
我可以在 javascript
中通过像这样的过滤器为 category_id
business
和 day
15 :
r.table("news").filter({category_id: 'business'}).filter(
r.row("created_at").day().eq(15)
)
但是我如何为 category_id
和 created_at
创建索引并在 created_at
的某一天查询它。
r.table("news").indexCreate( ???
谢谢
应该是这样的:
r.table('news').indexCreate('businessAndDate', function (doc) {
return doc('category_id').add(doc('created_at').day().coerceTo('string'));
});
那么您可以:
r.table('news').getAll('business15', {index: 'businessAndDate'})
您可能希望在数据之间插入一个分隔符(如 'business-15'
),并且不仅仅使用日期(否则,为什么要使用完整的时间戳?),这取决于您以及多少.add
你愿意写。 ;)
根据the reference,这被称为任意索引。