Gremlin: How do you find vertices without a particular edge?

你。 提交于 2021-02-07 13:31:48

问题


I've been looking at the Gremlin graph language, and it appears very powerful. However, whilst running through what it can do to evaluate it against requirements, I came across a case that I can't seem to complete.

Assuming Gremlin is started, and using its example database:

gremlin> g = TinkerGraphFactory.createTinkerGraph()
...
gremlin> g.V.out('knows')
==>v[2]
==>v[4]

So this shows vertices that have an edge of 'knows'.

However, I want to find vertices that do not have edges of 'knows'. Something like:

gremlin> g.V.outNot('knows')
==>v[3]
==>v[5]
==>v[6]

How do I find these vertices?

(Edited to make the output correct)


回答1:


I interpret this question several ways, but perhaps this is what you are after. One way would be to do:

gremlin> g = TinkerGraphFactory.createTinkerGraph()    
==>tinkergraph[vertices:6 edges:6]
gremlin> g.V.outE.hasNot('label','knows')
==>e[9][1-created->3]
==>e[12][6-created->3]
==>e[10][4-created->5]
==>e[11][4-created->3]
gremlin> g.V.outE.hasNot('label','knows').inV
==>v[3]
==>v[3]
==>v[5]
==>v[3]

Note that label and id are both recognized as properties:

gremlin> g.V.has('id',"1")
==>v[1]
gremlin> g.E.map("label","id")
==>{id=10, label=created}
==>{id=7, label=knows}
==>{id=9, label=created}
==>{id=8, label=knows}
==>{id=11, label=created}
==>{id=12, label=created}

Another way to consider this question would be to find a list of vertices that don't have a "knows" edge:

gremlin> g.V.filter{!it.bothE('knows').hasNext()}    
==>v[3]
==>v[6]
==>v[5]



回答2:


i am using gremlin over orientdb and this is working for me; just prefix edge label with 'out_'

g.V.hasNot('out_knows');



回答3:


If you are looking for the Groovy syntax (which can be used by AWS Neptune with their notebooks for example). You can build your query like this

g.V().not(outE('filtered_relationship'))


来源:https://stackoverflow.com/questions/22837082/gremlin-how-do-you-find-vertices-without-a-particular-edge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!