简体   繁体   English

查询neo4j java后获取路径

[英]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). 我正在尝试执行查询以查找与模式“(订单)-[订单]->(产品)-[PART_OF]->(类别)”相对应的所有可能路径,并希望获取完整路径(即所有3个节点和2个关系作为其相应的类)。

The method i used below only let me have 1 column of data (number of orders: 2155). 我下面使用的方法仅让我拥有1列数据(订单数:2155)。 If I tried it once more (the 2nd for loop), the number of row i'd get is 0(number of products: 0). 如果我再试一次(第二个for循环),我得到的行数是0(产品数: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? 有没有一种方法可以将所有结果另存为节点和关系,还是必须查询该命令5次以上? 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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