使用Gremlin查询所有Person到Person的连接(附Tinker Modern示例)
Finding All Person-to-Person Connections with Gremlin
Sure thing! If you need to pull every direct connection between Person nodes in the Tinker Modern sample graph—regardless of the edge type linking them—this Gremlin query will get you exactly what you need:
g.V().hasLabel("Person").bothE().bothV().hasLabel("Person").path().by(label())
Query Breakdown
Let’s break down each part to make it clear how this works:
g.V().hasLabel("Person"): Starts by selecting all vertices labeledPersonin the graph.bothE(): Grabs every edge connected to thesePersonvertices (covers both outgoing and incoming edges).bothV().hasLabel("Person"): Traverses to the opposite end of each edge, filtering to only keep vertices that are also labeledPerson.path().by(label()): Returns the full path for each valid connection, showing the label of each element (vertex or edge) in the sequence
Sample Results
When run against the Tinker Modern dataset, you’ll get these path results that map out all Person -> Edge -> Person relationships:
- ['Person', 'knows', 'Person']
- ['Person', 'knows', 'Person']
- ['Person', 'created', 'Person']
- ['Person', 'knows', 'Person']
- ['Person', 'knows', 'Person']
- ['Person', 'knows', 'Person']
- ['Person', 'knows', 'Person']
- ['Person', 'created', 'Person']
- ['Person', 'created', 'Person']
- ['Person', 'knows', 'Person']
- ['Person', 'knows', 'Person']
- ['Person', 'created', 'Person']
内容的提问来源于stack exchange,提问作者Srinath Ganesh
相关产品推荐
相关产品推荐

