我有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_id
和created_at
(timestamp
)创建索引,并查询某些类别并仅按天(确定日期)进行过滤。我想优化并加快查询结果
我可以通过javascript
category_id
和business
15这样的过滤器在day
中进行过滤:
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( ???
谢谢
答案 0 :(得分:1)
应该是这样的:
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'
),并且不仅仅使用一天(否则,为什么要花整整的时间戳记?),这取决于您以及有多少{{ 1}}您愿意写。 ;)
根据{{3}},这称为任意索引。