简体   繁体   English

如何在Neo4j中找到匹配关系模式的节点列表?

[英]How to find a list of nodes matching a relationship pattern in Neo4j?

I have to find all nodes, x1 x2 x3 such that x1-[:R1]->x2-[:R2]->x3 . 我必须找到所有节点x1 x2 x3 ,使得x1-[:R1]->x2-[:R2]->x3

Here R1 and R2 will be known aforehand, and we have to find all such x1 x2 x3 which match the condition. 这里R1R2是事先已知的,我们必须找到所有与条件匹配的x1 x2 x3 Also, the number of relationships may increase or decrease ie there could be three relationship R1 R2 R3 and it should return all x1 x2 x3 x4 which satisfies the condition. 同样,关系的数量可以增加或减少,即可能存在三个关系R1 R2 R3并且它应该返回所有满足条件的x1 x2 x3 x4

I need to do this using Java API and not Cypher queries. 我需要使用Java API而不是Cypher查询来执行此操作。

I found the classes PatternNode and PatternRelationship, but found it hard to understand and how to construct them in order to achieve what I want to achieve. 我找到了PatternNode和PatternRelationship类,但是发现很难理解以及如何构造它们才能实现我想要实现的目标。 Also both the classes are deprecated. 同时也不推荐使用这两个类。 Is there a way I could achieve this through Java? 有没有办法可以通过Java实现呢?

You have a couple of ways to traverse a graph in java. 您可以通过两种方法遍历Java中的图形。 These are your options: 这些是您的选择:

Execute a Cypher query 执行密码查询

Use the ExecutionEngine to execute a Cypher Query: 使用ExecutionEngine执行Cypher查询:

final ExecutionEngine engine = new ExecutionEngine(graphDB);
ExecutionResult result = engine.execute("YOUR_CYPHER_QUERY");

Use the Cypher DSL syntax 使用Cypher DSL语法

Again, you're basically writing a Cypher query, but this time, your using a nice DSL statement. 同样,您基本上是在编写Cypher查询,但是这次,您使用了不错的DSL语句。 Check out https://github.com/neo4j/cypher-dsl . 查看https://github.com/neo4j/cypher-dsl

Execute q = start(node("john", john)).
            match(path().from("john").out("friend").link().out("friend").to("fof")).
            returns(properties("john.name", "fof.name"));
ExecutionResult result = engine.execute( q.toString() ).toString();

Use the traversal API 使用遍历API

You control the complete graph traversal. 您可以控制完整的图形遍历。 An example: 一个例子:

for (final Path position : Traversal.description().depthFirst()
    .relationships(RelationType.RELATIONSHIP_TYPE, Direction.INCOMING).traverse(node)) {
   System.out.println(position.endNode();
}

It's up to you, but I'd prefer a simple Cypher statement. 这取决于您,但是我希望使用简单的Cypher语句。

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

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