Using Property value of Node1 as a label name of Node2 - neo4j

I'm using neo4j 2.1.2 Community Edition.
I have a node called Labelled as Company and it's properties are as below:
id : 101
Name : Company
Also i have more node which is a child node of Company with a relationship :Active . I want this child node label-name should derive from the ParentNode. That is the parent Name property should act as a label for it's Child Node.
My Child node looks like as below:
Id : 101
Name: Flipkart
How can i achieve this through Cypher query. How can i pass the Property of one node to act as a Label of another node ?
Thanks

Related

Create Nested tree structure in neo4j

consider a situation . There is a company say A. So its a root node. Node is created (n: Comapny{id:'a',title:'A' parentId:''}) . A adds 2 depts say IT Team, the testing team. .So Node is created
({cl:ComapnyLeaf{id:'itTeam',title:'IT Team',parentId:'a'}); ({cl:ComapnyLeaf{id:'testingTeam',title:'TESTING Team',parentId:'a'}); Now it adds emapolyee
({cl:ComapnyLeaf{id:'emp1',title:'Emp 1',parentId:'itTeam'});
so while creating nodes relationship between them should be created my final out will look like
Company(A) < ---- child--- it Team <----child---emp1
< ---- child--- Testing Team
I think you need to recheck your query and your desired output, you have misspellings and bad syntax in there, and your output doesn't reflect your input. The testing team should end up being the child of :Company node a, not the Emp 1 :CompanyLeaf node.
Let's first create the nodes like this (you'll need to enable the multi-statement query editor in the Neo4j Browser preferences):
CREATE (n:Company:Node {id:'a', title:'A'});
CREATE (cl:CompanyLeaf:Node {id:'itTeam', title:'IT Team', parentId:'a'});
CREATE (cl:CompanyLeaf:Node {id:'testingTeam', title:'TESTING Team', parentId:'a'});
CREATE (cl:CompanyLeaf:Node {id:'emp1', title:'Emp 1', parentId:'itTeam'});
Then let's add an index on :Node(id) to support the fast lookups we'll need to create the relationships.
CREATE INDEX ON :Node(id)
Then for checking all :CompanyLeaf nodes in the graph and attaching them to a node with the given id as the parent, we can use this:
MATCH (c:CompanyLeaf)
WHERE NOT (c)-[:PARENT]->()
MATCH (parent:Node {id:c.parentId})
CREATE (c)-[:PARENT]->(parent)
The WHERE NOT clause here makes sure we don't create duplicate relationships in case we already ran the query, or in case we do several imports of nodes with relationship creation happening in between.
Also this is creating :PARENT relationships pointing the parent, since your example didn't make sense with :child relationships pointing to the parent.

what does colon mean in a merge clause in neo4j?

MERGE (robert:Critic)
RETURN robert, labels(robert)
A new node is created because there are no nodes labeled Critic in the database.
But what is robert?and what does a colon mean?
MERGE (charlie { name: 'Charlie Sheen', age: 10 })
RETURN charlie
A new node with the name 'Charlie Sheen' will be created since not all properties matched the existing 'Charlie Sheen' node.
but in this example,there is no colon,why the variable charlie is still returned?
robert is a variable name that has meaning only within the query in which it is used (and is not saved in the DB). It is defined and instantiated the first time it is used in the query, and as long as there is no WITH clause afterwards, the query will always use robert to refer to the same node (or relationship, path, ...).
A WITH clause will only carry forward the variables that it specifies. So, WITH robert, foo would allow the same robert variable name and value to be used after the WITH clause. But WITH foo will cause robert to be forgotten.
A colon is used in a node to designate that the following name is a label. It is also used in a relationship to designate that the following name is a type.
In this cypher, robert is the node while Critic is the node label
MERGE (robert:Critic)
In below, you are creating a node (without a node label) and with properties name and age
MERGE (charlie { name: 'Charlie Sheen', age: 10 })
Try below. Notice the colon BEFORE the node label
MERGE (:Critic)
In short, for your examples, colon is a way for you to differentiate if you are creating a node or a node label or both.

Neo4j node name significance

What is the significance of node name in Neo4j. What are the repercussions of making nodes without node name. Like the query
CREATE (:player {name: "Roger", YOB: 1985, POB: "Switzerland"})
has no node name?
The node "names" are actually variables. You can assign a node (or a relationship) to a variable to handle this node in the rest of the Cypher query. The Neo4j docs says that:
When you reference parts of a pattern or a query, you do so by naming
them. The names you give the different parts are called variables.
For example, you can create a node and return it using a variable:
CREATE (node:player {name: "Roger", YOB: 1985, POB: "Switzerland"})
RETURN node
but if you are not referencing the node in the rest of the query has no necessity to assign it to a variable, in this case your Cypher example is fine.
Also, if you don't add a variable name, that the Cypher planner will automatically assign a unique variable name to the items you didn't. So the only repercussion for not using a variable name, is that you can't make explicit references to that item (Thanks to #Tezra).

How to get last node created in neo4j?

So I know when you created nodes neo4j has a UUID for each node. I know you can access a particular node by that UUID by accessing the ID. For example:
START n=node(144)
RETURN n;
How would I get the last node that was created? I know I could show all nodes and then run the same command in anotehr query with the corresponding ID, but is there a way to do this quickly? Can I order nodes by id and limit by 1? Is there a simpler way? Either way I have not figured out how to do so through a simple cypher query.
Every time not guaranteed that a new node always has a larger id than all previously created nodes,
So Better way is to set created_at property which stores current time-stamp while creating node.
You can use timestamp() function to store current time stamp
Then,
Match (n)
Return n
Order by n.created_at desc
Limit 1
Please be aware that Neo4j's internal node id is not a UUID. Nor is it guaranteed that a new node always has a larger id than all previously created nodes. The node id is (multiplied with some constant) the offset of the node's location within a store file. Due to space reclaiming a new node might get a lower id number.
BIG FAT WARNING: Do not take any assumption on node ids.
Depending on your requirements you could organize all nodes into a linked list. There is one "magic" node having a specific label, e.g. References that has always a relationship to the latest created node:
CREATE (entryPoint:Reference {to:'latest'}) // create reference node
When a node from your domain is created, you need to take multiple actions:
remove the latest relationships if existing
create your node
connect your new node to the previously latest node
create the reference link
.
MATCH (entryPoint:Reference {to:'latest'})-[r:latest]->(latestNode)
CREATE (domainNode:Person {name:'Foo'}), // create your domain node
(domainNode)-[:previous]->(latestNode), // build up a linked list based on creation timepoint
(entryPoint)-[:latest]->(domainNode) // connect to reference node
DELETE r //delete old reference link
I finally found the answer. The ID() function will return the neo4j ID for a node:
Match (n)
Return n
Order by ID(n) desc
Limit 1;

return nodes with selected properties

I am using a graph database having many nodes connected to a super node by a relationship TYPE. And each of those sub nodes have properties via a FROM relationship.
How should i access the properties of individual nodes in cypher query?
I tried this but its not working.
start a=node(2) match (a)<-[:TYPE]-(node) match (node)<-[:FROM]-(prop) where prop.name="ABC" return node;
Here i have to return a node with a property name whose value is ABC!?
How should i correct it?
START a=node(2)
MATCH (a)<-[:TYPE]-(node)<-[:FROM]-(prop)
WHERE prop.name="ABC"
RETURN node;
You could also do this using indexes:
START a=node:properties('name:ABC')
MATCH (node)<-[:FROM]-(a)
Where 'properties' is your index which indexes name as 'ABC'. Realize that your above query is only interested in the node related with :FROM, therefore searching in the first example could be very long compared to simply using indexing.
If you are using Neo4j 2.x, then you are far better off with labels.
MATCH (a:Property(name='ABC'))-[:FROM]->(node)
RETURN node
Where you would set the label 'Property' when you create/update a property node.

Resources