Unable to resolve the Query on MATCH


(Sucheta) #1


I have a difficulty.
My application uploads a file , and that file uploads the data in Neo4j database. In my Neo4j Application, i already have a database present and the file uploaded also makes its entries in the same database. That is the Database already has an Organisation and it uploads another new Organisation.

So when i do --> Match (n) Return n; --> i get all the data network of both the root - Organisation nodes.

To obtain a single network , i did -
MATCH (n:Organization) RETURN n; --> ( as Organization is the root node of that uploaded file data)

output - Is only the Organisation single node.

And with this query --> MATCH (n:Organization)-[r]-(m) RETURN n,r,m

The result is just this much ->

Please help me retrieve a query that just gives one network out of the many and the entire network together.

(Andrew Bowman) #2

So if I understand correctly, each network has a single root :Organization node, and you want to view only a single network's nodes starting from its root.

You'll need to first match to the :Organization node you want. As you haven't provided any preferences for which one you want, or supplied anything in your query to find a specific :Organization node, we can just match and limit to a single node. From there we can use a variable-length pattern match to get the entire network.

If you're using only outgoing relationships for this hierarchy, then we can ensure we only traverse outgoing relationships:

MATCH (root:Organization)
WITH root
MATCH p = (root)-[*]->()

(Sucheta) #3

Thanks Andrew. But i have an extended question to this query.


When i run the query you suggested above without mentioning the root node name -

MATCH (root) WITH root LIMIT 1 MATCH p = (root)-->() RETURN p

I get result of root: Century

why is it so arbitrary. Why didn't i get the result with root :Organization.


All around, i have been trying to retreive the nodes with a particular relationship,

And i found out these nodes for retrieving relationships like - Attribute, Method, System,etc.
For example,

And i want the node - list with the relationship -> Parameter

The queries i tried for it is -

MATCH p=()-[r:Parameter]->() RETURN p LIMIT 25

But i also want the number of root nodes. I couldn't find a query for it. Please help .
I want to know how many root nodes are present.

(Andrew Bowman) #4

Hi Sucheta,

Your query isn't correct. The start of your query doesn't have a label, so it's matching to all nodes:

MATCH (root)

Remember that root here is a variable, and variables are only in scope for the duration of a single query, so even if you used root before in a previous query that doesn't matter, it's out of scope. Your match here is binding a new variable root to every node in your graph.

You need

MATCH (root:Organization)

to ensure that the newly-introduced root variable only binds to nodes with the :Organization label (lookup via label scan).

You may want to review the documentation on basic match syntax.

(Sucheta) #5

Hi Andrew,

I did not mention the root node because i want to find the root node. How do we retrieve it ? I want to list down all the root nodes present in my database.

(Ameyasoft) #6

Hi Sucheta,

Try MATCH (r:root) RETURN r;

(12kunal34) #7

Hi Sucheta,

What is root node in your Data ??
Is it label or some criteria ?

(Sucheta) #8

Hi Kunal,

root node is not a name. It is the first node from which all the other nodes extend . I tried this query -

MATCH pathx=(n)-[*1]-(m) RETURN pathx

But it doesn't work. It gives away all the nodes. The entire network.
Please suggest.

(Andrew Bowman) #9

Is an :Organization node always going to be a root node? If so either use the :Organization label when matching to it, or add a secondary :Root label on your :Organization nodes.

If you don't know what type of node the root nodes are going to be, only that they are at the top of any hierarchy, then you can explicitly look for nodes that are not children of any other nodes, then label them as :Root nodes for faster lookup later:

WHERE NOT ()-->(n)
SET n:Root

If that doesn't work for you, then you'll need to tell us what the criteria is for a node to be a root node so we can suggest a query to match to them.

(Sucheta) #10

Yeah. Ok thanks andrew. I just got confused. I have labelled the node as Organization. I shall retrieve like -

var cypherquery = "MATCH (n:Organization) RETURN n"