0

Consider the image of a graph:

enter image description here

How can I query in gremlin to get a tree with at most n vertices per edge type ?

To ask in a different way same problem would be like this: Given a root get at most n nodes per edge type recursively as a tree.

I have this query but it returns all nodes:

g.V().has('librarie','nume','nemira').repeat(out()).emit().tree()

Full result:

{
    {id=217216, label=librarie, nume=nemira}=
        {
            {id=147608, label=locatie, nume=iasi}={}
            {id=213120, label=locatie, nume=constanta}={}, 
            {id=167968, label=locatie, nume=cluj}={}, 
            
            {id=163872, label=carte, nume=s9}=
                {{id=204928, label=autor, nume=motzard}={}, {id=159960, label=categorie, nume=muzica}={}}, 

            {id=200832, label=carte, nume=s3}=
                {{id=204928, label=autor, nume=motzard}={}, {id=159960, label=categorie, nume=muzica}={}}, 

            {id=327744, label=carte, nume=electricitate}=
                {{id=151768, label=autor, nume=tesla}={}, {id=155864, label=categorie, nume=fizica}={}}, 

        }
}

(SO falsely evaluates this as being to much code.. in fact beeing an initialisation script for data, now I'm adding this text here as required ..)

Sample data script:

### @name initDB
g.addV('book').property('name','cugetarea');

g.addV('book').property('name','salate');
g.addV('book').property('name','pi');
g.addV('book').property('name','geometrie');
g.addV('book').property('name','sosuri');
g.addV('book').property('name','electro');
g.addV('book').property('name','algebra');
g.addV('book').property('name','electricitate');
g.addV('book').property('name','s9');
g.addV('book').property('name','s3');

g.addV('author').property('name','aristotel');
g.addV('author').property('name','oliver');
g.addV('author').property('name','ganga');
g.addV('author').property('name','oliver');
g.addV('author').property('name','tesla');
g.addV('author').property('name','nita');
g.addV('author').property('name','motzard');

g.addV('cathegory').property('name','filozofie');
g.addV('cathegory').property('name','bucate');
g.addV('cathegory').property('name','matematica');
g.addV('cathegory').property('name','bucate');
g.addV('cathegory').property('name','fizica');
g.addV('cathegory').property('name','muzica');

g.addV('location').property('name','bucuresti');
g.addV('location').property('name','iasi');
g.addV('location').property('name','cluj');
g.addV('location').property('name','constanta');

g.addV('library').property('name','carturesti');
g.addV('library').property('name','teora');
g.addV('library').property('name','nemira');

g.addE('hasLocation').from(V().has('library','name','carturesti')).to(V().has('location','name','bucuresti'));
g.addE('hasLocation').from(V().has('library','name','carturesti')).to(V().has('location','name','iasi'));
g.addE('hasLocation').from(V().has('library','name','carturesti')).to(V().has('location','name','cluj'));

g.addE('hasLocation').from(V().has('library','name','teora')).to(V().has('location','name','iasi'));
g.addE('hasLocation').from(V().has('library','name','teora')).to(V().has('location','name','bucuresti'));
g.addE('hasLocation').from(V().has('library','name','teora')).to(V().has('location','name','constanta'));

g.addE('hasLocation').from(V().has('library','name','nemira')).to(V().has('location','name','cluj'));
g.addE('hasLocation').from(V().has('library','name','nemira')).to(V().has('location','name','constanta'));
g.addE('hasLocation').from(V().has('library','name','nemira')).to(V().has('location','name','iasi'));

g.addE('hasBook').from(V().has('library','name','carturesti')).to(V().has('book','name','cugetarea'));
g.addE('hasBook').from(V().has('library','name','carturesti')).to(V().has('book','name','salate'));
g.addE('hasBook').from(V().has('library','name','carturesti')).to(V().has('book','name','pi'));
g.addE('hasBook').from(V().has('library','name','carturesti')).to(V().has('book','name','geometrie'));
g.addE('hasBook').from(V().has('library','name','teora')).to(V().has('book','name','sosuri'));
g.addE('hasBook').from(V().has('library','name','teora')).to(V().has('book','name','electro'));
g.addE('hasBook').from(V().has('library','name','teora')).to(V().has('book','name','algebra'));
g.addE('hasBook').from(V().has('library','name','nemira')).to(V().has('book','name','electricitate'));
g.addE('hasBook').from(V().has('library','name','nemira')).to(V().has('book','name','s9'));
g.addE('hasBook').from(V().has('library','name','nemira')).to(V().has('book','name','s3'));

g.addE('hasCathegory').from(V().has('book','name','cugetarea')).to(V().has('cathegory','name','filozofie'));
g.addE('hasCathegory').from(V().has('book','name','salate')).to(V().has('cathegory','name','bucate'));
g.addE('hasCathegory').from(V().has('book','name','pi')).to(V().has('cathegory','name','matematica'));
g.addE('hasCathegory').from(V().has('book','name','geometrie')).to(V().has('cathegory','name','matematica'));
g.addE('hasCathegory').from(V().has('book','name','sosuri')).to(V().has('cathegory','name','bucate'));
g.addE('hasCathegory').from(V().has('book','name','electro')).to(V().has('cathegory','name','fizica'));
g.addE('hasCathegory').from(V().has('book','name','algebra')).to(V().has('cathegory','name','matematica'));
g.addE('hasCathegory').from(V().has('book','name','electricitate')).to(V().has('cathegory','name','fizica'));
g.addE('hasCathegory').from(V().has('book','name','s9')).to(V().has('cathegory','name','muzica'));
g.addE('hasCathegory').from(V().has('book','name','s3')).to(V().has('cathegory','name','muzica'));

g.addE('hasAuthor').from(V().has('book','name','cugetarea')).to(V().has('author','name','aristotel'));
g.addE('hasAuthor').from(V().has('book','name','salate')).to(V().has('author','name','oliver'));
g.addE('hasAuthor').from(V().has('book','name','pi')).to(V().has('author','name','ganga'));
g.addE('hasAuthor').from(V().has('book','name','geometrie')).to(V().has('author','name','ganga'));
g.addE('hasAuthor').from(V().has('book','name','sosuri')).to(V().has('author','name','oliver'));
g.addE('hasAuthor').from(V().has('book','name','electro')).to(V().has('author','name','tesla'));
g.addE('hasAuthor').from(V().has('book','name','algebra')).to(V().has('author','name','ganga'));
g.addE('hasAuthor').from(V().has('book','name','electricitate')).to(V().has('author','name','tesla'));
g.addE('hasAuthor').from(V().has('book','name','s9')).to(V().has('author','name','motzard'));
g.addE('hasAuthor').from(V().has('book','name','s3')).to(V().has('author','name','motzard'));

g.tx().commit();
asa
  • 1
  • 1
  • Thank you for the picture but when asking question on Gremlin it is more helpful to provide a Gremlin script with some sample data - here is an example https://stackoverflow.com/questions/51388315/gremlin-choose-one-item-at-random – stephen mallette Mar 12 '21 at 11:35
  • @stephenmallette thanks, I added some sample data, I'd be very grateful to have a solution to the above problem. – asa Mar 14 '21 at 20:01
  • @stephenmallette maybe this can't be achived ? – asa Mar 15 '21 at 14:19
  • are you able to specify the edge labels up front when you write the traversal? – stephen mallette Mar 16 '21 at 11:04
  • no the solution would have to bring vertices from all existing edges – asa Mar 16 '21 at 17:26

0 Answers0