过滤掉重复路径时遇到问题

时间:2014-07-15 12:26:36

标签: neo4j cypher

我在从Neo4j的返回中删除重复路径时遇到了一些问题。

这是密码查询:

MATCH (subA:`Subaccount` {acct: 199345})-[:`contains`]->(intA:`Physical Interface`)-[conn:`connects`]-(intB:`Physical Interface`)<-[:`contains`]-(subB:`Subaccount`)
RETURN DISTINCT id(conn), subA.name, intA.name, intB.name AS `Connected Host Interface`, subB.name AS `Connected Host`, subB.acct AS `Connected Host Account`
ORDER BY id(conn);

返回以下结果集:

+-----------------------------------------------------------------------------------------------------------------------------------+
| id(conn) | subA.name           | intA.name     | Connected Host Interface | Connected Host               | Connected Host Account |
+-----------------------------------------------------------------------------------------------------------------------------------+
| 30832    | "fw.rrfaae.com"     | "ethernet0/0" | "Gi0/1"                  | "b3s5r12k11s1.liquidweb.com" | 22                     |
| 30833    | "fw.rrfaae.com"     | "ethernet0/1" | "Gi0/48"                 | "sw.rrfaae.com"              | 199345                 |
| 30833    | "sw.rrfaae.com"     | "Gi0/48"      | "ethernet0/1"            | "fw.rrfaae.com"              | 199345                 |
| 30834    | "dummy2.rrfaae.com" | "eth0"        | "Gi0/2"                  | "b3s5r12k11s1.liquidweb.com" | 22                     |
| 30835    | "dummy1.rrfaae.com" | "eth0"        | "Gi0/3"                  | "b3s5r12k11s1.liquidweb.com" | 22                     |
| 30836    | "dummy1.rrfaae.com" | "eth1"        | "eth1"                   | "dummy2.rrfaae.com"          | 199345                 |
| 30836    | "dummy2.rrfaae.com" | "eth1"        | "eth1"                   | "dummy1.rrfaae.com"          | 199345                 |
| 30837    | "sw.rrfaae.com"     | "Gi0/1"       | "eth0"                   | "web01.rrfaae.com"           | 199345                 |
| 30837    | "web01.rrfaae.com"  | "eth0"        | "Gi0/1"                  | "sw.rrfaae.com"              | 199345                 |
| 30838    | "sw.rrfaae.com"     | "Gi0/2"       | "eth1"                   | "web01.rrfaae.com"           | 199345                 |
| 30838    | "web01.rrfaae.com"  | "eth1"        | "Gi0/2"                  | "sw.rrfaae.com"              | 199345                 |
| 30840    | "sw.rrfaae.com"     | "Gi0/4"       | "eth1"                   | "web02.rrfaae.com"           | 199345                 |
| 30840    | "web02.rrfaae.com"  | "eth1"        | "Gi0/4"                  | "sw.rrfaae.com"              | 199345                 |
| 30841    | "sw.rrfaae.com"     | "Gi0/3"       | "eth0"                   | "web02.rrfaae.com"           | 199345                 |
| 30841    | "web02.rrfaae.com"  | "eth0"        | "Gi0/3"                  | "sw.rrfaae.com"              | 199345                 |
| 30842    | "db01.rrfaae.com"   | "serial"      | "serial"                 | "db02.rrfaae.com"            | 199345                 |
| 30842    | "db02.rrfaae.com"   | "serial"      | "serial"                 | "db01.rrfaae.com"            | 199345                 |
| 30843    | "sw.rrfaae.com"     | "Gi0/5"       | "eth0"                   | "db01.rrfaae.com"            | 199345                 |
| 30843    | "db01.rrfaae.com"   | "eth0"        | "Gi0/5"                  | "sw.rrfaae.com"              | 199345                 |
| 30844    | "sw.rrfaae.com"     | "Gi0/6"       | "eth1"                   | "db01.rrfaae.com"            | 199345                 |
| 30844    | "db01.rrfaae.com"   | "eth1"        | "Gi0/6"                  | "sw.rrfaae.com"              | 199345                 |
| 30845    | "db02.rrfaae.com"   | "eth2"        | "eth2"                   | "db01.rrfaae.com"            | 199345                 |
| 30845    | "db01.rrfaae.com"   | "eth2"        | "eth2"                   | "db02.rrfaae.com"            | 199345                 |
| 30846    | "sw.rrfaae.com"     | "Gi0/7"       | "eth0"                   | "db02.rrfaae.com"            | 199345                 |
| 30846    | "db02.rrfaae.com"   | "eth0"        | "Gi0/7"                  | "sw.rrfaae.com"              | 199345                 |
| 30847    | "sw.rrfaae.com"     | "Gi0/8"       | "eth1"                   | "db02.rrfaae.com"            | 199345                 |
| 30847    | "db02.rrfaae.com"   | "eth1"        | "Gi0/8"                  | "sw.rrfaae.com"              | 199345                 |
+-----------------------------------------------------------------------------------------------------------------------------------+

