rorymadden
rorymadden

Reputation: 954

Perform MATCH on collection / Break apart collection

I am trying to mimc the functionality of the neo4j browser to display my graph in my front end. The neo4j browser issues two calls for every query - the first call performs the query that the user types into the query box and the second call uses find the relationships between every node returned in the first user-entered query.

{
  "statements":[{
    "statement":"START a = node(1,2,3,4), b = node(1,2,3,4)
                 MATCH a -[r]-> b RETURN r;",
    "resultDataContents":["row","graph"],
    "includeStats":true}]
}

In my application I would like to be more efficient so I would like to be able to get all of my nodes and relationships in a single query. The query that I have at present is:

START person = node({personId})
MATCH person-[:RELATIONSHIP*]-(p:Person)
WITH distinct p
MATCH p-[r]-(d:Data), p-[:DETAILS]->(details), d-[:FACT]->(facts)
RETURN p, r, d, details, facts

This query runs well but it doesn't give me the "d" and "details" nodes which were linked to the original "person".

I have tried to join the "p" and "person" results in a collection:

collect(p) + collect(person) AS people 

But this does not allow me to perform a MATCH on the resulting collection. As far as I can figure out there is no way of breaking apart a collection.

The only option I see at the moment is to split the query into two; return the "collect(p) + collect(person) AS people" collection and then use the node values in a second query. Is there a more efficient way of performing this query?

Upvotes: 0

Views: 81

Answers (1)

Stefan Armbruster
Stefan Armbruster

Reputation: 39915

If you use the quantifier *0.. RELATIONSHIP is also match at a depth of 0 making person the same as p in this case. The * without specified limits defaults to 1..infinity

START person = node({personId})
MATCH person-[:RELATIONSHIP*0..]-(p:Person)
WITH distinct p
MATCH p-[r]-(d:Data), p-[:DETAILS]->(details), d-[:FACT]->(facts)
RETURN p, r, d, details, facts

Upvotes: 3

Related Questions