2

I'm just starting out so pardon me if this is a stupid question but I was wondering how to stop traversal of a tree on a node property value.

For instance. If you have a tree type graph with nodes and subnodes. All nodes have a property of published which is either 1 or 0. I would like to traverse the tree not including any node with a published:0 (nor it's children even if they have a published:1)

So far (with n being the root node) n-[:HAS_CHILD_CONTENT*]->m WHERE m.published=1 return m; will obviously simply return all published nodes ignoring the fact that some nodes have an unpublished parent

Can anyone help me sort this out? Thanks in advancce.

PS: I'm using Neo4j - Graph Database Kernel 2.0.0-M02 through the webapp cypher shell on ubuntu 12.04 LTS

4
  • Why not just add AND n.published = 1?
    – ean5533
    May 2, 2013 at 21:46
  • @ean5533 that doesn't work. it will still select subnodes that are published even though one of the parent nodes is unpublished May 2, 2013 at 22:27
  • Ah, you added a * to your query that wasn't there before.
    – ean5533
    May 2, 2013 at 23:08
  • @ean5533 yeah sorry about that. I realized later only that I forgot to put that in May 3, 2013 at 10:07

1 Answer 1

3

You can examine the path, like

match p=root-[:PARENT*..]-file 
   where root.name='a' and all(x in nodes(p) WHERE x.published = true) 
return p;

http://docs.neo4j.org/chunked/preview/query-function.html#functions-all

See http://bit.ly/12ARbL5 for an example.

2
  • That's perfect!! Thank you very much! I missed that all was used in that manner from documentation. Do you know what incidence all has on query performance? And/or if there are ways of optimizing performance? May 4, 2013 at 20:08
  • 1
    It is probably not the fastest, as it Cypher does not yet pull these cross path expressions into the pattern matcher. If the performance doesn't meet your needs, check out the Java core API (node.getRelationships and node.getProperty) or the traversal API (docs.neo4j.org/chunked/milestone/tutorial-traversal.html) to shortcut the traversal when you encounter an unpublished parent. May 4, 2013 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.