looking at建议使用WHERE id(a) < id(b)类型条款的一种可能解决方案。

如果subA没有根据属性进行匹配,那么这将有效。但是,既然如此,一些应该返回的结果就不会出现:

查询:

MATCH (subA:`Subaccount` {acct: 199345})-[:`contains`]->(intA:`Physical Interface`)-[conn:`connects`]-(intB:`Physical Interface`)<-[:`contains`]-(subB:`Subaccount`)
WHERE id(subA) < id(subB)
RETURN DISTINCT id(conn), subA.name, intA.name, intB.name AS `Connected Host Interface`, subB.name AS `Connected Host`, subB.acct AS `Connected Host Account`
ORDER BY id(conn);

结果集:

+--------------------------------------------------------------------------------------------------------------------------+
| id(conn) | subA.name           | intA.name     | Connected Host Interface | Connected Host      | Connected Host Account |
+--------------------------------------------------------------------------------------------------------------------------+
| 30833    | "fw.rrfaae.com"     | "ethernet0/1" | "Gi0/48"                 | "sw.rrfaae.com"     | 199345                 |
| 30836    | "dummy1.rrfaae.com" | "eth1"        | "eth1"                   | "dummy2.rrfaae.com" | 199345                 |
| 30837    | "sw.rrfaae.com"     | "Gi0/1"       | "eth0"                   | "web01.rrfaae.com"  | 199345                 |
| 30838    | "sw.rrfaae.com"     | "Gi0/2"       | "eth1"                   | "web01.rrfaae.com"  | 199345                 |
| 30840    | "sw.rrfaae.com"     | "Gi0/4"       | "eth1"                   | "web02.rrfaae.com"  | 199345                 |
| 30841    | "sw.rrfaae.com"     | "Gi0/3"       | "eth0"                   | "web02.rrfaae.com"  | 199345                 |
| 30842    | "db01.rrfaae.com"   | "serial"      | "serial"                 | "db02.rrfaae.com"   | 199345                 |
| 30843    | "sw.rrfaae.com"     | "Gi0/5"       | "eth0"                   | "db01.rrfaae.com"   | 199345                 |
| 30844    | "sw.rrfaae.com"     | "Gi0/6"       | "eth1"                   | "db01.rrfaae.com"   | 199345                 |
| 30845    | "db01.rrfaae.com"   | "eth2"        | "eth2"                   | "db02.rrfaae.com"   | 199345                 |
| 30846    | "sw.rrfaae.com"     | "Gi0/7"       | "eth0"                   | "db02.rrfaae.com"   | 199345                 |
| 30847    | "sw.rrfaae.com"     | "Gi0/8"       | "eth1"                   | "db02.rrfaae.com"   | 199345                 |
+--------------------------------------------------------------------------------------------------------------------------+

在这种情况下,b3s5r12k11s1.liquidweb.com的ID低于连接到它的设备,但由于它的acct属性为22,因此不会显示在subA中。

似乎DISTINCT语句中的RETURN限定符有效,但前提是唯一列为id(conn)

非常感谢任何帮助!

-Jason

2 个答案:

答案 0 :(得分:0)

我相信在使用条件id(subA) < id(subB)时,您必须省略关系方向:

