In RethinkDB using multiple indexes for tags, how to get elements that match more than one tag?
Suppose I have a story table (with example data):
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']}
The history table has a multi-index in the field tags
.
I can get all stories that have a tag plants
with:
r.table('story').getAll('plants', {index: tags})
Now how can I get all stories that have both tags plants
and in an wedding
efficient way (hopefully using a multidetective index)?
If used, it is required to selectively filter the user for an arbitrary number of arbitrary tags.
source to share
Passing multiple arguments to getAll
finds documents matching the tag:
r.table('story').getAll('plants', 'wedding', {index: 'tags'})
A simple multi-index on tags cannot be used to match all tags. A query that does not use an index would look like this:
r.table('story').filter(r.row('tags').contains('plants','wedding'))
It might be possible to create and use a multi-index on a power tag set:
r.table('story').indexCreate('tags-powerset', r.row('tags').do(powerset), {multi:true})
r.table('story').getAll(['plants', 'wedding'], {index: 'tags'})
Due to the limitations of ReQL and in the interest of efficiency, an approximation of the poweret function may be required, for example:
function(tags) {
return tags.concatMap(function(a){
tags.map(function(b){
return [a,b] })})}
source to share