Cypher query to exclude results based upon relationship

时间:2015-10-06 08:37:24

标签: neo4j cypher neo4j.rb

I'm trying to out how to exclude nodes from a query. My graph consists of users, skills, skill scoring, questions and endo

  • user has skills
  • skill has scorings in relation with questions ((skill)-->(scorings)-->(questions))
  • endo is a relationship for users and skills scorings ((user)-->(endos)-->(scorings))

I would like to find all question for user, but exclude those questions who user has already endo relationship

I thought I could do :

MATCH (u:`User`),
  u<-[rel1:`USERS`]-(s:`Skill`),
  s-[rel2:`SKILLS`]->(k:`SkillScoring`),
  k-[rel3:`ANSWER`]->(q:`Question`),
  u<-[rel4:`ENDO`]-(e:`Endo`)

WHERE NOT((e)-->(u)) AND (u.id='1')

RETURN u, e, k, q

UPDATE:

endo nodes are connected like this.

  • blue is user node
  • purple is journaled endorsement node (created at)
  • green is skill scoring node

In fact the relationship "ENDORSEMENT" has a node (journalised) which connects the skill scoring nodes

endo relationship

UPDATE:

When I execute this query, it returns me the question in connection with the user

MATCH (u:User),
  u<-[rel1:USERS]-(s:SoftSkill),
  s-[rel2:SOFT_SKILLS]->(k:SkillScoring),
  k-[rel3:ANSWER]->(q:Question),
  u<-[:ENDO]-()<-[:ENDO]->(k)  
WHERE u.id='1'
RETURN q, u

by cons when I execute this query to exclude the question, the query returns me questions but also question that I don't want

MATCH (u:User),
  u<-[rel1:USERS]-(s:SoftSkill),
  s-[rel2:SOFT_SKILLS]->(k:SkillScoring),
  k-[rel3:ANSWER]->(q:Question)
WHERE u.id='1' AND NOT u<-[:ENDO]-()<-[:ENDO]->(k)
RETURN q, u

What's wrong? Any suggestions?

Thanks

1 个答案:

答案 0 :(得分:1)

您的查询首先说明u:ENDO之间存在e关系,然后eu没有任何关系,但这可以&#39}是的。

在您要排除的情况下,endo节点如何连接到scorings / questions?你可以试试像

这样的东西
MATCH (u:User)-[rel1:USERS]->(s:Skill)-[rel2:SKILLS]->(k:SkillScoring)-[rel3:ANSWER]->(q:Question)
WHERE u.id = '1' AND NOT u<-[:ENDO]-()-[:??]->k

并在??处填写关于endo节点(上面的匿名节点())如何连接的关系类型?

如果您愿意,可以在http://console.neo4j.org创建示例图表,这有助于明确您的意图。

此外,在标签和关系周围使用反引号也没关系,但除非它们包含空格或其他一些不常见的字符,否则你不需要。