简体   繁体   中英

get path after querying neo4j java

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 (ie 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();
    }

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

我在下面的代码中找到了解决此问题的方法,在该代码中,我将使用id()将返回值更改为节点ID,然后使用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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM