简体   繁体   中英

Gremlin - Select vertices based on group keys

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

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM