gpt4 book ai didi

javascript - RethinkDB 用时间索引两个字段

转载 作者:行者123 更新时间:2023-11-30 20:16:43 24 4
gpt4 key购买 nike

我有带有数据/表的 RethinkDB 让我们说带有大量数据行的“新闻”:

[
{
"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_idcreated_at (timestamp) 创建索引并查询特定类别并按现在(特定日期)过滤仅有的。我想优化和加速查询结果

我可以在 javascript 中通过这样的过滤器为 category_id businessday 15 做到这一点:

r.table("news").filter({category_id: 'business'}).filter(
r.row("created_at").day().eq(15)
)

但是我如何为 category_idcreated_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 ,这称为任意索引。

关于javascript - RethinkDB 用时间索引两个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51852479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com