Joe Kennedy
Joe Kennedy

Reputation: 91

Gremlin - Adding multiple child vertices in one go - addV and step

Here is the general theme I want to play with:

I am trying to do this by doing the following:

  1. Create "parent" vertex and use as("parent")
  2. Create edge "parent_to"
  3. Create "child" vertex
  4. select("parent)
  5. Repeat edge and child vertex steps

Example:

g.addV("parent").property("parent_value","value1").as("parent")
    .addE("parent_to")
        .addV("child").property("child_value","value2")
.select("parent")
    .addE("parent_to")
        .addV("child").property("child_value","value3")

Problem is this gives me 1 parent vertex with two child vertices, but the edges loop back to and from the parent, leaving the child vertices "orphaned".
I have linked a post that gives me a working pattern, but is it really the best way to handle hierarchy in one query without need for recursion? Other QA Note: Added a level for example's sake

g.addV("parent").as("parent")
    .and(
            addV("child").property("child_value","value2")
                .addE("parent_to").from("parent"),
            addV("child").property("child_value","value3").as("child3").and(
                        addV("grandchild").property("gchild_value","value4").addE("parent_to").from("child3"),
                        addV("grandchild").property("gchild_value","value5").addE("parent_to").from("child3")
                    )
                .addE("parent_to").from("parent")
        )

Because this will be used on AWS Neptune, I have to add everything into one query to maintain the transaction.

Upvotes: 1

Views: 417

Answers (1)

bechbd
bechbd

Reputation: 6341

Your query is close, it is just missing specifying the from() or to() steps to tell addE() which vertices to connect. I this case you probably want to use to() step as shown here.

g.addV("parent").
  property("parent_value", "value1").as("parent").
  addE("parent_to").to(addV("child").property("child_value", "value2")).
  select("parent").
  addE("parent_to").to(addV("child").property("child_value", "value3"))

Upvotes: 1

Related Questions