Search code examples
graphqlgremlintinkerpop

Get vertex with particular edges - remove duplicates


I need to export vertex with some of connected to it. Without any projection - just as it is (like a data dump). What I want to achieve is to remove redundant vertices in result (json document). Let assume there is a graph structure:

g.V().has('id', '000-000').as('a').out().as('b').out().as('c').select('a','b','c')
==>[a:v[1],b:v[4],c:v[5]]
==>[a:v[1],b:v[4],c:v[3]]

I'd like to get result in format like:

==>[a:v[1],b:v[4],[c:v[5], c:v[3]]]

If there will be more b or c vertices, something like that:

==>[a:v[1],[b:v[4],b:v[7]],[c:v[5], c:v[3], c:v[9]]]

Just to make output data (in json) smaller.

Currently I'm getting something like that:

[{
    "a": {},
    "b4": {},
    "c5": {}
},
{
    "a": {},
    "b4": {},
    "c3": {}
}]

And would like to have:

[{
    "a": {},
    "b": [
        {b4},
        {b4}
    ],
    "c": [
        {c5}, 
        {c3}
    ]
]}

Which operators should I use to achieve that?


Solution

  • The best way to do this is to use the project() step in Gremlin. If you want a list of values in each field, you can use fold() to put them into a list. Example:

    g.('000-000').project('a','b','c')
         .by()
         .by(out().fold())
         .by(out().out().fold())