cypher - If the number of properties is greater than n, return a subgraph in Neo4j -


this question direct extension of a question asked previously.

say have graph database looks this:

enter image description here

just previous question asked, interesting thing someproperty can 'yes' or 'no'.

in top row, 1 of 3 nodes have 'yes' property.

on bottom row, there 3 nodes have 'yes' property. other 2 have 'no'.

how write cypher query returns whole bottom row, asking question: do either of these disjoint subgraphs have 2 or more values someproperty = 'yes'?

previously, brilliant @cybersam recommended using like:

match p=(:person)-[:related_to*]->(:person) 2 < reduce(s = 0, x in nodes(p) | case when x. someproperty = 'yes' s + 1 else s end) return p; 

...which return matching paths. in case, return

enter image description here

but trying generalize return whole set of 5 "b" nodes.

after struggling this, realize i'm trying create 3 node subgraph, not return it. i'm trying use existence way return supergraph of match, , ignore non-connected subgraphs! getting pretty complicated , i'm stumped.

here code:

create (albert:person {gender: 'male', name: 'albert', someproperty: 'yes'}) create (annie:person {gender: 'female', name: 'annie', someproperty: 'no'}) create (adrian:person {gender: 'female', name: 'adrian', someproperty: 'no'})  create (albert)-[r1:related_to]->(annie) set r1.relationship='related' create (annie)-[r2:related_to]->(adrian) set r2.relationship='related'  create (bill:person {gender: 'male', name: 'bill', someproperty: 'yes'}) create (barb:person {gender: 'female', name: 'barb', someproperty: 'yes'}) create (barry:person {gender: 'male', name: 'barry', someproperty: 'yes'}) create (bart:person {gender: 'male', name: 'bart', someproperty: 'no'}) create (bartholemu:person {gender: 'male', name: 'bartholemu', someproperty: 'no'})  create (bill)-[r4:related_to]->(barb) set r4.relationship='related' create (barb)-[r5:related_to]->(barry) set r5.relationship='related' create (barry)-[r6:related_to]->(bart) set r6.relationship='related' create (bart)-[r7:related_to]->(bartholemu) set r7.relationship='related' 

this query filter out sub-paths results:

match p=(a:person)-[:related_to*]->(b:person)   not ()-[:related_to]->(a) ,   not (b)-[:related_to]->() ,   2 < reduce(s = 0, x in nodes(p) | case when x. someproperty = 'yes' s + 1 else s end) return p; 

explanation:

  • not ()-[:related_to]->(a) true if a node not @ end of related_to relationship.
  • not (b)-[:related_to]->() true if b node not @ start of related_to relationship.

therefore, matching path cannot sub-path.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -