简体   繁体   English

Neo4j Cypher查询和复合对象

[英]Neo4j Cypher query and composite objects

I have a following Cypher query that returns all Decision that belong to a particular Tag : 我有一个以下Cypher查询,该查询返回属于特定Tag所有Decision

MATCH (d:Decision)-[:BELONGS_TO]->(t:Tag) WHERE t.id = {tagId} RETURN d

According to my business logic every Tag can have a set of synonyms: 根据我的业务逻辑,每个Tag可以具有一组同义词:

(t:Tag)<-[:FOR]-(ts:TagSynonym)-[:HAS]->(s:Tag) 

and every s:Tag can also have a synonyms associated at the same way.. unlimited depth and where ts.approved = true . 并且每个s:Tag也可以具有以相同方式关联的同义词。.无限深度,其中ts.approved = true

Could you please show how to extend the first query in order to return not only Decisions associated with a start tag ( t.id = {tagId} ) but also the all Decisions associated with all tag's synonyms(unlimited depth). 您能否显示如何扩展第一个查询,以便不仅返回与开始标签( t.id = {tagId} )相关的决策,而且还返回与所有标签的同义词(无限深度)相关的所有决策。

Ideally all of these Decisions should be returned under one d variable. 理想情况下,所有这些决策都应在一个d变量下返回。

Right now I'm playing with the following query: 现在我在玩以下查询:

MATCH p=(t:Tag)-[:FOR|HAS*]-(end:Tag) 
WHERE t.id = {tagId} AND NOT (end)<-[:FOR]-() 
OPTIONAL MATCH (d:Decision)-[:BELONGS_TO]->(tag) 
RETURN d

but it doesn't work. 但这不起作用。

UPDATED 更新

I have created a Neo4j sandbox: 我创建了一个Neo4j沙箱:

http://54.165.53.29:33761/browser/
neo4j
timer-rocks-hilltop

Please use the following query: 请使用以下查询:

MATCH p=(t:Tag)-[:FOR|HAS*0..]-(end:Tag) 
WHERE t.id = 1 AND NOT (end)<-[:FOR]-() 
MATCH (d:Decision)-[:BELONGS_TO]->(end) 
RETURN d

It returns only the last Decision in the path(Decision3) but should also return Decision1 and Decision 2. 它仅返回路径中的最后一个决策(决策3),但也应返回决策1和决策2。

This is the sample database username/password: neo4j/neo4j1 这是示例数据库用户名/密码:neo4j / neo4j1

I have a 3 Tag and 3 Decision associated with this tag. 我有一个与此标签相关的3个Tag和3个Decision

Also

Tag 2 is a synonym of Tag 1 and Tag 3 is a synonym of Tag 2 . Tag 2Tag 1的同义词, Tag 1 Tag 3Tag 2的同义词。

I need to find all of the Decision by Tag 1 and by its synonyms( Tag 2 and Tag 3 ). 我需要按Tag 1及其同义词( Tag 2Tag 3 )找到所有Decision This is decisions: Decision1 , Decision2 , Decision 3 这是决策: Decision1Decision2Decision 3

There is a subtle thing with variable depth traversals which is the zero depth : 深度遍历有一个微妙的东西,它是零深度:

MATCH p=(t:Tag)-[:FOR|HAS*0..]-(end:Tag) 
WHERE t.id = {tagId} AND NOT (end)<-[:FOR]-() 
MATCH (d:Decision)-[:BELONGS_TO]->(end) 
RETURN d

The idea is that, it handles both the cases where t nodes have a FOR or HAS relationships and when they not. 这个想法是,它可以处理t个节点具有FOR或HAS关系的情况,也可以处理不具有FOR或HAS关系的情况。 The trick is that found tags (even the t node hence the 0) are under the end alias. 诀窍是找到的标签(即使t节点也因此为0)都在end别名下。

You can find a more in depth documentation about variable length relationships in this article : https://graphaware.com/graphaware/2015/05/19/neo4j-cypher-variable-length-relationships-by-example.html 您可以在本文中找到有关变量长度关系的更深入的文档: https : //graphaware.com/graphaware/2015/05/19/neo4j-cypher-variable-length-relationships-by-example.html

EDIT 编辑

It seems the logic for finding the Tag nodes is wrong on your side, the query returns only one Tag due to this part in the query AND NOT (end)<-[:FOR]-() . 似乎找不到Tag节点的逻辑在您这边是错误的,由于查询中的这一部分,查询仅返回一个Tag,而AND NOT (end)<-[:FOR]-() Remove it and you'll see it returns 3 Decisions 删除它,您将看到它返回3个决策

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

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