Search code examples
gremlintinkerpop

Sort paths based on Edge properties


Sorting traversal paths based on Edge property and Dedup

Hello, I'm having a in memory graph and I want to sort paths based on Edge property and also dedup where paths leading to same destination.

E.g.

    String NAME = "name";
   String id = "id";
    g.addV().property(id, 1).property(NAME, "u1").as("u1")
            .addV().property(id, 2).property(NAME, "u2").as("u2")
            .addV().property(id, 3).property(NAME, "u3").as("u3")
            .addV().property(id, 4).property(NAME, "u4").as("u4")
            .addE(rel).from("u2").to("u1").property("order", 2)
            .addE(rel).from("u3").to("u1").property("order", 1)
            .addE(rel).from("u4").to("u2").property("order", 3)
            .addE(rel).from("u4").to("u3").property("order", 4)
            .iterate();

What I'm trying to achieve is a traversal which gives me only one path i.e.

vertices = [path[u1, u3, u4]].

I tried using below gremlin.

    List<Path> maps = g.V()
                .has("id", 1)
                .repeat(in()
                        .simplePath())
                .until(inE().count().is(0))
                .order().by(outE("rel").values("order"),Order.asc)
                .path().by("name")
                .toList();

However sorting doesn't happen. It gives me two paths : vertices = [path[u1, u2, u4], path[u1, u3, u4]]

But I'm looking for output as vertices = [path[u1, u3, u4]]

I'm new to gremlin and ran out of options to try. can someone help ?


Solution

  • g.V() 
    .has("id", 1) 
    .repeat(in("rel") .order() .by(outE().values("order"), Order.asc) .simplePath() )
    .until(inE().count().is(0)) 
    .dedup() 
    .path() 
    .by("name") 
    .toList() ;