简体   繁体   中英

How to get all vertices connected to at least two other vertices in a group using gremlin?

I am pretty new to GraphDBs so excuse me if this is trivial.

I have two groups of vertices in an Azure CosmosDB using gremlin. Group a and b . Every vertex of group a can connect to any vertex of group b , once at most. I am looking to find every vertex in group b that is connected to at least two vertices in group a .

在此处输入图片说明

So in this example I would like to get back vertices [b2, b3] .

In case it's relevant: There would usually be a lot more vertices in group b

Creating a graph from your diagram, you can do something like this

g.addV('person').property('group','A').property('name','a1').as('a1').
    addV('person').property('group','A').property('name','a2').as('a2').
    addV('person').property('group','A').property('name','a3').as('a3').
    addV('person').property('group','B').property('name','b1').as('b1').
    addV('person').property('group','B').property('name','b2').as('b2').
    addV('person').property('group','B').property('name','b3').as('b3').   
    addV('person').property('group','B').property('name','b4').as('b4').
    addE('knows').from('b3').to('a2').
    addE('knows').from('b3').to('a3').
    addE('knows').from('a2').to('b2').
    addE('knows').from('b2').to('a1').
    addE('knows').from('a1').to('b1').
    addE('knows').from('a3').to('b4').iterate()          

gremlin>   g.V().has('group','B').
......1>         filter(both().has('group','A').count().is(gte(2))).
......2>         values('name')  
==>b2
==>b3     

If there is a possibility that any of the vertices may have a lot of connected edges it is probably more efficient to change the test part of the query to be.

filter(both().has('group','A').limit(2).count().is(2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM