What is the correct way to find the edge between two vertices?

Using the tinkerpop blueprints API , what is the best way to find if a border exists between two vertices? I would like to avoid vertex.getEdges() and repeat until I find the right one.

For example: check if v1 friend of v2

 Vertex v1 = g.addVertex(null); Vertex v2 = g.addVertex(null); Edge edge = g.addEdge(null, v1, v2, "friends"); Edge edge = g.addEdge(null, v1, v2, "follows"); // Node with lots of edges - Supernode - problem? List<Edge> edges = new ArrayList<Edge>(); for(Edge edge : g.getVertex(v1.getId()).getEdges(Direction.OUT, "friends")){ if(edge.getVertex(Direction.IN).getId().equals(v2.getId()){ edges.add(edge); } } 

Should I use Vertex Query ?


Through gremlin I could do:

 gv(v1.getID()).outE("friends").inV.filter{it.id == v2.getID} 

Neo4j method:

 IndexHits<Relationship> relationships = relationshipIndex().get("type", edgeType, node1, node2); 

Thanks for the help! I am still new to this.

+6
source share
3 answers
 gremlin> gv(1).bothE.as('x').bothV.retain([gv(3)]).back('x') 
+4
source

The back step used in Huangmao Quan's answer is no longer available in Tinkerpop. Since I already answered this question , the following query may apply to later versions of the tinkerpop stack.

 gV().has('propertykey','value1').outE('thatlabel').as('e').inV().has('propertykey','value2').select('e') 
+3
source

Try the following:

 if (gv(1).out('follows').retain([gv(2)]).hasNext()) { println('v(1) follows v(2)') } 

NOTE : the edge here is unidirectional, i.e. you cannot conclude whether v2 should follow v1 or not.

Source : https://groups.google.com/forum/#!msg/gremlin-users/Og7Xf8tYbx4/gqBArTw98sAJ

+1
source

Source: https://habr.com/ru/post/944080/


All Articles