gremlingraph-databases

How to use `project` step with `tree` step in Gremlin query


I would like to be able to fetch my categories in my graph database in a way that the children of the categories will be inside children property.

I know this is possible with project step, but how do I use it with tree step?

This is the array structure that I want to achieve:

[
    {
        'name' : 'shoes',
        'children': [
            {
                'name' : 'nike',
                'children' : [
                    {
                        'name' : 'jordan 1',
                        'children' : []
                    },
                    {
                        'name' : 'air force 1',
                        'children' : []
                    },
                ],
            },
            {
                'name' : 'adidas',
                'children': [
                    {
                        'name' : 'nike',
                        'children' : [
                            {
                                'name' : 'yeezy',
                                'children' : []
                            },
                            {
                                'name' : 'stan smith',
                                'children' : []
                            },
                        ],
                    },
                ],
            },
        ],
    },
]

Please see image below to see the graph database. enter image description here

Use the below code to seed your graph database from the image above.

g
.addV('category')
.property('name', 'shoes')
.as('shoes')
.addE('has')
.to(
    addV('category')
    .property('name', 'nike')
    .as('nike')
    .addE('has')
    .to(
        addV('category')
        .property('name', 'air force 1')
    )
    .select('nike')
    .addE('has')
    .to(
        addV('category')
        .property('name', 'jordan 1')
    )
    .select('nike')
)
.select('shoes')
.addE('has')
.to(
    addV('category')
    .property('name', 'adidas')
    .as('adidas')
    .addE('has')
    .to(
        addV('category')
        .property('name', 'yeezy')
    )
    .select('adidas')
    .addE('has')
    .to(
        addV('category')
        .property('name', 'stan smith')
    )
    .select('adidas')
)

Solution

  • Similar ask in this question here: How to transform a Gremlin GraphSON in tree format to custom JSON tree format in CosmosDB?

    The Java GLV has a better implementation of the tree() step, as there is a native class that it can return the results to: https://tinkerpop.apache.org/javadocs/current/full/org/apache/tinkerpop/gremlin/process/traversal/step/util/Tree.html

    Other GLVs are going to return a verbose JSON (GraphSON) like result. This is mainly because other GLVs (like Python, Node, etc.) have not implemented a Tree class or don't have any other datatype/structure that a tree could map to. You're likely better off returning the entire tree JSON object and creating a method to convert it to the format that you would like.