RethinkDB随时间为两个字段建立索引

时间:2018-08-15 03:16:57

标签: javascript indexing rethinkdb rethinkdb-javascript

我有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_attimestamp)创建索引,并查询某些类别并仅按天(确定日期)进行过滤。我想优化并加快查询结果

我可以通过javascript category_idbusiness 15这样的过滤器在day中进行过滤:

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

但是我如何为category_idcreated_at创建索引并在created_at的某一天进行查询。

r.table("news").indexCreate( ???

谢谢

1 个答案:

答案 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}},这称为任意索引。