从 rethinkdb 获取不同二级索引值的好方法是什么?

What is a good way of getting distinct secondary index values from rethinkdb?

假设我在 rethinkdb 中有一个 "products" table,每个产品都可以有很多标签。我想检索我系统中所有不同的标签(不添加额外的 "tags" table) 目前我正在这样做(添加了自动完成过滤):

r.table('products')
    .getAll('my.email@mail.com', { index: "email" })
    .filter(function (doc) {
      return doc.hasFields(['tags']).and(doc('tags').contains(function(tag) {
        return tag.upcase().match("^a".toUpperCase());
      }));
    })
    .pluck('tags')
    .concatMap(function (row) {
      return row('tags').filter(function(tag) {
        return tag.upcase().match("^a".toUpperCase());
      })
    })
    .distinct()

我似乎没有利用我有一个很好的索引 'email_tags' 可能已经有这些信息的事实,我想做这样的事情:

r.table('products_email_tags_index_table')
.between(['my.email@mail.com', 'A'], 
         ['my.email@mail.com', 'B'], 
         {index: 'email_tags', left_bound: 'open', right_bound: 'closed'})
.map(function(indexField) { return indexField.get(1) })

(虽然不保留外壳...)

所以我的问题是:有没有办法直接查询二级索引?

distinct 可以接受索引。这是否符合您的要求?

r.table('products_email_tags_index_table')
 .between(['my.email@mail.com', 'A'], 
          ['my.email@mail.com', 'B'], 
          {index: 'email_tags', left_bound: 'open', right_bound: 'closed'})
 .distinct({index: 'email_tags'})