Gremlin查询:如何获取ORG1到ORG2的两条完整SimplePath
获取ORG1到ORG2的两条完整路径(要求使用bothE())
图结构定义
以下是构建图结构的Gremlin语句:
graph.addV("ORG").property(T.id, "ORG1").property("orgId", "ef5c").iterate(); graph.addV("COMP").property(T.id, "COMP1").property("compId", "2112896").iterate(); graph.addE("edge").property("sub", "5779").property(T.id, "edge1").from(__.V("ORG1")).to(__.V("COMP1")).iterate(); graph.addV("COMP").property(T.id, "COMP2").property("compId", "2100198").iterate(); graph.addE("edge").property("sub", "5779").property(T.id, "edge2").from(__.V("COMP1")).to(__.V("COMP2")).iterate(); graph.addV("COMP").property(T.id, "COMP3").property("compId", "4007384").iterate(); graph.addE("edge").property("sub", "5779").property(T.id, "edge3").from(__.V("COMP2")).to(__.V("COMP3")).iterate(); graph.addV("COMP").property(T.id, "COMP4").property("compId", "4031986").iterate(); graph.addE("edge").property("sub", "5779").property(T.id, "edge4").from(__.V("COMP3")).to(__.V("COMP4")).iterate(); graph.addV("COMP").property(T.id, "COMP5").property("compId", "2116697").iterate(); graph.addE("edge").property("sub", "5779").property(T.id, "edge5").from(__.V("COMP4")).to(__.V("COMP5")).iterate(); graph.addE("edge").property("sub", "951a").property(T.id, "edge9").from(__.V("COMP4")).to(__.V("COMP5")).iterate(); graph.addV("COMP").property(T.id, "COMP6").property("compId", "2116698").iterate(); graph.addE("edge").property("sub", "5779").property(T.id, "edge6").from(__.V("COMP5")).to(__.V("COMP6")).iterate(); graph.addE("edge").property("sub", "951a").property(T.id, "edge10").from(__.V("COMP5")).to(__.V("COMP6")).iterate(); graph.addV("COMP").property(T.id, "COMP7").property("compId", "6040").iterate(); graph.addE("edge").property("sub", "5779").property(T.id, "edge7").from(__.V("COMP6")).to(__.V("COMP7")).iterate(); graph.addE("edge").property("sub", "951a").property(T.id, "edge11").from(__.V("COMP6")).to(__.V("COMP7")).iterate(); graph.addV("ORG").property(T.id, "ORG2").property("orgId", "3ef3").iterate(); graph.addE("edge").property("sub", "5779").property(T.id, "edge8").from(__.V("COMP7")).to(__.V("ORG2")).iterate(); graph.addE("edge").property("sub", "951a").property(T.id, "edge12").from(__.V("COMP7")).to(__.V("ORG2")).iterate(); graph.addV("COMP").property(T.id, "COMP8").property("compId", "2106827").iterate(); graph.addE("edge").property("sub", "951a").property(T.id, "edge13").from(__.V("ORG1")).to(__.V("COMP8")).iterate(); graph.addV("COMP").property(T.id, "COMP9").property("compId", "2106829").iterate(); graph.addE("edge").property("sub", "951a").property(T.id, "edge14").from(__.V("COMP8")).to(__.V("COMP9")).iterate(); graph.addV("COMP").property(T.id, "COMP10").property("compId", "2104080").iterate(); graph.addE("edge").property("sub", "951a").property(T.id, "edge15").from(__.V("COMP9")).to(__.V("COMP10")).iterate(); graph.addV("COMP").property(T.id, "COMP11").property("compId", "2110851").iterate(); graph.addE("edge").property("sub", "951a").property(T.id, "edge16").from(__.V("COMP10")).to(__.V("COMP11")).iterate(); graph.addV("COMP").property(T.id, "COMP12").property("compId", "4020209").iterate(); graph.addE("edge").property("sub", "951a").property(T.id, "edge17").from(__.V("COMP11")).to(__.V("COMP12")).iterate(); graph.addE("edge").property("sub", "951a").property(T.id, "edge18").from(__.V("COMP12")).to(__.V("COMP4")).iterate();
已尝试的查询及问题
查询1
List<Path> routes = graph.V().has("orgId", "ef5c").repeat(bothE().otherV().simplePath()).until(__.hasLabel("ORG")).dedup().path().toList();
结果:仅返回一条路径
Route : path[v[ORG1], e[edge1][ORG1-edge->COMP1], v[COMP1], e[edge2][COMP1-edge->COMP2], v[COMP2], e[edge3][COMP2-edge->COMP3], v[COMP3], e[edge4][COMP3-edge->COMP4], v[COMP4], e[edge5][COMP4-edge->COMP5], v[COMP5], e[edge10][COMP5-edge->COMP6], v[COMP6], e[edge7][COMP6-edge->COMP7], v[COMP7], e[edge12][COMP7-edge->ORG2], v[ORG2]]
问题:未识别出另一条通过COMP8-COMP12的路径。
查询2
g.V().has("orgId", "ef5c").repeat(bothE().dedup().by("sub").otherV().simplePath()).until(hasLabel("ORG")).emit().path()
问题:返回了两条路径,但未走到ORG2就终止,只返回路径到下一个顶点,不满足until条件要求的到达ORG标签顶点。
解决方案
要同时满足使用bothE()、获取到ORG2的完整路径、返回两条不同路径的要求,可以使用以下查询:
g.V().has("orgId", "ef5c") .repeat(bothE().otherV().simplePath()) .until(hasLabel("ORG").and(neq(V().has("orgId", "ef5c")))) .dedup().by(path().by("sub").fold()) .path()
关键说明
until条件优化:添加neq(V().has("orgId", "ef5c"))排除起始顶点ORG1,确保终止于ORG2。- 路径去重:
dedup().by(path().by("sub").fold())基于路径中所有边的sub属性集合进行去重,确保两条不同sub属性的路径都被保留。 simplePath():避免路径中出现循环,确保遍历的有效性。
内容的提问来源于stack exchange,提问作者Parvesh Kumar
相关产品推荐
相关产品推荐