MATCH (subA:`Subaccount` {acct: 199345})-[:`contains`]-(intA:`Physical Interface`)-[conn:`connects`]-(intB:`Physical Interface`)-[:`contains`]-(subB:`Subaccount`)
WHERE id(subA) < id(subB)
RETURN DISTINCT id(conn), subA.name, intA.name, intB.name AS `Connected Host Interface`, subB.name AS `Connected Host`, subB.acct AS `Connected Host Account`
ORDER BY id(conn);

修改

比使用WITH

使用更长的语句
MATCH (subA:`Subaccount` {acct: 199345})-[:`contains`]->(intA:`Physical Interface`)-[conn:`connects`]-(intB:`Physical Interface`)<-[:`contains`]-(subB:`Subaccount`)
WITH conn
MATCH (subA:`Subaccount`)-[:`contains`]->(intA:`Physical Interface`)-[conn:`connects`]-(intB:`Physical Interface`)<-[:`contains`]-(subB:`Subaccount`)
WHERE id(subA) < id(subB)
RETURN DISTINCT id(conn), subA.name, intA.name, intB.name AS `Connected Host Interface`, subB.name AS `Connected Host`, subB.acct AS `Connected Host Account`
ORDER BY id(conn);

答案 1 :(得分:0)

我仍然希望@ulkas获得荣誉,因为他的更新查询给了我尝试这个想法:

修改

MATCH (subA:`Subaccount`)-[:`contains`]->(intA:`Physical Interface`)-[conn:`connects`]-(intB:`Physical Interface`)<-[:`contains`]-(subB:`Subaccount`)
WHERE (subA.acct = 199345 AND id(subA) < id(subB)) OR (subA.acct = 199345 AND subB.acct <> 199345)
RETURN subA.name, intA.name, intB.name, subB.name
ORDER BY subA.name;

结果集:

+-----------------------------------------------------------------------------------------------------------------------------------+
| id(conn) | subA.name           | intA.name     | Connected Host Interface | Connected Host               | Connected Host Account |
+-----------------------------------------------------------------------------------------------------------------------------------+
| 30832    | "fw.rrfaae.com"     | "ethernet0/0" | "Gi0/1"                  | "b3s5r12k11s1.liquidweb.com" | 22                     |
| 30833    | "fw.rrfaae.com"     | "ethernet0/1" | "Gi0/48"                 | "sw.rrfaae.com"              | 199345                 |
| 30834    | "dummy2.rrfaae.com" | "eth0"        | "Gi0/2"                  | "b3s5r12k11s1.liquidweb.com" | 22                     |
| 30835    | "dummy1.rrfaae.com" | "eth0"        | "Gi0/3"                  | "b3s5r12k11s1.liquidweb.com" | 22                     |
| 30836    | "dummy1.rrfaae.com" | "eth1"        | "eth1"                   | "dummy2.rrfaae.com"          | 199345                 |
| 30837    | "sw.rrfaae.com"     | "Gi0/1"       | "eth0"                   | "web01.rrfaae.com"           | 199345                 |
| 30838    | "sw.rrfaae.com"     | "Gi0/2"       | "eth1"                   | "web01.rrfaae.com"           | 199345                 |
| 30840    | "sw.rrfaae.com"     | "Gi0/4"       | "eth1"                   | "web02.rrfaae.com"           | 199345                 |
| 30841    | "sw.rrfaae.com"     | "Gi0/3"       | "eth0"                   | "web02.rrfaae.com"           | 199345                 |
| 30842    | "db01.rrfaae.com"   | "serial"      | "serial"                 | "db02.rrfaae.com"            | 199345                 |
| 30843    | "sw.rrfaae.com"     | "Gi0/5"       | "eth0"                   | "db01.rrfaae.com"            | 199345                 |
| 30844    | "sw.rrfaae.com"     | "Gi0/6"       | "eth1"                   | "db01.rrfaae.com"            | 199345                 |
| 30845    | "db01.rrfaae.com"   | "eth2"        | "eth2"                   | "db02.rrfaae.com"            | 199345                 |
| 30846    | "sw.rrfaae.com"     | "Gi0/7"       | "eth0"                   | "db02.rrfaae.com"            | 199345                 |
| 30847    | "sw.rrfaae.com"     | "Gi0/8"       | "eth1"                   | "db02.rrfaae.com"            | 199345                 |
+-----------------------------------------------------------------------------------------------------------------------------------+