Cypher查询将在两个节点之间仅返回每种类型的1个关系

时间:2016-10-11 12:52:51

标签: neo4j cypher

如何创建一个只返回两个节点之间某种类型的关系的查询?

例如:

MATCH (a)-[r:InteractsWith*..5]->(b) RETURN a,r,b

因为(a)可能与(b)多次相互作用,结果将包含两者之间的许多关系。但是,这种关系并不完全相同。它们具有不同的属性,因为它们发生在不同的时间点。

但是,如果您只对他们至少与进行过一次的互动感兴趣呢?

而不是result as it appears currently我希望收到的结果是:

  • (a)和(b)
  • 之间的关系集中只有一个随机关系
  • 只有那些符合某些标准的关系(例如“最新”或每种类型中的一种,......)

我想到的一种方法是创建“hasEverInteractedWith”类型的新关系。但应该有另一种方式,对吗?

1 个答案:

答案 0 :(得分:1)

使用<groupId>com.spring</groupId> <artifactId>RegistrationWithSpringMvc</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>RegistrationWithSpringMvc</name> <url>http://maven.apache.org</url> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.2.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.2.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/com.hynnet/oracle-driver-ojdbc6 --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.3.2.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.2.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.2.4.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.0.Final</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.21</version> </dependency> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.3.0.Final</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> 获得最快的单个结果。

shortestPath()

如果你想获得一个特定的,你必须得到所有的MATCH (a)-[:InteractsWith*..5]->(b) WITH DISTINCT a, b MATCH p = shortestPath((a)-[:InteractsWith*..5]->(b)) RETURN a, b, RELATIONSHIPS(p) AS r 然后过滤它们,这将会更慢(但提供更多的上下文)。

r