TinkerPop: Gremlin revisiting visited edges

我的梦境 提交于 2019-12-11 08:46:06

问题


Sample Data:

Query to create sample data

g.addV("Test1").property("title", "A")
g.addV("Test2").property("title", "B")
g.addV("Test3").property("title", "C")
g.addV("Test4").property("title", "D")

g.V().has("Test1", "title", "A").addE("rel").to(g.V().has("Test2", "title", "B"))
g.V().has("Test2", "title", "B").addE("rel").to(g.V().has("Test3", "title", "C"))
g.V().has("Test3", "title", "C").addE("rel").to(g.V().has("Test4", "title", "D"))

Query:

  1. Find how A is connected to A

Expected answer: Not Connected

My Queries:

g.V().has("Test1", "title", "A").as("nodes")
.repeat(both().as("nodes"))
.emit(hasLabel("Test1")).hasLabel("Test1")
.limit(25)
.project("val").by(select(all, "nodes").unfold().values("title").fold())

Results (limit 25)

{'val': ['A', 'B', 'A']}
{'val': ['A', 'B', 'C', 'B', 'A']}
{'val': ['A', 'B', 'A', 'B', 'A']}
{'val': ['A', 'B', 'C', 'D', 'C', 'B', 'A']}
{'val': ['A', 'B', 'C', 'B', 'C', 'B', 'A']}
{'val': ['A', 'B', 'C', 'B', 'A', 'B', 'A']}
{'val': ['A', 'B', 'A', 'B', 'C', 'B', 'A']}
{'val': ['A', 'B', 'A', 'B', 'A', 'B', 'A']}
{'val': ['A', 'B', 'C', 'D', 'C', 'D', 'C', 'B', 'A']}
{'val': ['A', 'B', 'C', 'D', 'C', 'B', 'C', 'B', 'A']}
{'val': ['A', 'B', 'C', 'D', 'C', 'B', 'A', 'B', 'A']}
{'val': ['A', 'B', 'C', 'B', 'C', 'D', 'C', 'B', 'A']}
{'val': ['A', 'B', 'C', 'B', 'C', 'B', 'C', 'B', 'A']}
{'val': ['A', 'B', 'C', 'B', 'C', 'B', 'A', 'B', 'A']}
{'val': ['A', 'B', 'C', 'B', 'A', 'B', 'C', 'B', 'A']}
{'val': ['A', 'B', 'C', 'B', 'A', 'B', 'A', 'B', 'A']}
{'val': ['A', 'B', 'A', 'B', 'C', 'D', 'C', 'B', 'A']}
{'val': ['A', 'B', 'A', 'B', 'C', 'B', 'C', 'B', 'A']}
{'val': ['A', 'B', 'A', 'B', 'C', 'B', 'A', 'B', 'A']}
{'val': ['A', 'B', 'A', 'B', 'A', 'B', 'C', 'B', 'A']}
{'val': ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A']}
{'val': ['A', 'B', 'C', 'D', 'C', 'D', 'C', 'D', 'C', 'B', 'A']}
{'val': ['A', 'B', 'C', 'D', 'C', 'D', 'C', 'B', 'C', 'B', 'A']}
{'val': ['A', 'B', 'C', 'D', 'C', 'D', 'C', 'B', 'A', 'B', 'A']}
{'val': ['A', 'B', 'C', 'D', 'C', 'B', 'C', 'D', 'C', 'B', 'A']}

Okay lets try dedup

g.V().has("Test1", "title", "A").as("nodes")
.repeat(both().as("nodes").dedup())
.emit(hasLabel("Test1")).hasLabel("Test1")
.project("val").by(select(all, "nodes").unfold().values("title").fold())

Result

{'val': ['A', 'B', 'A']}

There is exactly one edge between A and B. Then why is Gremlin traversing on the same edge? Am I writing the wrong query?

How Cypher Query behaves seems to be the correct way to be

MATCH p=((n:Test1)-[*]-(m:Test1)) RETURN p


Gremlin Server version: 3.3.1


回答1:


Looks like all you want is non-cyclic paths:

g.V().has("Test1", "title", "A").
  repeat(both().simplePath()).
    until(hasLabel("Test1")).
  path().
    by("title")


来源:https://stackoverflow.com/questions/51967983/tinkerpop-gremlin-revisiting-visited-edges

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!