Reputation: 626
i'm still quite new at this so I will just go to the case.
In our application we have notes that can be shared between users and stored in folders. Single note can obviously be in multiple folders (which cannot be shared). Till now for displaying notes in folder we were using this query:
(select expand(both(\'InFolder\')) from @folder_rid)
But now, we are developing some "advanced filters" where we want to show notes that are inside specific folder or those notes which are not in in. Notes can also be tagged (by single tag) so it was quite easy using "where clause"
SELECT FROM (select expand(both(\'CanView\')) from @user_rid) where out(\'HasTag\').@rid = @tag_rid
or
SELECT FROM (select expand(both(\'CanView\')) from @user_rid) where out(\'HasTag\').@rid <> @tag_rid
And now the problem. Since note can be stored in multiple folders above approach does not work. It works when I specify on which position in this array of edges(?) target folder should be:
SELECT FROM (select expand(both(\'CanView\')) from @user_rid) where out(\'InFolder\')[0].@rid = @folder_rid
SELECT FROM (select expand(both(\'CanView\')) from @user_rid) where out(\'InFolder\')[1].@rid = @folder_rid
But obviously this is not the way to do this. I found (http://orientdb.com/orientdb-improved-sql-filtering/) that i can use ranges to do this, like
SELECT FROM (select expand(both(\'CanView\')) from @user_rid) where out(\'InFolder\')[0-2].@rid = @folder_rid
But it just calculates the value inside square brackets(oO). I also tried with [1,2,3] and using infos from https://code.google.com/p/orient/wiki/Document_Field_Part but I couldn't get it to work.
What is the correct syntax to do this or is this whole approach just bad. If problem is in the basics what is the good way to do this? Thanks for help and sorry for all mistakes I did in grammar.
Upvotes: 1
Views: 1341
Reputation: 1579
I believe the query you're looking for is:
SELECT
FROM (SELECT expand(out('CanView')) FROM <user_rid>)
WHERE <folder_rid> IN out('InFolder')
An example:
create class User extends V
create class Note extends V
create class Folder extends V
create class CanView extends E
create class InFolder extends E
create vertex User set name = 'user'
create vertex Note set name = 'note0'
create vertex Note set name = 'note1'
create vertex Note set name = 'note2'
create vertex Folder set name = 'folder0'
create vertex Folder set name = 'folder1'
create edge CanView from (select from User where name = 'user') to (select from Note where name = 'note0')
create edge CanView from (select from User where name = 'user') to (select from Note where name = 'note1')
create edge InFolder from (select from Note where name = 'note0') to (select from Folder where name = 'folder0')
create edge InFolder from (select from Note where name = 'note1') to (select from Folder where name = 'folder1')
create edge InFolder from (select from Note where name = 'note2') to (select from Folder where name = 'folder1')
There are three notes. The user can see note0 and note1. note0 is in folder0, note1 is in folder1.
Let's say we want all the notes in folder0 the user can see. The query above will return note0.
Upvotes: 2