Cypher自定义路径输出

时间:2019-05-03 17:07:49

标签: neo4j cypher

我在Cypher中有以下查询。

MATCH (n:Resource { uri: 'http://elite.polito.it/ontologies/dogont.owl#StateValue' }),(m:Resource { uri: 'http://elite.polito.it/ontologies/dogont.owl#Actuator'}), p=allShortestPaths((n)-[r*]-(m)) WHERE NONE(x IN NODES(p) WHERE x.uri='http://www.w3.org/2002/07/owl#Thing') RETURN p

它返回以下结果:

│[{"uri":"http://elite.polito.it/ontologies/dogont.owl#StateValue"},{},│
│{"uri":"http://elite.polito.it/ontologies/dogont.owl#realStateValue"},│
│{"uri":"http://elite.polito.it/ontologies/dogont.owl#realStateValue"},│
│{},{"uri":"http://www.w3.org/2001/XMLSchema#string"},{"uri":"http://ww│
│w.w3.org/2001/XMLSchema#string"},{},{"uri":"http://purl.org/goodrelati│
│ons/v1#serialNumber"},{"uri":"http://purl.org/goodrelations/v1#serialN│
│umber"},{},{"rdfs__comment":"All building things that can be controlle│
│d by domotic system","uri":"http://elite.polito.it/ontologies/dogont.o│
│wl#Controllable","rdfs__label":"Controllable"},{"rdfs__comment":"All b│
│uilding things that can be controlled by domotic system","uri":"http:/│
│/elite.polito.it/ontologies/dogont.owl#Controllable","rdfs__label":"Co│
│ntrollable"},{},{"rdfs__comment":"A mechanism that puts something into│
│ automatic action","uri":"http://elite.polito.it/ontologies/dogont.owl│
│#Actuator","rdfs__label":"Actuator"}]                                 │

结果是,中间节点出现两次。为什么会发生这种情况以及如何防止这种情况发生?同样,目前relationships是空白的,如何用{}替换relationships的{​​{1}}。

1 个答案:

答案 0 :(得分:1)

返回的路径由一系列关系组成,并且为每个关系返回的数据实际上是一个三元组(开始节点,关系,结束节点)。因此,一个关系的结束节点再次显示为下一个关系的开始节点。

如果只想获取没有节点的关系,则可以使用RELATIONSHIPS(p);如果只希望这些节点没有关系,则可以使用NODES(p)

如果您想生成自己的路径列表而不增加节点数量,请尝试以下操作:

MATCH
  (n:Resource { uri: 'http://elite.polito.it/ontologies/dogont.owl#StateValue'}),
  (m:Resource { uri: 'http://elite.polito.it/ontologies/dogont.owl#Actuator'}), 
  p=allShortestPaths((n)-[*]-(m))
WHERE NONE(x IN NODES(p) WHERE x.uri='http://www.w3.org/2002/07/owl#Thing') 
RETURN REDUCE(s=[PROPERTIES(n)], r IN RELATIONSHIPS(p) | s + r + ENDNODE(r)) AS p