cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to return all the nodes with a common property

How to return all the nodes and relationships with a common property without forming cartesian product.

6 REPLIES 6

Abstract out the common property values into separate nodes, and do something like this:

MATCH (n:Stuff)
WITH n, distinct(n.color)
MERGE (c:Color { name: n.color })
MERGE (n)-[:COLOR]->(c)

Now you've got an enhanced graph where everything is linked to its color, no cartesian product, and you can navigate your graph by that commonality

Hey, thanks for the reply.Suppose we have one node labeled as Filetype with a property of lets say file_name and then I have another node labeled as FileSize which also has a property named as file_name and few other properties.Then I have a other node with label as FileFormat and common property as file_name, this node has a relationship with FileSize and FileSize has relationship with FileType.So how do I extract the files with based on the common property of files_name and their relationships with each other?

What have you tried? Please post your code and describe what it does and why that isn't right.

MATCH (a:FileType),(b:FileName),(c:FileSize) where a.md5='84c82835a5d21bbcf75a61706d8ab549' and b.md5='84c82835a5d21bbcf75a61706d8ab549' and c.md5='84c82835a5d21bbcf75a61706d8ab549' return a,b,c
FileName has a relationship (x) with FileType label and FileSize has a relationship (y) with FileName label node.They all have a common property of md5 hash .So how do I return all the nodes with the common property.

There are two ways to approach this. Match "1 at a time" and use WITH, or abstract the MD5 out into a separate node. The second approach is better if you need to find matching MD5s frequently, which I'm guessing you do.

Approach 1 (which is similar to what you're doing)

WITH "84c82835a5d21bbcf75a61706d8ab549" as val
MATCH (a:FileType { md5: val })
WITH a
MATCH (b:FileName { md5: val })
WITH a, b
MATCH (c:FileSize { md5: val })

RETURN a, b, c;

The second approach is what I already suggested, and I think it's probably better for you:

MATCH (anyNode)
WHERE anyNode.md5 = "84c82835a5d21bbcf75a61706d8ab549"
MERGE (hash:MD5 { value: "84c82835a5d21bbcf75a61706d8ab549" })
MERGE (anyNode)-[:HASH]->(hash);

If you do this second thing, then you can find all commonalities really easily:

MATCH (hash:MD5 { value: "Whatever you're looking for" })<-[:HASH]-(stuff)
RETURN stuff

First I need to say that I think a separate node as suggested is the best answer. A graph approach.


but if these options didn't do what you need, I'd mention secondary labels

neo4j allows multiple labels, and I believe very few people use that capability. It will mangle your apoc.meta.graph output, but using multiple labels on nodes can be very useful in a few situations.

For example, one could add an additional label to all nodes with the md5 property (e.g. MD5), then put an index on that label/property combination, it would enable returning all three nodes with this cypher

match(a:MD5 {md5:"84c82835a5d21bbcf75a61706d8ab549})
return a

Caveats: Depending on your specific situation, there are likely better ways to model it (as proposed earlier). Technically this would pull back all nodes listed in that index with a matching md5 value... (if you cross label more that those three...)