ean5533
ean5533

Reputation: 8994

Filter based on properties of related nodes

I have a situation where I'm trying to filter out nodes that are not related to nodes with a particular property. For example, say I have a query that gets me all people, but I want to filter down to just the ones that have dogs with brown fur. What I would like to do is something like this:

//For simplicity's sake, assume nodes 1,2,3 are the potentials I care about
START person=node(1,2,3)  
WHERE person-[:has]->(dog{furColor:"Brown"})  // <-- would be nice to use
RETURN person

However, this doesn't work. The parser explicitly tells me:

Properties on pattern elements are not allowed in MATCH.

(Amusing that it references "MATCH" when I'm using "WHERE", but whatever). So you can't reference properties of nodes in this fashion. What other options do I have? I was able to emulate what I want by adding in a MATCH clause...

START person=node(1,2,3) 
MATCH person-[:has]->dog
WHERE dog.furColor! = "Brown"
RETURN person

...but it seems strange and inefficient to me that I have to MATCH additional paths and then filter them back out again. Is there another way?

Upvotes: 2

Views: 492

Answers (2)

Michael Hunger
Michael Hunger

Reputation: 41706

You can try, this, but measure it against the match performance wise.

path expressions in where return a collection of paths, with the collection functions extract and the collection predicate all you can operate on that collection of paths.

START person=node(1,2,3)  
WHERE ALL(dog in extract( path in person-[:has]->() : last(path) // last node of path
           WHERE dog.furColor! = "Brown"
         )
RETURN person

Upvotes: 0

ulkas
ulkas

Reputation: 5918

i'm afraid there is no other way then to use the WHERE clause in your graph design.

but considering you can change and you really want to, you can make the property furColor a relationship type to the Brown node. so instead of keeping the info in the property, you can create additional nodes for all colors, and than just relate the dog nodes with the relationship of type furColor to those color nodes. than, querying would be faster for a MATCH clause like MATCH person-[:has]->dog-[:furColor]->brown (when you also specify the brown node in the START phase)

Upvotes: 1

Related Questions