(Product) - [PART_OF] -> (Category)" and woul..." /> (Product) - [PART_OF] -> (Category)" and woul..." /> (Product) - [PART_OF] -> (Category)" and woul..."/>

get path after querying neo4j java

384 Views Asked by At

I'm trying to do a query to fin all possible paths that correspond to the pattern "(Order) - [ORDERS] -> (Product) - [PART_OF] -> (Category)" and would like to get the whole path (i.e. all 3 nodes and 2 relationships as their appropriate classes).

The method i used below only let me have 1 column of data (number of orders: 2155). If I tried it once more (the 2nd for loop), the number of row i'd get is 0(number of products: 0). Is there a way to save all the results as nodes and relationships or do I have to query the command 5 times over? Please help!

String query = "MATCH (o:Order)-[:ORDERS]->(p:Product)-[:PART_OF]->(cate:Category) return o,p,cate";
    try( Transaction tx = db.beginTx();
         Result result = db.execute(query) ){
        Iterator<Node> o_column = result.columnAs( "o" );
        int i = 0;
        for ( Node node : Iterators.asIterable( o_column ) )
        {
            i++;
        }
        System.out.println("number of orders: " + i);
        i = 0;
        Iterator<Node> p_column = result.columnAs( "p" );
        for ( Node node : Iterators.asIterable( p_column ) )
        {
            i++;
        }
        System.out.println("number of products: " + i);

        tx.success();
    }
2

There are 2 best solutions below

0
On

I've found a way to work around this in the code below, where i'd changes the return value to the node ID using id() then uses GraphDatabaseService.getNodeByID(long):

String query = "MATCH (o:Order)-[:ORDERS]->(p:Product)-[:PART_OF]->(cate:Category) return id(o), id(p), id(cate)";

int nodeID = Integer.parseInt(column.getValue().toString()); Node node = db.getNodeById(nodeID);

0
On

If you do this :

MATCH path=(o:Order)-[:ORDERS]->(p:Product)-[:PART_OF]->(cate:Category) return path

You can process path in your loop and unpack that. Takes a bit of exploring but all the information is in there.

Hope that helps.

Regards, Tom