I have a case: I want to get all the connected vertex (including the middle vertex) from a base vertex.
For example, the graph as below
enter image description here
I want to query all the connected vertexes from vertex ("giggs"), and I also want to query the connected path. ex: "giggs"->"192.168.0.1"->"ronaldo"->"192.168.0.2"->"veri". I used query as below:
MATCH {class: ic, as: s, where: (title = 'giggs')}.(outE(){where: 'some condition'}.inV().inE(){where: 'some condition'}.outV()){class: %s, as: t, while: ($depth <= 5), where: ($matched.s != $currentMatch)} RETURN $paths
I can get all the target nodes, ex: "veri", but I don't know the preceding vertex of "veri" and the edge between "veri" and its preceding vertex.
So how I can write the query? Thanks in advance.
Try this:
TRAVERSE both() FROM (SELECT EXPAND(s) FROM (MATCH {CLASS:ic, AS:s, WHERE:(name='giggs')} RETURN s))
Hope it helps
Regards
Related
I want to query a collection in ArangoDB using AQL, and at each node in the query, expand the node using a traversal.
I have attempted to do this by calling the traversal as a subquery using a LET statement within the collection query.
The result set for the traversal is empty, even though the query completes.
FOR ne IN energy
FILTER ne.identifier == "12345"
LET ne_edges = (
FOR v, e IN 1..1 ANY ne relation
RETURN e
)
RETURN MERGE(ne, {"edges": ne_edges})
[
{
"value": 123.99,
"edges": []
}
]
I have verified there are edges, and the traversal returns correctly when it is not executed as a subquery.
It seems as if the initial query is completing before a result is returned from the subquery, giving the result below.
What am I missing? or is there a better way?
I can think of two way to do this. This first is easier to understand but the second is more compact. For the examples below, I have a vertex collection test2 and an edge collection testEdge that links parent and child items within test2
Using Collect:
let seed = (FOR testItem IN test2
FILTER testItem._id in ['test2/Q1', 'test2/Q3']
RETURN testItem._id)
let traversal = (FOR seedItem in seed
FOR v, e IN 1..1 ANY seedItem
testEdge
RETURN {seed: seedItem, e_to: e._to})
for t in traversal
COLLECT seeds = t.seed INTO groups = t.e_to
return {myseed: seeds, mygroups: groups}
Above we first get the items we want to traverse through (seed), then we perform the traversal and get an object that has the seed .id and the related edges
Then we finally use collect into to group the results
Using array expansion
FOR testItem IN test2
FILTER testItem._id in ['test2/Q1', 'test2/Q3']
LET testEdges = (
FOR v, e IN 1..1 ANY testItem testEdge
RETURN e
)
RETURN {myseed: testItem._id, mygroups: testEdges[*]._to}
This time we combine the seed search and the traversal by using the let statement. then we use array expansion to group items
In either case, I end up with something that looks like this:
[
{
"myseed": "test2/Q1",
"mygroups": [
"test2/Q1-P5-2",
"test2/Q1-P6-3",
"test2/Q1-P4-1"
]
},
{
"myseed": "test2/Q3",
"mygroups": [
"test2/Q3",
"test2/Q3"
]
}
]
I am using Java, and I want to get the property" name "of each vertex of the shortest path between #26:1 and #24.0 . I am using the sql command select dijkstra (#26:1,#24.0,"distance") from V. And I get the result OResultSet. I dont know how the get the rid of each vertex in my java program (I mean OVertex or ORID of each vertex : objects offered by orientdb in my java program) .
try to do it with following code:
String query3 = "SELECT dijkstra (#26:1, #28:1, 'valeur') FROM V";
OResultSet rs3 = db.query(query3);
while(rs3.hasNext()) {
OResult row = rs3.next();
String rid= row.getProperty("#rid");
}
rs3.close();
For more information, you can look for in official java-api
I hope it will help you!
How can I get depth traversal of a graph in Orientdb .
Using the documentation here is what I tried , yet when I run in I get an error here is the query .
EXPLAIN SELECT FROM (TRAVERSE any("Edge1") FROM P_H WHILE $depth <= 3) WHERE p ='SP00000000001';
The goal is the get the equivalent of this Neo4j Query :
MATCH (n:Node{NodeID:"SP00000000001"})-[:Edge1*1..3]-(d) RETURN Distinct d, n
Any help would be appreciated
The easiest thing is using a MATCH statement: http://orientdb.com/docs/2.2.x/SQL-Match.html
MATCH
{class:Node, as:n, where:(NodeID = "SP00000000001") -EdgeClass- {as:d, while:($depth < 3), where: ($matched.n != $currentMatch)} }
RETURN d, n
Or RETURN $elements if you want the vertices expanded
I need to get all list of vertices label of all outgoing egdes from a vertex using scala gremlin.
My code looks like below,
val names :ListBuffer[String] = ListBuffer()
val toList: List[Vertex] = graph.V().hasLabel(100).outE().outV().toList()
for(vertex <- toList){
names += vertex.label()
}
Its returning the same label name for all vertex
Eg :
Vertex A is having outE to B,C,D . It returns the label of A.
Output:
ListBuffer(100, 100, 100)
Anything am i missing?
I believe you asking for the wrong vertex in the end. Honestly, I often make the same mistake. Maybe this is the traversal you looking for:
graph.V().hasLabel(100).outE().inV().label().toList()
If you like me and often get confused by inV() and outV() you can use otherV which gets the opposite vertex. Like so:
graph.V().hasLabel(100).outE().otherV().label().toList()
Finally you can even shorten your traversal by not explicitly stating the edge part:
graph.V().hasLabel(100).out().label().toList()
By using out() instead of outE() you don't need to specify you want the vertex, out() gets you the vertex directly.
I'm trying to get multiple values by running an OrientDb command from Java. Specifically I am trying to get a list of Vertices that are linked to a vertex and the #rid of the Edges.
E.g If vertex V1 is linked to vertex V2 by edge E1, my query for V1 should return #rid of E1 and V2.
I can do that in Orient Studio by running the query:
select #rid, expand(in) from ExampleEdge where out = '#14:33'
How can I code the above query in Java? All the examples are showing only single value results like:
Iterable<Vertex> vertexes = graph.command(new OCommandSQL("select expand(in()) from node where #rid = '#14:33'")).execute();
I have this simple structure:
to get the RIDS, you can use this code:
String yourRid = "#12:0";
Iterable<Vertex> targets = g.command(new OSQLSynchQuery<Vertex>("select from ?")).execute(yourRid);
for (Vertex target : targets) {
Iterable<Edge> r = target.getEdges(Direction.IN, "exampleEdge");
List<Edge> results = new ArrayList<Edge>();
CollectionUtils.addAll(results, r.iterator());
System.out.println("Starting Vertex: "+yourRid);
System.out.println();
for (Edge result:results){
System.out.println("Edge "+result.getId()+" connected with Vertex "+result.getVertex(Direction.OUT).getId());
}
}
Output:
Starting Vertex: #12:0
Edge #13:3 connected with Vertex #12:1
Edge #13:4 connected with Vertex #12:2
Edge #13:5 connected with Vertex #12:3