简体   繁体   English

使用 Cypher 在 Neo4j 中找到具有模式的最大路径

[英]Find largest path featuring pattern in Neo4j with Cypher

I have the following node types, and a pattern to find:我有以下节点类型,以及要查找的模式:

Node Labels节点标签

Questions Outcomes问题结果

Relationship关系

Answers (with a 'title' field)答案(带有“标题”字段)

I want to "find all questions that are connected to each other by "no" answers and all have a "yes" answer to the same outcome" - i suspect there are hundreds of these groups in my database, some are just two nodes in size, some are up to 5 or 6 nodes.我想“找到所有通过“否”答案相互关联的问题,并且所有问题的答案都是“是”,结果相同”-我怀疑我的数据库中有数百个这样的组,有些只是其中的两个节点大小,有些多达 5 或 6 个节点。 None bigger than six.不超过六。

So I can match all answers joined by nos in a 2-group:所以我可以匹配两个组中由 nos 连接的所有答案:

(outcome)<-[yes]-(question)->[no]->(question)->[yes]->(outcome) (结果)<-[是]-(问题)->[否]->(问题)->[是]->(结果)

But i can't figure out how to apply that to 3 or 4-groups.但我不知道如何将其应用于 3 或 4 组。 I thought i could perhaps do a straight:我想我也许可以做一个直:

(question)<-[no][0..*]-(question) - and find all the questions joined by nos, then in a WHERE clause add something like WHERE all matched questions point to -> outcome - but i have no idea how to express that in cypher (question)<-[no][0..*]-(question) - 并找到所有由 nos 连接的问题,然后在 WHERE 子句中添加类似 WHERE所有匹配的问题指向 -> 结果 - 但我没有想法如何用密码表达它

Can anyone help?任何人都可以帮忙吗?

[RE-RE-EDITED] [重新编辑]

This might work for you.这可能对你有用。

MATCH p=(oc:Outcome)<-[:Answer {title: 'yes'}]-(q1:Question)-[:Answer* {title: 'no'}]->(q2:Question)-[:Answer {title: 'yes'}]->(oc)
WHERE
  NOT (
    (oc)<-[:Answer {title: 'yes'}]-(:Question)-[:Answer {title: 'no'}]->(q1) OR
    (q2)-[:Answer {title: 'no'}]->(:Question)-[:Answer {title: 'yes'}]->(oc)
  ) AND
  ALL(q IN NODES(p)[1..-1] WHERE (q)-[:Answer {title: 'yes'}]->(oc))
RETURN oc, NODES(p)[1..-1] AS questions;

The NOT (...) term in the WHERE clause is there to make sure we only use the longest valid sequences of Questions . WHERE子句中的NOT (...)术语是为了确保我们只使用最长Questions有效序列。

Each result row contains a shared outcome and an ordered collection of Question nodes.每个结果行都包含一个共享结果和一个有序的 Question 节点集合。

This query can take a long time to finish, as it does not specify an upper bound for the variable length pattern.此查询可能需要很长时间才能完成,因为它没有指定可变长度模式的上限。 You may want to specify an appropriate upper bound if this is a problem (eg, Answer*..5 ).如果这是一个问题,您可能需要指定适当的上限(例如, Answer*..5 )。

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

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