bepi_roggiuzza
bepi_roggiuzza

Reputation: 295

How to get all relationships of a node with cypher neo4j where clause, not only where matches

I've this cypher query:

MATCH (p:Person)-->(s:Startup)
WHERE p.name =~ '(?i).*something.*' OR p.description =~ '(?i).*something.*' OR s.name =~ '(?i).*something.*'
RETURN DISTINCT p, collect(DISTINCT s)

This returns:

+--------+------------+
|   p    | collect(s) |
+--------+------------+
|   1    |     1      |
+--------+------------+

where I was expecting:

+--------+------------+
|   p    | collect(s) |
+--------+------------+
|   1    |     1, 2   |
+--------+------------+

When someone search for a name, it returns me all the matched people and related startups with this criteria, but I want that it returns me all the matched people and always all the relationships between the 2 nodes.

(i.e. if I search for a startup name, I want that the result is the list of people and the startups in relationship, not only the people and the matched startup)

I hope I've explained well the issue.

My wish is to get the results in one query.

Upvotes: 1

Views: 3417

Answers (1)

Eve Freeman
Eve Freeman

Reputation: 33145

I think you're looking for something like this, where you do the first query to find any person related to your query, and then do a WITH and continue the query using that person:

MATCH (p:Person)-->(s:Startup)
WHERE p.name =~ '(?i).*something.*' OR p.description =~ '(?i).*something.*' OR s.name =~ '(?i).*something.*'
WITH DISTINCT p
MATCH (p)-->(s:Startup)
RETURN DISTINCT p, collect(DISTINCT s)

Upvotes: 3

Related Questions