What is the best way to retrieve multiple connected vertexes as a nested object?

202 views
Skip to first unread message

Eric Lin

unread,
Sep 5, 2015, 5:34:29 AM9/5/15
to orient-...@googlegroups.com
Hi everyone,

Pretty new with orientdb and I can't find any examples regarding my current situation.
I'm using OrientDB version 2.0.11. Also, since I'm working with node, I'm using the official js library here https://github.com/orientechnologies/orientjs. Though, that shouldn't really matter since I can manually write full queries myself.

Suppose I have three vertexes

first_node(#11:1) --hasEdge--> second_node --hasEdge--> third_node

I would like to query in such a way that the result looks like


{
  first_node
: {
    properties
of first_node...,
    second_node
: [{
      properties of second_node
...,
      third_node
: [{
        properties of third_node
...
     
}, {
        another third node
      }, {
        another third node
      }]
   
}, {
      another second node
    }, {
      another second node
    }]
  }
}


Now, I can get one level deep using a Fetchplan and it works great:

SELECT *, out('hasEdge') as first_edge FROM #11:1 FETCHPLAN first_edge:1

This returns me

{
  first_node
: {
    first_node properties
...,
    first_edge
: [{
      second_node properties
...
   
}, {
      other second node
    }, 
    ...
    ]
 
}
}

How do I extend this out another level? I've tried stuff like

SELECT *, out('hasEdge').out('hasEdge') as first_edge FROM #11:1 FETCHPLAN first_edge:2

But I end up only getting the the third_node properties in the `first_edge` key.

Thanks in advance.

Cheers,
Eric

user.w...@gmail.com

unread,
Sep 7, 2015, 2:26:50 AM9/7/15
to OrientDB
Hi Eric,

Try this:

traverse out("hasEdge") as FirstEdge from #12:0 strategy DEPTH_FIRST

Regards,
Michela

Eric Lin

unread,
Sep 7, 2015, 3:20:31 AM9/7/15
to OrientDB
Hi Michela,

traverse gives the results out as an array of objects which are the vertexes it traverses through, it doesn't give me the nested object structure that I was hoping for.

Of course, I could use these nodes retrieved and manually match the in and out vertex @rids to build my nested object but I feel that there should be a better way to do this?

Plus I can get nested objects 1 level deep with FETCHPLAN so I imagine there should be a way to get nested objects 2 levels deep in a similar fashion...

Thanks,
Eric

user.w...@gmail.com

unread,
Sep 7, 2015, 4:31:05 AM9/7/15
to OrientDB
Hi Eric,

Sorry my bad :)

Try this:

SELECT *, out('hasEdge') as FirstEdge FROM #12:0 FETCHPLAN FirstEdge:-1 *:-1

Regards,
Michela

Eric Lin

unread,
Sep 7, 2015, 1:03:16 PM9/7/15
to OrientDB
Hi Michela,

Trying that out in Orient Studio, it's still only retrieving nodes 1 level deep / 1 step away, it's not getting the vertexes two steps away and it's also not returning properties of the adjacent vertexes it's only returning the @rid's... :(

However, I have noticed in Orient Studio in the PROPERTIES column it only shows the @rids but in the OUT column it shows nested objects which means that they must have been pre-fetched ? But we're just not attaching those prefetched results to our PROPERTIES?

Thanks for all the help,
Eric

Eric Lin

unread,
Sep 8, 2015, 8:08:13 PM9/8/15
to orient-...@googlegroups.com
So I haven't found a good solution to this, as of now I'm making this query:

SELECT *, out('hasEdge') as first_vertexes, out('hasEdge').out('hasEdge') as second_vertexes FROM #11:1 FETCHPLAN first_vertexes:1 second_vertexes:1

and in the resulting rows that I get I manually match the the in's and the out's @rids for the first and second vertexes and building the nested object before sending it in the json response.

Seems like there must be a better way but I can't have this be a blocker for me so... temporary solution until someone can figure something else out.

Cheers,
Eric

Geoff Goodman

unread,
Sep 24, 2015, 11:30:35 AM9/24/15
to orient-...@googlegroups.com
Try this:

SELECT *, out('hasEdge')[0] as second_node FRPM #11:1 FETCHPLAN second_vertexes:0

This is partly from memory and partly from a query I built to achieve something similar to what you're looking for.

Geoff

Eric Lin

unread,
Sep 24, 2015, 1:05:56 PM9/24/15
to OrientDB
Hey Geoff,

I gave that a shot and that gives me the first item and nested nodes one step away only. I need nested nodes up to two steps away with a given edge.

so if we have

root_node --> first_child --> second_child

That query gives me the 'root_node' with the nested 'first_child'. Doesn't give me the nested 'second_child's inside the 'first_child'.

Any ideas there?

Cheers,
Eric

Geoff Goodman

unread,
Sep 24, 2015, 2:19:43 PM9/24/15
to OrientDB
I would suggest experimenting with the pattern to see how it could be expanded to a 2nd level.

--

---
You received this message because you are subscribed to a topic in the Google Groups "OrientDB" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/orient-database/qfzP4gE4vwU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alejandro prado

unread,
Sep 24, 2015, 4:31:32 PM9/24/15
to OrientDB
We wrote a recursive query as a server side JavaScript function that takes the parent "rid" as a parameter. Have not tested the performance but seems powerful and flexible, since as you traverse down you get to analyze your current results.


var db = orient.getGraph();
var root;
var roorArr;
var children = [];
var cmd;


cmd = "select from  #"+ rid;
rootArr = db.command("sql",cmd,[]);
root = rootArr[0];
root = JSON.parse(root.getRecord().toJSON());
root.children = [];
children.push(root);
queryChildren(root.children, "#" + rid);
function queryChildren(childrenArray, rid) {
var arr;
var record;
var child;
arr = NavTreeGetChildrenByFullId(rid);
for (var j = 0; j < arr.length; j++) {
record = arr[ j ];
child = {};
child = JSON.parse(record.getRecord().toJSON());
child.children = [];
childrenArray.push(child);

queryChildren(child.children, child[ "@rid" ]);
}
}
return children;
Reply all
Reply to author
Forward
0 new messages