0
votes

I am using Traversal API, but seems Traversal.expanderForTypes() deprecated and I do not know how to find shortest path between two nodes.

My method

 public Iterable<Path> shortestPath(long firstNodeId, long secondNodeId) {
        Node firstNode = null;
        Node secondNode = null;
        try (Transaction tx = mainServiceBean.getDatabaseService().beginTx()) {
            firstNode = mainServiceBean.getDatabaseService().getNodeById(firstNodeId);
            secondNode = mainServiceBean.getDatabaseService().getNodeById(secondNodeId);
            PathExpander expander = Traversal.expanderForTypes();//?
            PathFinder<Path> shortestPath = GraphAlgoFactory.shortestPath(expander, 4, 4);
            tx.success();
            return shortestPath.findAllPaths(firstNode, secondNode);
        }
    }

My nodes are cities and relationship between like this

 Node nodeACity = mainServiceBean.getDatabaseService().createNode(ProjectLabels.City);

            nodeACity .setProperty(City.NAME, CityNames.ACiTY.name());

            Node nodeBCity = mainServiceBean.getDatabaseService().createNode(ProjectLabels.City);
            nodeBCity.setProperty(City.NAME, CityNames.BCity.name());


 Relationship ab= nodeACity .createRelationshipTo(nodeBCity , NodesRelationship.DISTANCE_TO);
            ab.setProperty("distance", 124.31);

            Relationship ba= nodeBCity .createRelationshipTo(nodeACity , NodesRelationship.DISTANCE_TO);
            ba.setProperty("distance", 124.31);

So relationship have property distance with value.

How can use traversal API from neo4j 3? Seems a lot of changed.

1
just to clarify: are you really talking about Neo4j 3.1 - this is not GA and milestone 8 is out. Or are you referring to Neo4j 3.0.x - which is GA.Stefan Armbruster
yes it is about 3.0.xArmen Arzumanyan

1 Answers

1
votes

You'll find couple of predefined PathExpander implementations in PathExpanders.

Is there a specific reason why you're modelling DISTANCE_TO twice for given nodes a and b? In most cases it's preferable to only have one relationship and ignore the direction during traversal. In this case you can use

PathExpander expander = PathExpanders.forType(NodesRelationship.DISTANCE_TO);