使用Cypher如何在一个跃点内选择连接到节点的所有节点,同时从结果中排除中心节点?

时间:2017-08-05 18:40:35

标签: neo4j cypher

Graph

以上图为例。使用Cypher,我将如何匹配除最长链和中心节点之外的所有节点?即中心节点正好一跳内的所有节点,同时排除中心节点(除3个节点和2个边缘外的所有节点和边缘)。

我尝试了以下内容:

<?xml version="1.0" encoding="utf-8"?>    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="horizontal"
    android:clickable="true"
    android:onClick="openGithubUrl">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/github_icon"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/github_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/github_url"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>

这几乎可以工作,但它仍然返回中心节点(即节点n)。如何从查询结果中排除此节点?

2 个答案:

答案 0 :(得分:0)

[增订]

这将返回直接连接到指定节点的所有不同节点,并显式阻止返回指定节点(如果它与自身有关系):

MATCH (n:Node)--(m)
WHERE n.id = "123" AND n <> m
RETURN DISTINCT m;

答案 1 :(得分:0)

理想情况下,我希望匹配我的问题中提到的节点并删除它们。但是,由于我还没有找到这样做的方法,可以使用逆方法,其中除了问题中提到的那些节点之外的所有节点都匹配。从而有效地排除(但不删除)不需要的节点。

这可以使用此查询来实现:

MATCH (n:Node) WHERE n.id = "123" MATCH path = (m)-[*2..]->(n) RETURN path

这将返回中心节点以及该节点的“长度”大于或等于2的所有路径。