【问题标题】:In RethinkDB using multi indexes for tags, how to get items that match more than one tag?在 RethinkDB 中为标签使用多索引,如何获取匹配多个标签的项目?
【发布时间】:2014-10-25 11:14:00
【问题描述】:

假设我有一个故事表(带有示例数据):

 story
 {id: 1, name: 'First Story', tags: ['plants', 'flowers', 'dog']}
 {id: 2, name: 'Second Story', tags: ['flowers', 'wedding']}
 {id: 3, name: 'Third Story', tags: ['plants', 'wedding']}

故事表在tags 字段上有一个多重索引。

我可以获取所有带有plants 标签的故事:

 r.table('story').getAll('plants', {index: tags})

现在我将如何以有效的方式(希望利用标签多索引)获得同时plantswedding 标签的所有故事?

该用例需要用户对任意数量的任意标签进行可选择的过滤。

【问题讨论】:

    标签: tagging rethinkdb


    【解决方案1】:

    将多个参数传递给getAll 查找与任一标签匹配的文档:

    r.table('story').getAll('plants', 'wedding', {index: 'tags'})
    

    标签上的简单多索引不能用于匹配所有标签。不使用索引的查询如下所示:

    r.table('story').filter(r.row('tags').contains('plants','wedding'))
    

    也许可以在标签的 powerset 上创建和使用多索引:

    r.table('story').indexCreate('tags-powerset', r.row('tags').do(powerset), {multi:true})
    r.table('story').getAll(['plants', 'wedding'], {index: 'tags'})
    

    由于 ReQL 的限制,为了效率,powerset 函数可能需要近似,例如:

    function(tags) {
      return tags.concatMap(function(a){
        tags.map(function(b){
          return [a,b] })})}
    

    【讨论】:

    • 在 'tags-powerset' 示例中,我猜 getAll 应该使用 'tags-powerset' 索引?
    猜你喜欢
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2021-11-22
    • 1970-01-01
    相关资源
    最近更新 更多