4

I have a graph with 2 different vertex classes with some identical properties.

I need to:

  1. group all vertices of class Item based on some properties
  2. find the vertices of class Product that share these properties
g.addV("Item").
    property("color", "green").
    property("material", "wood").
    property("shape", "round").
    property("price", 1.2).
  addV("Item").
    property("color", "green").
    property("material", "wood").
    property("shape", "round").
    property("price", .9).
  addV("Item").
    property("color", "green").
    property("material", "wood").
    property("shape", "square").
    property("price", 5).
  addV("Product").
    property("color", "green").
    property("material", "wood").next();

What I've tried so far is this

g.V().has("Item", "price", P.inside(0, 10)).
  group().
    by(project("c", "m").
      by("color").by("material")). //\1
    local(unfold().
      project("color", "material","price","product")
        .by(select(Column.keys).select("c"))
        .by(select(Column.keys).select("m"))
        .by(select(Column.values).unfold().values("price").mean())
        .by(
          V().hasLabel("Product"). //\2
          has("material",P.eq(select(Column.keys).select("c"))).fold()));

I understand that at 2 the scope changes so select(Column.keys) no longer refers to the group. However, I'm at a loss how to get the value of the c (and m) key into the traversal at 2

1 Answers1

2

So I tried to solve it with a slightly different approach.

Each group will have all the items and the products for the color and material combo

that way most of the work will be done on your first group step:

g.V().coalesce(
    hasLabel("Item").has("price", P.inside(0, 10)),
    hasLabel("Product").has("color").has("material")
    ).group()
    .by(project("c", "m").by("color").by("material"))
    .unfold()
    .where(select(values).unfold().hasLabel("Item"))
      .project("color", "material","price","product")
        .by(select(keys).select("c"))
        .by(select(keys).select("m"))
        .by(select(values).unfold().hasLabel("Item").values("price").mean())
        .by(select(values).unfold().hasLabel("Product").fold())
noam621
  • 2,766
  • 1
  • 16
  • 26
  • Thank you that works. Can you clarify, why? From the docs _coalesce - returns the result from the first traversal to emit at least one element_. Why is the product even in the group then? I read this as "if the first traversal has elements don't do the others" – Denis Kirchhübel Feb 13 '20 at 11:51
  • 1
    Never mind, group takes in all elements of the traverser, so it first "exhausts" the item traversal and then the product. I got confused because the most common use for coalesce I had seen so far, was for adding a vertex, if it didn't exist, but then you only consume 1 element, using ```next``` – Denis Kirchhübel Feb 13 '20 at 11:59