-1

I might be asking an obvious question, but new to the graphs and gremlin language and got a bit stuck.

I have a graph setup where I can find N vertices of a particular type. Let's say I find 2 vertices of type X. These vertices have edges to K vertices of type Y.

I want to find vertices of type Y that all have connection to the 3 vertices I found of the type X. In this situation, the vertices of type Y could be connected to either of the 3 vertices of type X, but I want to get only common ones.

Script to create sample data ```

g.addV("X1").property("name", "category1")
g.addV("X2").property("name", "category2")


g.addV("Y").property("name", "y1")
g.addV("Y").property("name", "y2")
g.addV("Y").property("name", "y3")


g.V().has("Y", "name", "y1").addE("isOf").to(g.V().has("X1", "name", "category1"))
g.V().has("Y", "name", "y1").addE("isOf").to(g.V().has("X2", "name", "category2"))

g.V().has("Y", "name", "y2").addE("isOf").to(g.V().has("X1", "name", "category1"))
g.V().has("Y", "name", "y2").addE("isOf").to(g.V().has("X2", "name", "category2"))

g.V().has("Y", "name", "y3").addE("isOf").to(g.V().has("X1", "name", "category1"))

```

And what I am interested finding are the "Y" vertices that have isOf category1 and category2, and potentially more categories. I need to eliminate vertices Y that connected only to a subset of specified categories.

enter image description here

Stanley Kirdey
  • 602
  • 5
  • 20
  • 3
    When asking questions about Gremlin diagrams are helpful but it's much easier for others to understand a simple Gremlin script that generates a small sample graph that can be pasted into a Gremlin Console - for example, please consider taking a look at this question: https://stackoverflow.com/q/51967983/1831717 – stephen mallette Sep 06 '18 at 20:40
  • Got it, I'll edit the question to show actual relationship. Thanks! – Stanley Kirdey Sep 06 '18 at 22:07

1 Answers1

2

Aggregate all source vertices in a collection named x, then traverse to all y vertices and verify that each y vertex has n number of edges leading to vertices stored in x (where n equals the size of x).

gremlin> g.V().hasLabel("X1","X2").aggregate("x").
           in("isOf").dedup().
           filter(out("isOf").where(within("x")).count().
                  where(eq("x")).
                    by().
                    by(count(local))).
           valueMap()
==>[name:[y1]]
==>[name:[y2]]
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34
  • Thanks! It works in TinkerGraph, but unfortunately I can not run it in the AWS Neptune, as it has limited support of the filter operation. If you have any hints on how to do the same without the filter, I would appreciate it. Accepting answer. – Stanley Kirdey Sep 07 '18 at 00:09
  • Huh? This the `filter` step is one of the most basic steps. What's the error message in Neptune? – Daniel Kuppitz Sep 07 '18 at 00:12
  • {"detailedMessage":"Cannot use path elements for within","requestId":"adc753aa-b114-45be-a5b8-ab2df2f2dafd","code":"UnsupportedOperationException"} – Stanley Kirdey Sep 07 '18 at 00:12
  • 1
    Hmm, perhaps ping the Neptune support..? `x` is not a path element, it's a side-effect. Maybe it's just their query analyzer doing something wrong. – Daniel Kuppitz Sep 07 '18 at 00:16
  • Make sense, I I'll open a ticket with Neptune. – Stanley Kirdey Sep 07 '18 at 00:18