Search code examples
gremlin

How do I find all first level children and the edges between them from a specific vertex


My graph representing a sentence split up into subject, predicate and object where each top node represent what used to be a sentence but now holds a number of subject, predicate and objects as child nodes.

Sentence1 (top level node) Kevin (belongs_to Sentence1) loves (edge between Kevin and formula_1) formula_1 (belongs_to Sentence1)

where "Kevin" and "formula_1" is nodes and "loves" is an edge. Both nodes are connected to the sentence1 via an edge named 'belongs_to'

I would like to find the Sentence1 node by the predicate (loves) and the object (formula_1) and then find all the nodes below sentence1 (in this example there is only these 3 words but it could be a lot more) The problem is that both "Kevin" and "formula_1" has a lot of both in going and out going edges that I want to filter out.

So for, I can only find the sentence and the nodes below it but not the edges. This is what I have tried so far but I know there must be a simpler way.

g.V()
    .hasId('formula_1').as('inVertex')
    .inE().hasLabel('loves')
    .outV().as('outVertex')
    .out('belongs_to')
    .where(in('belongs_to').hasId('formula_1')).as('sentence')
    .in('belongs_to').dedup().as('object')
    .bothE().as('predicate')
    .bothV().as('vertex')
    .where('vertex', eq('object')).dedup()

This will find the sentence I am looking for and the "belongs_to" nodes but how do I find the edges between the nodes belonging to sentence1?

Obviously I am new to gremlin and hope someone can help.


Solution

  • You can use below query.

    g.V('formula1').
      where(inE('loves')).
      out('belongsTo').
      in().
      aggregate('allVertices').
      bothE().as('a').
      otherV().as('b').
      where('b', within('allVertices')).
      select('a')